0

I don't know if this question is against the rules, so I'll understand if it gets deleted or if I receive a punishment.

The following script creates an alert with the answer of a multiple choice exercise in Khanacademy. It works basically in all exercises that have an "A B C" type selectable multiple choice answers. It used to work perfectly before, but now the console spits out the following error:

ERROR: Execution of script 'KA Multiple Choice' failed! Cannot read property 'props' of undefined

I don't know if the method used was patched, but I would like to know if I can do anything to fix it.

Here is the script:

const arrayanswers = JSON.parse(window["./javascript/app-shell-package/app-entry.js"].props.componentProps.initialItem.itemData).question.widgets["radio 1"].options.choices,
filtered = arrayanswers.reduce((acc, item, index) => ((item.correct) ? [...acc, index] : acc), []);

alert("0 = Option A" + '\n' + "1 = Option B" + '\n' + "2 = Option C" + '\n' + "3 = Option D" + '\n' + "Answers are: " + `${filtered.join(', ')}`);

EDIT: I got it working by using a Snippet from Chrome. However, I would still like to make it work in Tampermonkey.

TBG
  • 305
  • 4
  • 10
  • 2
    The error means that `window["./javascript/app-shell-package/app-entry.js"]` is `undefined`, which is hardly surprising. What is it that you expect that to be? – Pointy May 13 '19 at 15:05
  • The original JSON.parse was `JSON.parse(window["./javascript/app-shell-package/app-entry.js"].props.componentProps.initialItem.itemData).question.widgets["radio 1"].options.choices` which outputted the array of possible answers. The `filtered` variable obviously filters out the arrays which have the incorrect answer. When I input that JSON.parse in the console, it does work and the arrays do show – TBG May 13 '19 at 15:29
  • It seems to be undefined as soon as I put the JSON.parse into a variable. Executing it directly on console works perfectly well. – TBG May 13 '19 at 15:46
  • You may need a `// @grant unsafeWindow` and use unsafeWindow instead of window. See TM docs for more information – YellowAfterlife May 14 '19 at 10:00
  • I tried this `unsafeWindow`. It didn't change anything with Tampermonkey. The script still works perfectly if it is entered directly in the Console or run as a snippet in the dev panel. – TBG May 14 '19 at 14:54

0 Answers0