我的扩展程序我希望"阻止"一些网站网址并在用户尝试加入被阻止的网址时重定向到我想要的一个网址。
让我们说我想"禁止" facebook.com如果有人试图去facebook.com ... firefox addon应该将他重定向到www.google.com。
我在这里有这个代码
=============================================== ================================
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.facebook.com'
];
var redir_obj = {
'www.google.com': 'data:text,' + escape('url_blocked that would have went to google')
}
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.toLowerCase();
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(redir_obj[urls_block[i]], 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)
break;
}
}
},
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();
}
}
=============================================== ===================
但我不知道如何使其工作,或者即使它的代码正确,也可以重定向&#34; facebook&#34;到&#34; google&#34;
有人可以帮我这个,告诉我如何将它打包为firefox扩展程序???
非常感谢你的阅读时间。
答案 0 :(得分:0)