Take the 2-minute tour ×

I've got a little problem with UserScripts in Google Chrome, to be precise with getting to the object window of an iframe. Very doable via the Google Chrome console, very impossible via the UserScript or so it seems so far. To be honest it seems as if it was on purpose, as if there was some reason why I'm not allowed to access other window objects.

document.body.innerHTML += "<iframe name='iframe'></iframe>";


chrome-extension://eelclpmekkanegjojjmaldeddncficoj/script.js:14 undefined
chrome-extension://eelclpmekkanegjojjmaldeddncficoj/script.js:15 undefined
chrome-extension://eelclpmekkanegjojjmaldeddncficoj/script.js:16 undefined
chrome-extension://eelclpmekkanegjojjmaldeddncficoj/script.js:17 undefined
chrome-extension://eelclpmekkanegjojjmaldeddncficoj/script.js:18 undefined

Might I ask what Chrome's problem is? I don't really get why should a UserScript have lesser access to javascript than a normal script, what are the implications? By the way, yes, the iframe is on the same domain and protocol. :(

share|improve this question
I believe that an injection would do the trick, but why does GChrome behave like this? Is it a bug, or on purpose? –  Witiko Oct 31 '10 at 14:45
add comment

1 Answer

up vote 3 down vote accepted

UnsafeWindow isn't support by Chrome, try TamperMonkey, pretty sure it provides read-only access to that variable.

contentWindow.document isn't available for Chrome. contentDocument should work.

Also, XMLHttpRequest for cross domains also aren't supported. Most of these are for security purposes. Userscripts in Chrome are content scripts, they cannot access the functions/variables defined by web pages or by other content scripts. It's mostly for security and isolation of scripts, to prevent scripts from conflicting with each other.

As for document.getElementsByName('iframe')[0].contentWindow, I think it's because the way you're trying to add in your iframe. For starters, don't name your iframe as 'iframe', always a very bad practice.

Instead to attempting to add it into the body's innerHTML, use appendChild(), and append a new iframe object into document.body. Also, instead of document.getElementsByName, try document.body.getElementsByName.

I write greasemonkey scripts for firefox, and Chrome seems too restrictive. And I hope you know about the location hack for userscripts. Check out http://wiki.greasespot.net/Location_hack . You can use Javascript in your userscripts ;) And just to let you know right now, I would VERY much warn against messing with iframes and userscripts. I've wrote a script for Greasemonkey, been trying for 6 months, but somehow, when I involve code inside the iframe, half of the time, that result is undefined, and I never get into that problem with javascript. Also, if you inject .js script objects into a document from a userscripts, the new code is still somehow affected, and so how, randomly, elements show up as undefined. After 6 months of trying, I gave up, and I just have a bookmarklet just injects a .js script into documents manually. Of course, you don't have to do that, you can just use a location hack to inject the code from a userscript. But as for writing entire scripts based on userscripts for iframes, I'm staying far far away...

share|improve this answer
About scripting conventions and valid scripting - I know that, I wrote it simple for you to see, writing it the nice way changes nothing in this case.Also, Chrome already supports unsafeWindow, it wraps all extensions in (function(unsafeWindow) {})(window) –  Witiko Oct 31 '10 at 17:43
Currently I'm double-injecting (first script is injected to the current document and it injects the main script into the iframe whereat they both delete themselves from the documents), should it fail, I'll certainly try that location hack. –  Witiko Oct 31 '10 at 17:50
"and so how, randomly, elements show up as undefined" - maybe you wrote the code so that it was runing in the original window's scope. It's easy to mess up when ure working with multiple scopes. –  Witiko Oct 31 '10 at 19:29
Ah, thanks for that info. Good luck with your script, I'll see if I can inject into the iframe instead. –  LostInTheCode Nov 1 '10 at 21:36
That location hack is amazingly useful. Ugly, but so simple and seductive. It's like the Dark Side. –  Protector one Mar 1 '12 at 8:32
add comment

Your Answer


