【问题标题】:How to convert Chrome extension to firefox - url blocking/redirecting如何将 Chrome 扩展程序转换为 firefox - url 阻止/重定向
【发布时间】:2014-11-15 18:28:21
【问题描述】:

我有一个问题,我做了一个 chrome 扩展和 opéra,但对于 Firefox,它根本不起作用,我的扩展只是阻止了我添加到我的 background.js 的 URL 列表。 有人可以帮我将 chrome 转换为 firefox 吗?

更多细节: 我试图将我的 chrome 扩展程序转换为 fireox,我的 chrome 扩展程序阻止了 background.js 中的 url 列表,我真的坚持使用 firefox,我试图在 firefox 下使用我的扩展程序的 chrome 版本,它加载图标,manifest.json的信息,但是background.js不起作用,url没有被屏蔽

这里我先给你我的manifest.json代码:

{
"name":"blocker",
"description":"block bad urls",
"version":"1.0",
"manifest_version":2,
"permissions": [
        "http://*/*",
        "https://*/*",
        "webRequest",
        "webRequestBlocking",
       "<all_urls>" ,    
        "unlimitedStorage"
]      
,"icons":{"48":"icon.png"},
"background":{
    "scripts": ["background.js"]
},
"browser_action":{
    "default_popup":"popup.html",
    "default_icon":"icon.png"},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["disablef12.js"]
}
]
}

这是我的background.js

var AUTHORIZED_DOMAINS= {
"www.url01.com": false,
"url01.com": false,
"www.url02.com": false,
 etc..
};
function extract_domain(url)    {
    var matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
    return matches[1];
}

chrome.webRequest.onBeforeRequest.addListener(function(details) { 
var domain = extract_domain(details.url);
return { cancel: AUTHORIZED_DOMAINS[domain ]===false }; 
}, {urls: ["<all_urls>"]},["blocking"]);

再次感谢您的宝贵时间和帮助

【问题讨论】:

    标签: google-chrome firefox-addon


    【解决方案1】:

    屏蔽网址:

    如何阻止 url,引导方法的完整工作示例:

    https://github.com/Noitidart/PortableTester/tree/block-urls

    下载xpi并将其拖到firefox上,它会阻止google和bbc。

    const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
    Cu.import('resource://gre/modules/Services.jsm');
    
    var urls_block = [ //if urls ontain any of these elements they will be blocked or redirected, your choice based on code in observer line 17
     'www.google.com',
     'www.bbc.com'
    ];
    
    var observers = {
        'http-on-modify-request': {
            observe: function (aSubject, aTopic, aData) {
                console.info('http-on-modify-request: aSubject = ' + aSubject + ' | aTopic = ' + aTopic + ' | aData = ' + aData);
                var httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);
                var requestUrl = httpChannel.URI.spec;
                for (var i=0; i<urls_block.length; i++) {
                 if (requestUrl.indexOf(urls_block[i]) > -1) {
                  //httpChannel.cancel(Cr.NS_BINDING_ABORTED); //this aborts the load
                  httpChannel.redirectTo(Services.io.newURI('data:text,url_blocked', null, null)); //can redirect with this line, if dont want to redirect and just block, then uncomment this line and comment out line above (line 17)
                 }
                }
            },
            reg: function () {
                Services.obs.addObserver(observers['http-on-modify-request'], 'http-on-modify-request', false);
            },
            unreg: function () {
                Services.obs.removeObserver(observers['http-on-modify-request'], 'http-on-modify-request');
            }
        }
    };
    
    function install() {}
    
    function uninstall() {}
    
    function startup() {
     for (var o in observers) {
        observers[o].reg();
     }
    }
    
    function shutdown(aData, aReason) {
     if (aReason == APP_SHUTDOWN) return;
    
     for (var o in observers) {
        observers[o].unreg();
     }
    }
    

    如果你想要一个图标,只需添加到 xpi 文件,它只是一个重命名为 xpi 的 zip 文件。添加一个名为 icon.png 的文件

    在此处查看此基本引导模板:https://gist.github.com/Noitidart/9025999

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 2014-03-06
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      相关资源
      最近更新 更多