I’m creating a Chrome extension that appends a script tag to a page, and then uses code defined in that external script:
$('body').append('<script src="..."></script><script>console.log(SomeObject)</script>');
SomeObject is defined in the external script, so I should be able to access it, right? Well, I can’t, as I get an “undefined” error.
I even tried using head.js to load the external script and execute a function after the script is loaded, to no avail.
If I open the Chrome console, I can access the damn object just fine!!!
I tried both a content script and executeScript inside a background page to no avail. On both, if I use console.log(window), I can inspect the window object in the console, and SomeObject is nowhere to be found. If I inspect the window object on the Chrome console, there it is!
Are injected scripts sandboxed somehow or what gives?
Thanks!
So it seems the answer is you can’t, due to security restrictions 🙁
I had to hack my way around it by using an
iframe(oddly, theiframecan access its parent document).