Jez
Jez

Reputation: 30071

Prevent JavaScript redirects in Firefox

I want to prevent JavaScript redirects in Firefox for one domain (youtube.com), and I was wondering whether there's a plugin that will do it. I'm trying to use NoScript, and I'd like to allow scripts globally because I don't want to disable most JavaScript, but this seems to just allow JavaScript redirects. Is there a way for me to just disable JavaScript redirects (or ideally, display a prompt)?

The only other way I can think of doing it is to write my own extension that messes around with window.onbeforeunload and window.unload, but ideally I'd like to use an existing addon.

Upvotes: 3

Views: 3943

Answers (1)

Noitidart
Noitidart

Reputation: 37328

var {utils: Cu, classes: Cc, instances: Ci, results: Cr} = Components
Cu.import('resource://gre/modules/Services.jsm');
var myobserve = function(aSubject, aTopic, aData) {
    var httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);


    if (httpChannel.loadFlags & Ci.nsIHttpChannel.LOAD_REPLACE) {
            //its a redirect, lets block it
            httpChannel.cancel(Cr.NS_BINDING_ABORTED);
    }
}

I use the presence of flags to test for redirect. Here are some notes i took on flags awhile back, im not totally sure of how accurate these notes are:

  • if has LOAD_DOCUMENT_URI it usually also has LOAD_INITIAL_DOCUMENT_URI if its top window, but on view source we just see LOAD_DOCUMENT_URI. If frame we just see LOAD_DOCUMENT_URI. js css files etc (i think just some imgs fire http modify, not sure, maybe all but not if cached) come in with LOAD_CLASSIFY_URI or no flags (0)
  • note however, that if there is a redirect, you will get the LOAD_DOC_URI and the LOAD_INIT_DOC_URI on initial and then on consequent redirects until final redirect. All redirects have LOAD_REPLACE flag tho
  • note: i think all imgs throw http-on-modify but cahced images dont. images can also have LOAD_REPLACE flag

Of course to start observing:

Services.obs.addObserver(myobserve, 'http-on-modify-request', false);

and to stop:

Services.obs.removeObserver(myobserve, 'http-on-modify-request', false);

Upvotes: 1

Related Questions