3

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!

Jiacai Liu
  • 2,623
  • 2
  • 22
  • 42
  • See "Second thing" in this solution here: http://stackoverflow.com/a/25328750/1828637 – Noitidart Sep 17 '14 at 18:23
  • Hi @Noitidart, before I use your "second thing", I have to ACCEPT all request in nsIContentPolicy. If I reject a request, then no redirect happen for it don't request it at all. Then I remove nsIContentPolicy code, Well it can actually redirect a url,but the original url still load. Can you deal with this? – Jiacai Liu Sep 18 '14 at 04:01

1 Answers1

1

I have a sdk module which does this here

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

erikvold
  • 15,988
  • 11
  • 54
  • 98
  • I use similar solution to yours, redirect is ok, but original link still load, why?? my code is here:https://gist.github.com/jiacai2050/ac7b5cb4f53063f83cfd – Jiacai Liu Sep 18 '14 at 14:32
  • I just added a test that the original link is not loaded, https://github.com/jetpack-labs/pathfinder/commit/95391644bedf4e5faf6806211ff6767c40c36578 so my module certainly works – erikvold Sep 26 '14 at 18:59
  • Yes, redirection did happen, It's a bug in firebug – Jiacai Liu Oct 07 '14 at 01:13