View Full Version : Can't access frame content

02-29-2012, 05:27 PM
I'm trying to write a little Greasemonkey script (in Firefox) to manipulate the top bar Google Translate produces when translating a site. For example: http://translate.google.com/translate?hl=en&sl=auto&tl=en&u=http://www.google.de.

Google has employed traditional HTML frames. According to many websites, I can use something like window.frames[0].contentDocument to access the first frame. However, this is refusing to work!

I have been debugging this for hours. I have verified that I am indeed running the script from the top-level window (window.parent === window.top), and I am able to manipulate the frames themselves without a problem. When I try something like window.frames[0].contentDocument.URL, it returns as about:blank (I also get this with the other frame, i.e., frames[1]). I have tried several different methods of getting at that frame, including getElementsByTagName("frame")[0] (which works for manipulating the frame itself, but not for editing the frame's content through contentDocument).

Here's my little script so far. I've included the first part so you can see for yourself that I'm definitely in the top-level window and using the right @include because it works. In this example, the URL returned is "about:blank" (again, I'm in Firefox).

// ==UserScript==
// @name GTranslate
// @include /^https?://translate\.google\.com/translate\?.*/
// ==/UserScript==

// change how the frame is displayed
var bar = document.getElementsByTagName("frame")[0];

// manipulate the content inside the frame
var gt = bar.contentDocument;