Firefox插件中的OnBeforeRequest URL重定向(从Chrome扩展名转换) [英] OnBeforeRequest URL redirect in Firefox Addon (Conversion from Chrome Extension)
问题描述
我想将我的Chrome扩展程序转换为Firefox。目前为止,除了我在Chrome扩展中的 现在我不确定如何在Firefox中实现此功能。 您引用 WebExtensions文档: 请求可以是: 很好地解释了这种情况。您的选择是: 等到WebExtensions更好地支持 如果完全没有必要建立连接,取消并不重定向请求。 改为在 webRequest.onBeforeRequest
中有一个url重定向,它是。
在Chrome background.js中
$ b
chrome.webRequest.onBeforeRequest.addListener(
function(details){
console.log('onBeforeRequest');
var returnuri;
returnuri = details.url;
if((details.url.indexOf(/ malicious / )> -1)||(details.url.indexOf(/ bad /)> -1)){
//我想重定向到安全内容
returnuri = details。 url +(/\&tag=/.test(details.url)?:'/ safe /');
} else {
returnuri = details.url;
}
return {redirectUrl:returnuri};
},
{
网址:[
*://malicious.com/*
] ,
类型:[main_frame]
},
[blocking]
);
onBeforeRequest
中取消
onBeforeSendHeaders
< onBeforeRequest
或 onHeadersReceived $ $> c $ c>,但允许在
onBeforeSendHeaders
中。
webRequest
。
onBeforeSendHeaders
中重定向。考虑到你只检查URL并且在那个事件中可用,它应该没有什么不同,除了在重定向之前TCP连接可能已经建立。
<请注意,相同的代码在Chrome中不起作用 - 它不会在此请求中提供重定向。
正如您所提到的,与Chrome不同,重定向到相同的URL会产生一个循环(因为此时请求需要从头开始重新制作)。
通常,如果您需要检查可用的东西在稍后的事件中,可以通过保存它的中的相同请求标识。不幸的是,文档声明不支持 requestId
和稍后的重定向在 onBeforeRequest
中标记请求 onBeforeSendHeaders
。 requestId
。 / p>
I want to convert a Chrome extension of mine to Firefox. So far so good, except I had an url redirect in webRequest.onBeforeRequest
in the Chrome extension, which is not allowed in Firefox WebExtensions.
Now I am unsure how to implement this in Firefox.
In the Chrome background.js
it looked something like this:
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
console.log('onBeforeRequest');
var returnuri;
returnuri = details.url;
if ((details.url.indexOf("/malicious/") > -1) || (details.url.indexOf("/bad/") > -1)){
//I want to redirect to safe content
returnuri = details.url + (/\&tag=/.test(details.url) ? "" : '/safe/');
}else{
returnuri = details.url;
}
return {redirectUrl: returnuri};
},
{
urls: [
"*://malicious.com/*"
],
types: ["main_frame"]
},
["blocking"]
);
You cite the WebExtensions docs:
Requests can be:
- canceled only in
onBeforeRequest
- modified/redirected only in
onBeforeSendHeaders
...
Redirection is not allowed in
onBeforeRequest
oronHeadersReceived
, but is allowed inonBeforeSendHeaders
.
Well, that explains the situation pretty well. Your options are:
Wait until WebExtensions achieve better support for
webRequest
.Cancel, and not redirect, the request if it's absolutely imperative that no connection is made at all.
Redirect in
onBeforeSendHeaders
instead. Considering that you're only checking the URL and that is available in that event, it shouldn't make a difference except that the TCP connection may be already established before you redirect.Note that the same code wouldn't work in Chrome - it doesn't expect a redirect in this request.
As mentioned by you, unlike Chrome, redirecting to the same URL would produce a loop (because at this point the request needs to be remade from scratch).
In general, if you need to inspect something available in an earlier event and act on it later, you can flag the request inUnfortunately, the docs state thatonBeforeRequest
by saving itsrequestId
and later redirecting the same request ID inonBeforeSendHeaders
.requestId
is not supported either.
这篇关于Firefox插件中的OnBeforeRequest URL重定向(从Chrome扩展名转换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!