Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

javascript - How can I check if url of current browser tab is changed?

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 doesn't work when page loads PDF.

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...