firefoxfirefox-addonfirefox-addon-sdk

Notification when the value in Address Bar changes


How to get notified whenever the URL in the address bar (also known as location bar) changes. Using the following code, I tried to get notified when user navigates to another page (by clicking a link, using the back/forward button, by typing an address in the Location Bar, etc.) and also when user switches tabs.

var myExtension = {
oldURL: null,


init: function() {
    gBrowser.addProgressListener(this);
},

uninit: function() {
    gBrowser.removeProgressListener(this);
},

processNewURL: function(aURI) {
    if (aURI.spec == this.oldURL) return;

    // now we know the url is new...
    alert(aURI.spec);
    this.oldURL = aURI.spec;
},

// nsIWebProgressListener
QueryInterface: XPCOMUtils.generateQI(["nsIWebProgressListener",
                                       "nsISupportsWeakReference"]),

onLocationChange: function(aProgress, aRequest, aURI) {
    this.processNewURL(aURI);
},

onStateChange: function() {},
onProgressChange: function() {},
onStatusChange: function() {},
onSecurityChange: function() {}
};


};


window.addEventListener("load", function() { myExtension.init() }, false);
window.addEventListener("unload", function() { myExtension.uninit() }, false);

but this code is getting reference error for XPCOMUtils and window.I m developing this code on firefox Add-on Builder. Is there anything i need to import???


Solution

  • hey i got the answer of my question but with click event i need it work on page load event i tried document.addEventListener() but it didn't work

    // This is an active module of the binitksingh (1) Add-on
    
        const contextMenu = require("context-menu"),
        data = require("self").data,
        notify = require("simple-notify").notify,
        tabs = require("tabs"),
        widgets = require("widget");
    
    
        exports.main = function(options, callbacks) {
       // Add a one-click option to addons bar.
        var widget = widgets.Widget({
        id: "Show-url",
        label: "Show URL",
        contentURL: "http://www.example.com/favicon.ico",
    
    
        onClick: function() {
            var tabworker = tabs.activeTab.attach({
                contentScriptFile: data.url('js/find-url.js'),
                onMessage: processUrl
            });
            tabworker.port.emit('click');
        },
    });
    
    
     }
    
    
      function processUrl(url) {
        if (url) {
            notify("Hello now u r on:\n" + url);
                 } else {
                    notify('ZOMG, error finding URL.');
                        }
             }