3

I'd like to get URL change notification (not load ones). There is an API, but I don't understand how to use in context of Addon SDK, which treats tabs in its own way.

  1. How to install a listener to an SDK tab?
  2. How to convert nsiDOMWindow from a progress object to SDK tab?

Related:

Community
  • 1
  • 1
Basilevs
  • 22,440
  • 15
  • 57
  • 102

1 Answers1

2

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

Inspired by Converting to chrome windows

Nils Guillermin
  • 1,867
  • 3
  • 21
  • 51
Basilevs
  • 22,440
  • 15
  • 57
  • 102
  • [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIWebProgress.addProgressListener]" – mate64 Dec 03 '15 at 00:56
  • I'm trying to get the tab object from within the location change function, to attach content scripts and workers to the listeners. Would you know how to it? – Warren Mar 14 '16 at 04:04
  • highLevel variable in example contains tab object. – Basilevs Mar 18 '16 at 16:18