I’m writing a form-filling userscript (for Tampermonkey in Chrome) which should take variables from JavaScript already in the page and put those variables in the form. Here’s my code so far:
document.getElementById('entry_1').value = window.a1[0];
repeated for entry_2 through entry_5 and a1 to a5. a1 is defined like this:
var a1 = new Array("Green","green");
so running the userscript should put the value “Green” in the form field entry_1. However, I’m getting this:
TypeError: Cannot read property '0' of undefined
at Function.eval (eval at <anonymous> (eval at eventHandler (eval at <anonymous> (chrome-extension://dhdgffkkebhmkfjojejmpbldmpobfkfo/content.js:56:21))), <anonymous>:65:53)
As stated before, I’m using Chrome. Running the userscript code in the javascript Console works fine and does change the form field. How can I achieve the same result from the Tampermonkey script?
The correct way to do this would be to use
unsafeWindowinstead ofwindow, like this:Note, however, that this does not work in the Chrome JS console.