Detect tab URL change inside a Firefox add-on

我是研究僧i 提交于 2020-01-10 03:49:06

问题


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 page load or navigation. Some sites do this (e.g. when you click to view another video on YouTube).

On Chrome, I accomplished this with:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if (changeInfo && changeInfo.status == "complete") {
        //do stuff here
    }
});

How do I detect such changes in a Firefox add-on?

I've been told to use: Listening to events on all tabs, but I couldn't put it together. One of the problems was that gBrowser was not defined in the extension.

What am I doing wrong?

Is there a simpler way?


回答1:


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




回答2:


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.




回答3:


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);


来源:https://stackoverflow.com/questions/30464941/detect-tab-url-change-inside-a-firefox-add-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!