Jiacai Liu
Jiacai Liu

Reputation: 2743

redirect a request with firefox addon

Currently I use addon sdk to develop a firefox extension,which can redirect some request without loading them.

For example, a page refer a.js (maybe have something like this <script src='example.com/a.js'>), then, I want to redirect this request to example2.com/b.js

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

But I don't know how to load example2.com/b.js at the time when a.js is blocked.

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);

Thanks!

Upvotes: 3

Views: 906

Answers (1)

erikvold
erikvold

Reputation: 16528

I have a sdk module which does this here

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

Upvotes: 1

Related Questions