Detecting tab URL changes in addition to Firefox

I have a Chrome functional extension that tracks the active tab for URL changes.

In particular, I need to determine when the URL changes, but there is no new page load or navigation. Some sites do this (for example, when you click to watch another video on YouTube).

In Chrome, I did this with

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

How to detect such changes in a Firefox add-in?

They told me: Listening to events on all tabs , but I could not collect them. One of the problems was that gBrowser not defined in the extension.

What am I doing wrong?

Is there an easier way?

+7
firefox firefox-addon firefox-addon-sdk
source share
3 answers

Use ProgressListener to notify you of location changes.

To set the listener, convert the SDK tab to its original (old) view using viewFor. Reverse 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); }); 

Remember to remove listeners when unloading. Listener output is automatically deleted, but there will be no ProgressListeners.

Inspired Convert To Chrome Windows

+4
source share

If you use the optional SDK, you are looking at the wrong documents. Here is the tab tab .

As indicated there, you create a listener like this:

 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 documents you are looking at should be a subset of developer.mozilla.org/en-US/Add-ons/SDK .

0
source share

I found that the activate and pageshow , between the two of them, cover all the changes in the URL that I can cause between switching tabs, opening pages in a new tab, closing tabs, updating pages and entering a new URL.

 var updateURL = function (tab) { var oldURL = url; var url = tab.url; console.log(url); }; tabs.on("activate", updateURL); tabs.on("pageshow", updateURL); 
0
source share

All Articles