How can I access a newly opened tab window object? [in the Firefox extension]

I am trying to convert a Greasemonky script to an extension for Firefox, and I am trying to have my extension automatically attach a simple script to any web page when opening a new tab. I am converting a script from Greasemonkey because I would like to use the advanced settings and menu options.

I access the tab using this:

var container = gBrowser.tabContainer; container.addEventListener("TabOpen", tabAdded, false); function tabAdded(event) { var newtabwindow = event.target.____ //I don't know what goes here //attach script to newtabwindow } 

and my goal is to add the script to the document in a new tab after loading using this function:

 function scriptrunner(targetwindow) { var myScript = targetwindow.content.document.createElement('script'); myScript.type = 'text/javascript'; myScript.setAttribute('src','chrome://addonname/content/addonscript.js'); targetwindow.content.document.getElementsByTagName('head')[0].appendChild(myScript); } 

This function is great for attaching a script to the current page when connecting to a toolbar button using oncommand = "scriptrunner (window)", but I don’t know how to access the window in a newly opened tab, or if I have to cut the window from equations and access the document in another way.

+6
source share
1 answer

You are looking for contentWindow , which is a property of the browser element.

For a tab call gBrowser.getBrowserForTab to get the browser element associated with the tab . Then use the contentDocument or contentWindow property of the browser element (they are equivalent to the document and window objects you should already be familiar with).

Also, if I'm not mistaken, you will need to listen to the "load" contentWindow event in addition to listening for events for the tab .

+2
source

All Articles