Skip to content

Commit 43cbb7f

Browse files
Merge pull request #766 from ossf/fix_browser_translation
Handle in-browser translations more gracefully
2 parents 9c7aead + 8224b9e commit 43cbb7f

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)