简体   繁体   English

如何查看当前浏览器标签页的URL是否被更改?

[英]How can I check if url of current browser tab is changed?

I need it to use in my Firefox extension. 我需要它在我的Firefox扩展中使用。 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. 我已经尝试过window.onload事件监听器,并检查当前url ==旧url,这是一个好主意,但是在页面加载PDF时不起作用。

I saw hash changed function too but it works only with Firefox 3.6; 我也看到哈希值更改了功能,但仅适用于Firefox 3.6。 I need it to work at least with Firefox 3. 我需要它至少与Firefox 3兼容。

So, I need an event listener that check if document.location changes. 因此,我需要一个事件侦听器来检查document.location更改。

Add a Progress Listener and monitor location changes inside tab. 在选项卡中添加进度监听器并监视位置更改。 Cover tab swithches with Addon SDK. 使用Addon SDK覆盖标签。

To install a listener, convert SDK tab to its raw (old) representation using viewFor. 要安装侦听器,请使用viewFor将SDK选项卡转换为其原始(旧)表示形式。 Backward conversion is possible with modelFor and getTabForContentWindow. 使用modelFor和getTabForContentWindow可以向后转换。

const tabs = require("sdk/tabs");
const {viewFor} = require('sdk/view/core');
const {modelFor} = require('sdk/model/core');
const {getBrowserForTab, getTabForContentWindow} = require("sdk/tabs/utils");
const {Ci, Cu} = require("chrome");
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);

var progressListener = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISupportsWeakReference]),
    onLocationChange: function(aProgress, aRequest, aURI) {
        var highLevel= modelFor(getTabForContentWindow(aProgress.DOMWindow));
        console.log("onLocationChange ", highLevel.url);
    }
};

tabs.on('open', function(newTab) {
    var lowLevel = viewFor(newTab);
    var browser = getBrowserForTab(lowLevel);
    browser.addProgressListener(progressListener);
});

Inspired by Converting to chrome windows 灵感来自转换为Chrome视窗

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. 有关从不同上下文访问浏览器的信息以及其他一些有用的信息,请参见https://developer.mozilla.org/En/Code_snippets/Tabbed_browser

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). 然后,在添加(关闭时将其删除)的每个选项卡上添加“ DOMContentLoaded”事件侦听器。

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 您可能还希望看到: https : //developer.mozilla.org/En/Code_snippets/Tabbed_browser#Notification_when_a_tab_is_added_or_removedhttps://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. 这应该可以让您获得99%的收益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM