I'm currently in the process of writing a userscript for a certain website. What I'm trying to accomplish requires access to the content scope with the page variables and functions.
Standard operating procedure for that sort of thing seems to be a function that injects a new <script>
tag into the DOM, like so:
function injectScript(f) {
var target = document.head || document.documentElement;
var script = document.createElement("script");
script.type = "text/javascript";
script.text = "(" + f.toString() + ")();";
target.appendChild(script);
target.removeChild(script);
}
However, this approach doesn't work because the website has a Content Security Policy which prevents the injected script tag from executing! I've also tried the location hack (basically location.href="javascript:someCode()"
), but that also counts as an inline script and won't work.
unsafeWindow
would be an option that is unfortunately only available with Greasemonkey. Ideally my script should work with Chrome as well. Is there a better way to access page variables?