tommed
tommed

Reputation: 1561

Firefox for Android Extension: How to trigger event on every page load?

I am trying to write a Firefox extension for Android that will fire an event every time the web page changes. It is monitoring which URLs are being loaded (all URLs) and the contents of the page loaded (via DOM inspection). My problem is that the window load event using the code below only gets loaded when a tab is opened, if you navigate away from the page, no events get fired.

How do I hook into every page load event for any URL?

This code is the entire contents of bootstrap.js:

Components.utils.import("resource://gre/modules/Services.jsm");
var windowListener = {
  onOpenWindow: function(aWindow) {
    console.log('vipro.openWindow');
    let domWindow = aWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIDOMWindowInternal || Components.interfaces.nsIDOMWindow);
    domWindow.addEventListener("UIReady", function onLoad() {
      domWindow.removeEventListener("UIReady", onLoad, false);
      console.log('vipro.openWindow.loaded');
      // ** ONLY EVER FIRED ONCE ** //
      try {
        var browser = Services.wm.getMostRecentWindow("navigator:browser");
      } catch(e) {
        console.log('vipro.openWindow.error.' + e.toString());
      }
      console.log('vipro.openWindow.loaded.DONE');
    });
    console.log('vipro.openWindow.DONE');
  },
  onCloseWindow: function(aWindow) {},
  onWindowTitleChange: function(aWindow, aTitle) {},
};

function startup(data, reason) {
  console.log('vipro.startup');
  try {
    let wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
    wm.addListener(windowListener);
  } catch(e) {
     console.log('vipro.startup.error.' + e.toString());
  }
  console.log('vipro.startup.DONE');
}

function shutdown() {
  console.log('vipro.shutdown');
}

function install(aData, aReason) {}
function uninstall(aData, aReason) {}

I'm not fussed that the code above only works for new tabs and not existing (I've intentionally kept it simple), it's just the fact that when I navigate away from the initial new tab page, I don't get the opportunity to hook into the other pages.

Upvotes: 1

Views: 167

Answers (1)

tommed
tommed

Reputation: 1561

Found a slightly different approach on this page:

https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Mobile_development

Using the layout that cfx provides and not using the bootstrap... bootstrap i am able to achieve what i need using page-mod.

Upvotes: 1

Related Questions