Detect tab URL change inside a Firefox add-on

前端 未结 3 1165
耶瑟儿~
耶瑟儿~ 2020-12-19 03:31

I have an extension, functional on Chrome, that monitors the active Tab for URL changes.

Specifically, I need to detect when the URL changes, but there is no new pag

3条回答
  •  梦毁少年i
    2020-12-19 04:23

    Use ProgressListener to be notified about location changes.

    To install a listener, convert SDK tab to its raw (old) representation using viewFor. Backward conversion is possible with modelFor and 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);
    });
    

    Don't forget to remove listeners on extension unload. Tab listeners are removed automagically, but ProgressListeners won't be.

    Inspired by Converting to chrome windows

提交回复
热议问题