Redirect request with firefox addon

I am currently using an addon sdk to develop a firefox extension that can redirect some requests without downloading them.

For example, the page links to a.js (maybe there is something like this <script src='example.com/a.js'>

), then I want to redirect this request to example2.com/b.js

Now, nsIContentPolicy I can block example.com/a.js,

But I don't know how to load example2.com/b.js at the moment of blocking a.js.

my codes now look like this:

const { Cc, Ci } = require("chrome");
const xpcom = require("sdk/platform/xpcom");
const { Class } = require("sdk/core/heritage");

exports.gcleaner = Class({
  extends: xpcom.Unknown,
  interfaces: ["nsIContentPolicy"],
  shouldLoad: function (contType, contLoc, reqOrig, ctx, typeGuess, extra) {
  //https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIContentPolicy#shouldLoad()   
    if (contType === 2)  //2 means js 
    {   
        var blockReg = new RegExp('example.com/a.js');
        if (contLoc['spec'].match(blockReg)) 
        {
            return Ci.nsIContentPolicy.REJECT;
        };

    };  
    return Ci.nsIContentPolicy.ACCEPT;
  },  

  shouldProcess: function (contType, contLoc, reqOrig, ctx, mimeType, extra) {
    return Ci.nsIContentPolicy.ACCEPT;
  }
});

let factory = xpcom.Factory({
  Component:   exports.gcleaner,
  description: "hello world",
  contract:    "@liujiacai.net/gcleaner"
});

var catman = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
catman.addCategoryEntry("content-policy", "speeding.gcleaner", factory.contract, false, true);

      

Thank!

+3


source to share


1 answer


I have an sdk module that does it here



https://github.com/jetpack-labs/pathfinder/blob/master/lib/redirect.js

+1


source







All Articles