Javid
Javid

Reputation: 2935

How to handle every request in a Firefox extension?

I'm trying to capture and handle every single request a web page, or a plugin in it is about to make. For example, if you open the console, and enable Net logging, when a HTTP request is about to be sent, console shows it there. I want to capture every link and call my function even when a video is loaded by flash player (which is logged in console also, if it is http). Can anyone guide me what I should do, or where I should get started?

Edit: I want to be able to cancel the request and handle it my way if needed.

Upvotes: 2

Views: 1537

Answers (2)

Noitidart
Noitidart

Reputation: 37238

non sdk version and with much much more control and detail: this allows you too look at the flags so you can only watch LOAD_DOCUMENT_URI which is frames and main window. main window is always LOAD_INITIAL_DOCUMENT_URI

https://github.com/Noitidart/demo-on-http-examine

https://github.com/Noitidart/demo-nsITraceableChannel - in this one you can see the source before it is parsed by the browser

in these examples you see how to get the contentWindow and browserWindow from the subject as well, you can apply this to sdk example, just use the "subject"

also i prefer to use http-on-examine-response, even in sdk version. because otherwise you will see all the pages it redirects FROM, not the final redirect TO. say a url blah.com redirects you to blah.com/1 and then blah.com/2

only blah.com/2 has a document, so on modify you see blah.com and blah.com/1, they will have flags LOAD_REPLACE, typically they redirect right away so the document never shows, if it is a timed redirect you will see the document and will also see LOAD_INITIAL_DOCUMENT_URI flag, im guessing i havent experienced it myself

Upvotes: 2

jsantell
jsantell

Reputation: 1268

You can use the Jetpack SDK to get most of what you need, I believe. If you register to system events and listen for http-on-modify-request, you can use the nsIHttpChannel methods to modify the response and request

let { Ci } = require('chrome');
let { on } = require('sdk/system/events');
let { newURI } = require('sdk/url/utils');

on('http-on-modify-request', function ({subject, type, data}) {
  if (/google/.test(subject.URI.spec)) {
    subject.QueryInterface(Ci.nsIHttpChannel);
    subject.redirectTo(newURI('http://mozilla.org'));
  }
});

Additional info, "Intercepting Page Loads"

Upvotes: 10

Related Questions