Skip to content

Commit 8224b9e

Browse files
Handle in-browser translations more gracefully
When Chrome is presented with a natural language, it can offer to translate the HTML page. Unfortunately, this caused labs to fail. This commit modifies to code to work around this, so that after translation the lab continues to work. The explanation about the lab is translated. Unfortunately, this does *not* translate the responses shown via alert boxes (e.g., hints and congrats). However, since the lab now *works*, users can copy that text into something to do a translation. This commit makes the labs much more accessible to those who speak natural languages we don't currently have translations for. Signed-off-by: David A. Wheeler <[email protected]>
1 parent 5af313d commit 8224b9e

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

docs/labs/checker.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,15 @@ const attemptIdPattern = /^attempt(\d+)$/;
342342

343343
/*
344344
* Given Node @form in document, return array of indexes of input/textareas
345+
* The values retrieved are *input* field indexes (`inputIndexes`),
346+
* starting at 0 for the first user input.
347+
*
348+
* Note: At one time we ran this calculation when a user pressed
349+
* a button. However, if you *translate* the page using Chrome's translator,
350+
* that will cause this routine to fail because querySelectorAll will fail.
351+
* To work around this,
352+
* it's better to calculate all of these values on page load and store it
353+
* (e.g., as dataset.inputIndexes values on the buttons).
345354
*/
346355
function findIndexes(form) {
347356
try {
@@ -364,7 +373,8 @@ function findIndexes(form) {
364373
}
365374
}
366375
// alert(`findIndexes = ${result}`);
367-
return result;
376+
// Return as JSON string so it's easily stored, etc.
377+
return JSON.stringify(result);
368378
}
369379
} catch (e) {
370380
showDebugOutput(
@@ -499,13 +509,16 @@ function showHint(e) {
499509
} else if (!hints) {
500510
alert(t('no_hints'));
501511
} else {
502-
let validIndexes = findIndexes(e.target.form);
512+
// Use *precalculated* input field indexes to work around
513+
// problem in Chrome translator.
514+
let validIndexes = e.target.dataset.inputIndexes;
503515
alert(findHint(attempt, validIndexes));
504516
}
505517
}
506518

507519
function showAnswer(e) {
508-
let formIndexes = findIndexes(e.target.form); // Indexes in this form
520+
// Get indexes in *this* form.
521+
let formIndexes = JSON.parse(e.target.dataset.inputIndexes);
509522
let goodAnswer = formIndexes.map(i => expected[i]).join('\n\n');
510523
if (!user_solved) {
511524
user_gave_up = true;
@@ -819,6 +832,8 @@ function initPage() {
819832
}
820833
for (let hintButton of document.querySelectorAll("button.hintButton")) {
821834
hintButton.addEventListener('click', (e) => { showHint(e); });
835+
// Presumes button's parent is the form
836+
hintButton.dataset.inputIndexes = findIndexes(hintButton.parentNode);
822837
if (!hintButton.title) {
823838
hintButton.title = t('hint_title');
824839
}
@@ -831,6 +846,8 @@ function initPage() {
831846
}
832847
for (let giveUpButton of document.querySelectorAll("button.giveUpButton")) {
833848
giveUpButton.addEventListener('click', (e) => { maybeShowAnswer(e); });
849+
// Presumes button's parent is the form
850+
giveUpButton.dataset.inputIndexes = findIndexes(giveUpButton.parentNode);
834851
if (!giveUpButton.title) {
835852
giveUpButton.title = t('give_up_title');
836853
}

0 commit comments

Comments
 (0)