Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid JS syntax errors when visiting our site from our browser #1362

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wbetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def decorator(new_cls):
if isinstance(new_cls, type): # Patching classes
assert isinstance(existing_cls, type), f"Can't patch {existing_cls} with {new_cls}"
for attr, obj in new_cls.__dict__.items():
if attr in ["__module__", "__dict__", "__weakref__", "__doc__"]: continue
if attr in ["__module__", "__dict__", "__weakref__", "__doc__", "__firstlineno__", "__static_attributes__"]: continue
assert isinstance(obj, type(record)), f"Can't patch attribute {attr} of {new_cls} to be {obj}"
setattr(existing_cls, attr, obj)
elif isinstance(new_cls, type(record)): # Patching functions
Expand Down
71 changes: 46 additions & 25 deletions www/book.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
/* NOTE: because this is deployed to browser.engineering, this must use
* only old-school JavaScript supported by DukPy, including no `let`, no
* `for of`, no arrow functions, and so on.
*
* Also there is no `window` object in early versions of the browser; so
* any references to window.addEventListener need to be guarded. */

// Add click handlers to open and close inline notes on small screens.
// Notes start out closed.
function addEventListeners() {
for (let note_container of document.querySelectorAll(".note-container")) {
note_container.addEventListener("click", (event) => {
console.log(event.target);
var containers = document.querySelectorAll(".note-container")
for (var i = 0; i < containers.length; i++) {
var callback = (function(note_container) {
return function (event) {
if (event.target != note_container &&
event.target != note_container.firstElementChild)
return;
let classes = note_container.classList;
var classes = note_container.classList;
if (!classes.contains("open"))
classes.add("open");
else
classes.remove("open");
event.preventDefault();
});
}
})(containers[i]);
containers[i].addEventListener("click", callback);
}

for (let header of document.querySelectorAll("h1")) {
header.addEventListener("click", (event) => {
var headers = document.querySelectorAll("h1");
for (var i = 0; i < headers.length; i++) {
var callback = (function(header) {
return function(event) {
if (header.id)
window.location.href = `#${header.id}`;
window.location.href = "#" + header.id;
event.preventDefault();
});
}
})(headers[i])
headers[i].addEventListener("click", callback);
}

}

window.addEventListener("load", addEventListeners);
if (globalThis.window && window.addEventListener)
window.addEventListener("load", addEventListeners);

function resize_iframes(event) {
let elts = document.querySelectorAll("[data-big-height][data-small-height]");
for (let elt of elts) {
var elts = document.querySelectorAll("[data-big-height][data-small-height]");
for (var i = 0; i < elts.length; i++) {
if (document.documentElement.clientWidth <= 800) {
elt.height = elt.dataset.smallHeight;
elts[i].height = elt.dataset.smallHeight;
} else {
elt.height = elt.dataset.bigHeight;
elts[i].height = elt.dataset.bigHeight;
}
}
}
Expand All @@ -51,21 +66,25 @@ const COLORS = [

function highlight_regions() {
var pres = document.querySelectorAll(".highlight-region");
for (var pre of pres) {
for (var j = 0; j < pres.length; j++) {
var pre = pres[j];
var marks = pre.querySelectorAll("mark");
for (var i = 0; i < marks.length; i++) {
let [bgcolor, labelcolor] = COLORS[i % COLORS.length];
let mark = marks[i];
let label = mark.querySelector("label");
var color_entry = COLORS[i % COLORS.length];
var bgcolor = color_entry[0], labelcolor = color_entry[1];
var mark = marks[i];
var label = mark.querySelector("label");
mark.style["background-color"] = bgcolor;
label.style["color"] = labelcolor;
}
}
}

window.addEventListener("load", resize_iframes);
window.addEventListener("resize", resize_iframes);
window.addEventListener("DOMContentLoaded", highlight_regions);
if (globalThis.window && window.addEventListener) {
window.addEventListener("load", resize_iframes);
window.addEventListener("resize", resize_iframes);
window.addEventListener("DOMContentLoaded", highlight_regions);
}

function close_signup(e) {
window.localStorage["signup"] = "close";
Expand All @@ -83,7 +102,8 @@ function setup_close() {
}
}

window.addEventListener("DOMContentLoaded", setup_close);
if (globalThis.window && window.addEventListener)
window.addEventListener("DOMContentLoaded", setup_close);

// Return UUID for user; generate and store in local storage if first time
function get_or_set_id() {
Expand All @@ -100,17 +120,18 @@ function get_or_set_id() {
}

function quiz_telemetry(event_type, event_payload) {
event_payload = { userId: get_or_set_id(), ...event_payload };
event_payload.userId = get_or_set_id();
if (event_type === "answers") {
fetch("/api/quiz_telemetry", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(event_payload)
}).catch(err => console.error('Quiz telemetry error', err));
}).catch(function (err) { console.error('Quiz telemetry error', err); });
}
}

// The quiz code picks this function up out of the window object
window.telemetry = { log: quiz_telemetry };
if (globalThis.window)
window.telemetry = { log: quiz_telemetry };
12 changes: 8 additions & 4 deletions www/feedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

// Thanks for reading the code! You can hit Ctrl-E / Cmd-E to access the feedback tools.

// This code is written in a bit of an odd style to avoid error
// messages in the WBE browser. There's a lot of old-school JS.

var chapter_overlay;

function ctrl_key_pressed(e) {
Expand All @@ -20,6 +23,7 @@ function ctrl_key_name() {
}
}

if (document.addEventListener)
chrishtr marked this conversation as resolved.
Show resolved Hide resolved
document.addEventListener("DOMContentLoaded", function() {
if (window.localStorage["edit"] == "true") {
typo_mode();
Expand All @@ -36,7 +40,7 @@ document.addEventListener("DOMContentLoaded", function() {
}
});

let feedback_button = document.querySelector("#feedback-button");
var feedback_button = document.querySelector("#feedback-button");
if (feedback_button) {
feedback_button.addEventListener("click", function(e) {
setup_chapter_feedback();
Expand Down Expand Up @@ -239,7 +243,7 @@ function submit_chapter_comment(comment, email) {
}));
}

let previous_comment = null;
var previous_comment = null;

function setup_chapter_feedback() {
var submit = Element("button", { type: "submit" }, "Submit feedback");
Expand Down Expand Up @@ -294,11 +298,11 @@ function setup_chapter_feedback() {
this.querySelector("input[name='email']").value)
e.preventDefault();
this.querySelector(".confirm-feedback").classList.add("active");
setTimeout(() => chapter_overlay.remove(), 2000);
setTimeout(function() { chapter_overlay.remove(); }, 2000);
}

function do_cancel(e) {
let result = this.querySelector("textarea");
var result = this.querySelector("textarea");
if (result)
previous_comment = result.value;
chapter_overlay.remove();
Expand Down
Loading