Detect tab URL change inside a Firefox add-on

前端 未结 3 1089
耶瑟儿~
耶瑟儿~ 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条回答
  • 2020-12-19 04:07

    If you're using the add-on SDK, you're looking at the wrong docs. Here are the tab docs.

    As stated there, you create a listener like so:

    var tabs = require("sdk/tabs");
    
    // Listen for tab openings.
    tabs.on('open', function onOpen(tab) {
      myOpenTabs.push(tab);
    });
    
    // Listen for tab content loads.
    tabs.on('ready', function(tab) {
      console.log('tab is loaded', tab.title, tab.url);
    });
    

    All the docs you look at should be a subset of developer.mozilla.org/en-US/Add-ons/SDK.

    0 讨论(0)
  • 2020-12-19 04:11

    I find that the activate and pageshow events, between the two of them, cover all changes in URL that I can conjure up between switching tabs, opening pages in a new tab, closing tabs, refreshing pages, and typing in new URL's.

    var updateURL = function (tab) {
     var oldURL = url;
     var url = tab.url;
     console.log(url);
    };
    
    tabs.on("activate", updateURL);
    tabs.on("pageshow", updateURL);
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题