使用firefox插件重定向请求

时间:2014-09-17 16:36:20

标签: firefox firefox-addon firefox-addon-sdk

目前我使用addon sdk开发一个firefox扩展,它可以在不加载的情况下重定向某些请求。

例如,一个页面引用a.js(可能有这样的<script src='example.com/a.js'>), 那么,我想将此请求重定向到example2.com/b.js

现在,nsIContentPolicy我可以阻止example.com/a.js,

但我不知道如何在阻止a.js时加载example2.com/b.js.

我的代码现在看起来像这样:

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

谢谢!

1 个答案:

答案 0 :(得分:1)