Vincent Van Den Berghe
Vincent Van Den Berghe

Reputation: 5575

Get hosting XULDocument for a ContentWindow or HTMLDocument in Firefox extension?

I have an Firefox Extension with a XUL overlay containing some Javascript with some event handlers. Among those is an EventListener for DOMContentLoaded. I have a second overlay I only want to load when visiting a certain website.

Here's some of the code I have so far:

var appcontent = document.getElementById("appcontent"); // browser
if (appcontent) appcontent.addEventListener("DOMContentLoaded", onPageLoad, true);

function onPageLoad(event) {
    var doc = iEvent.originalTarget; // doc is document that triggered "onload" event
    var win = doc.defaultView;

    if (doc.nodeName != 'document') return; // only documents
    if (win != win.top) return; //only top window.
    if (win.frameElement) return; // skip iframes/frames

    if(doc.location.href.search('http://somewebsite.com/') > -1) {
        //Find XULDocument somehow
        //var xulDoc = ??????;
        xulDoc.loadOverlay('chrome://myextension/content/secondoverlay.xul', null);            
    }
}

How can I retrieve the XULDocument hosting the DOM, given the DOMContentLoaded event data?

Upvotes: 0

Views: 227

Answers (1)

nmaier
nmaier

Reputation: 33192

Well, the XUL window is just window, aka. the global scope.

So the following two lines should both work and should be the same:

window.document.loadOverlay(...);
document.loadOverlay(...);

However, this most is not really what you want, because the XUL window is still the main browser.xul which hosts all content windows. There is no dedicated XUL window per content window! Loading the second overlay will overlay the whole browser window, not just the "tab" (or whatever) and will stay once you close the tab (content window) or navigate away.

Now, the question is: What do you really want to achieve?

Display some UI (toolbar button, menu, whatever) only for certain pages? Then usually you'd overlay all your stuff once (on the initial load, i.e. in the "first" overlay) and just hide/show your UI according to your rules. See Tabbed Browser for some code snippets when dealing with tab switching and/or page loads.

Or you really want to apply something to the content window itself. Then you'd usually just modify the DOM of the content window directly.

Upvotes: 1

Related Questions