Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect tab URL change inside a Firefox add-on

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?

like image 497
tiagosilva Avatar asked May 26 '15 17:05

tiagosilva


People also ask

How to view changes in url in Firefox?

In Firefox 87 and earlier, "url" changes can be observed by filtering by "status". Integer. Fire this event only for the tab identified by this ID. Integer. Fire this event only for tabs which are currently in the window identified by this ID. Lists the changes to the state of the tab that was updated.

What happens when a tab is updated?

Fired when a tab is updated. When the user navigates to a new URL in a tab, this will typically generate several onUpdated events as various properties of the tabs.Tab object are updated. This includes the url, but also potentially the title and favIconUrl properties. The status property will cycle through "loading" and "complete".

What happens when the user navigates to a new URL?

When the user navigates to a new URL in a tab, this will typically generate several onUpdated events as various properties of the tabs.Tab object are updated. This includes the url, but also potentially the title and favIconUrl properties. The status property will cycle through "loading" and "complete".


1 Answers

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

like image 59
Basilevs Avatar answered Oct 21 '22 13:10

Basilevs