如何在Firefox页面模式中重定向? [英] How to make a redirect in Firefox page-mod?
问题描述
我试图将我的Google Chrome浏览器扩展程序移植到Firefox附加组件SDK,并且需要扩展程序来从我的网站过滤页面并进行重定向。例如,如果用户打开 http://example.com/special 我需要将他发送到 http://example.com/redirect 同样的浏览器选项卡。
这是我试图做到这一点的方式:
<$ c PageMod({
include:*,
contentScriptWhen:start,
contentScript:,
onAttach:function(worker){
if(worker.tab.url == worker.url&&
worker.url.indexOf(example.com/special) > -1){
worker.tab.url =http://example.com/redirect;
}
}
});
问题是:我的浏览器在重定向后有时会挂起(刚刚在新页面显示标签)。我在做什么错了?
使用Firefox 16.0.2,附加SDK 1.11
最好的办法是在较低层次上完成:
const {Cc,Ci,Cr} = require(chrome);
var events = require(sdk / system / events);
var utils = require(sdk / window / utils);
函数监听器(事件){
var channel = event.subject.QueryInterface(Ci.nsIHttpChannel);
var url = event.subject.URI.spec;
//在这里你应该评估网址,并决定是否重定向或不。
//注意shouldIredirect和newUrl是你必须替换的猜测对象!
if(shouldIredirect){
//如果你想重定向到另一个url,你必须中止当前的请求
//见https://developer.mozilla.org/en- US / docs / XUL / School_tutorial / Intercepting_Page_Loads
channel.cancel(Cr.NS_BINDING_ABORTED);
//获取当前的gbrowser对象(因为用户可能有多个窗口和选项卡)并加载固定的URI
var gBrowser = utils.getMostRecentBrowserWindow()。gBrowser;
var domWin = channel.notificationCallbacks.getInterface(Ci.nsIDOMWindow);
var browser = gBrowser.getBrowserForDocument(domWin.top.document);
browser.loadURI(newUrl);
} else {
//什么也不做,让Firefox继续正常流程
}
};
};
exports.main = function(){
events.on(http-on-modify-request,listener);
};
如果您想在actiton中查看此代码,请查看这个插件(免责声明:这是一个插件我开发)。
I'm trying to port my Google Chrome extension to Firefox Add-On SDK and I need the extension to filter pages from my website and make redirects. For example, if the user opens "http://example.com/special" I need to send him to "http://example.com/redirect" in the same browser tab.
This is how I tried to do this:
var pageMod = require("page-mod").PageMod({
include: "*",
contentScriptWhen: "start",
contentScript: "",
onAttach: function(worker) {
if (worker.tab.url == worker.url &&
worker.url.indexOf("example.com/special") > -1) {
worker.tab.url = "http://example.com/redirect";
}
}
});
Problem is: my browser hangs sometimes after the redirect (immediately after the new page has been displayed in a tab). What am I doing wrong?
Using Firefox 16.0.2, Add-On SDK 1.11
The best way will be to do it at the lower level:
const { Cc, Ci, Cr } = require("chrome");
var events = require("sdk/system/events");
var utils = require("sdk/window/utils");
function listener(event) {
var channel = event.subject.QueryInterface(Ci.nsIHttpChannel);
var url = event.subject.URI.spec;
// Here you should evaluate the url and decide if make a redirect or not.
// Notice that "shouldIredirect" and "newUrl" are guessed objects you must replace!
if (shouldIredirect) {
// If you want to redirect to another url, the you have to abort current request
// See https://developer.mozilla.org/en-US/docs/XUL/School_tutorial/Intercepting_Page_Loads
channel.cancel(Cr.NS_BINDING_ABORTED);
// Aet the current gbrowser object (since the user may have several windows and tabs) and load the fixed URI
var gBrowser = utils.getMostRecentBrowserWindow().gBrowser;
var domWin = channel.notificationCallbacks.getInterface(Ci.nsIDOMWindow);
var browser = gBrowser.getBrowserForDocument(domWin.top.document);
browser.loadURI(newUrl);
} else {
// do nothing, let Firefox keep going on the normal flow
}
};
};
exports.main = function() {
events.on("http-on-modify-request", listener);
};
If you want to see this code in actiton, take a look at this addon (DISCLAIMER: it's an addon I developed).
这篇关于如何在Firefox页面模式中重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!