我有一个问题,我做了一个chrome扩展和opéra,但对于Firefox,它根本没用,我的扩展只是阻止我添加到我的background.js的网址列表。 有人可以帮我修改chrome转换为firefox吗?
更多细节: 我试图将我的chrome扩展转换为fireox,我的chrome扩展阻止了一个在background.js内的网址列表,我真的坚持使用firefox,我试图在firefox下使用我的扩展的chrome版本,它加载图标,manifest.json的信息,但background.js不起作用,网址没有被阻止
在这里,我先给你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"]);
再次感谢你的时间和帮助
答案 0 :(得分:1)
阻止网址:
如何阻止URL的完整工作示例,bootstrap方法:
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