views:

496

answers:

1

I need it to use in my firefox extension. I already tried window.onload event listener, and check if current url == old url, and it's a good idea, but it does'nt work when page loads pdf.

I saw hash changed function too but it works only with ff 3.6; i need it to work at least with ff 3.

So, i need an event listener that check if document.location changes.

Thanks

+1  A: 

For example, you could use:

var mainWindow = window
    .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
    .getInterface(Components.interfaces.nsIWebNavigation)
    .QueryInterface(Components.interfaces.nsIDocShellTreeItem).rootTreeItem
    .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
    .getInterface(Components.interfaces.nsIDOMWindow);

mainWindow.getBrowser().addEventListener("DOMContentLoaded",
                        your_function, false);

But, the point is, you need to add the listener to the browser, not window.

EDIT
See https://developer.mozilla.org/En/Code_snippets/Tabbed_browser for info on accessing the browser from different contexts, as well as some other useful info.

EDIT
Just to add a little more detail....

function your_function(event) {
    if (event.originalTarget instanceof HTMLDocument) {
    var doc = event.originalTarget;
        // if it's just a frame element, then return and wait for the
        // main event to fire.
        if (event.originalTarget.defaultView.frameElement)
             return;

        // now you can use doc.location however you want
    }
}

Note that this will respond to pages opened in any tab, not a specific tab.


For a specific tab, you could use something like this:

var newTabBrowser = gBrowser.getBrowserForTab(gBrowser.addTab("http://www.google.com/"));
newTabBrowser.addEventListener("load", function () {
    newTabBrowser.contentDocument.body.innerHTML = "<div>hello world</div>";
}, true);

The above adds a new tab and then adds a listener. However, for all tabs, you'll need something like the following. And then add the "DOMContentLoaded" event listener on each tab when added (and removed when closed).

You may also want to see: https://developer.mozilla.org/En/Code_snippets/Tabbed_browser#Notification_when_a_tab_is_added_or_removed and https://developer.mozilla.org/En/Code_snippets/Tabbed_browser#Getting_document_of_currently_selected_tab

(For preservation)

function exampleTabAdded(event) {
  var browser = gBrowser.getBrowserForTab(event.target);
  // browser is the XUL element of the browser that's been added
}

function exampleTabMoved(event) {
  var browser = gBrowser.getBrowserForTab(event.target);
  // browser is the XUL element of the browser that's been moved
}

function exampleTabRemoved(event) {
  var browser = gBrowser.getBrowserForTab(event.target);
  // browser is the XUL element of the browser that's been removed
}

// During initialisation
var container = gBrowser.tabContainer;
container.addEventListener("TabOpen", exampleTabAdded, false);
container.addEventListener("TabMove", exampleTabMoved, false);
container.addEventListener("TabClose", exampleTabRemoved, false);

// When no longer needed
container.removeEventListener("TabOpen", exampleTabAdded, false);
container.removeEventListener("TabMove", exampleTabMoved, false);
container.removeEventListener("TabClose", exampleTabRemoved, false);

This should get you 99% of the way there.

Jonathan Fingland
Thanks Jonathan.The function that you wrote seem to be right, but the problem is that when i load a pdf, the DOMContentLoaded event doesn't fire, so neither the rest of code..The best solution should be add an eventListener to some element of browser that control the URL, but i have no idea how
Manuel Bitto
the DOMContentLoaded event only fires on mime types that have a DOM (e.g. HTML, or XUL). Do you specifically want to listen for a PDF being loaded? from a specific domain? You might want to look at https://developer.mozilla.org/en/Observer_Notifications and specifically `http-on-examine-response`. And also see http://www.ashita.org/howto-xhr-listening-by-a-firefox-addon/ for an example of observer notification processing
Jonathan Fingland
Thank you Jonathan, you get me on the right way. Now with http-on-examine-response i can get events from page request and check if url of the page is changed, anf of course it works also with pdf. But i have to check also the history back and forward, because they don't make http request.Really there is no other way to check just the page request event, e.g. the event that should be fired when i write an url on my firefox navbar and press enter?!
Manuel Bitto
no problem. You may want to install ChromeBug (like firebug for firefox itself) so you can more easily determine how to listen to events on the address bar. If you want to listen to requests from multiple sources (bookmarks, links, address bar, etc) then you'll need observer notifications. You may want to look into on-modify-request as that gets fired when the request goes out (unlike on-examine-response)
Jonathan Fingland