0
votes

I'm trying to develop a firefox extension to record all resource loading urls for each browser tab/window. I searched for hours but couldn't find a way to associate each intercepted http request to its originating tab. Here is what I have so far.

Components.classes["@mozilla.org/observer-service;1"]
  .getService(Components.interfaces.nsIObserverService)
  .addObserver({
    observe: function(aSubject, aTopic, aData) {
       if ("http-on-modify-request" == aTopic) {
         var url = aSubject
              .QueryInterface(Components.interfaces.nsIHttpChannel)
              .originalURI.spec;
         alert(url);
       }
    }
}, "http-on-modify-request", false);

I can get the url of the http request, but I don't know there is way to link it to a browser window/tab.

I read through MDN's documentation but it didn't mention it. (https://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads)

Any suggestions?

2

2 Answers

3
votes

If you wish to build your extension, not only for Firefox but also for Chrome, IE & Safari with only 1 (javascript) code, I would suggest you to use Crossrider.

You can achieve very easily what you are looking for. You can listen to all out-going requests using their onRequest API:

appAPI.onRequest(function(resourceUrl, tabUrl) {
  // Where:
  //   * resourceUrl contains the URL of the requested resource
  //   * tabUrl contains the URL of the tab requesting the resource

  // Block the loading of js scripts
  if (resourceUrl.match(/.*/) {
    // Do what ever you need with the specific resource
    // For example - save it in the extension database using appAPI.db.set()
  }
});

The goes into the background.js of the extension and will allow you to do any action you want on each one of the loaded resource of every page / tab.

1
votes

My answer to Is it possible to know the target DOMWindow for an HTTPRequest will almost get you there. You get the window associated with the request but it might be a frame in the tab. Once you have it you can get window.top - this will be the top window in the browser tab. If you need the actual browser tab element, you can use my answer in Finding the tab associated with a DOM window for that.