Firefox插件中的OnBeforeRequest URL重定向(从Chrome扩展名转换) [英] OnBeforeRequest URL redirect in Firefox Addon (Conversion from Chrome Extension)

本文介绍了Firefox插件中的OnBeforeRequest URL重定向(从Chrome扩展名转换)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!


我想将我的Chrome扩展程序转换为Firefox。目前为止,除了我在Chrome扩展中的 webRequest.onBeforeRequest 中有一个url重定向,它是


在Chrome background.js中
$ b


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};
] ,

您引用 WebExtensions文档


  • 仅在 onBeforeRequest 中取消

  • 修改/重定向仅限于 onBeforeSendHeaders

< onBeforeRequest onHeadersReceived c $ c>,但允许在 onBeforeSendHeaders 中。


  1. 等到WebExtensions更好地支持 webRequest

  2. 如果完全没有必要建立连接,取消并不重定向请求。

  3. 改为在 onBeforeSendHeaders 中重定向。考虑到你只检查URL并且在那个事件中可用,它应该没有什么不同,除了在重定向之前TCP连接可能已经建立。

    <请注意,相同的代码在Chrome中不起作用 - 它不会在此请求中提供重定向。


    通常,如果您需要检查可用的东西在稍后的事件中,可以通过保存它的 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:

  function(details) {

    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/');
      returnuri = details.url;
    return {redirectUrl: returnuri};
    urls: [
    types: ["main_frame"]


You cite the WebExtensions docs:

Requests can be:

  • canceled only in onBeforeRequest
  • modified/redirected only in onBeforeSendHeaders


Redirection is not allowed in onBeforeRequest or onHeadersReceived, but is allowed in onBeforeSendHeaders.

Well, that explains the situation pretty well. Your options are:

  1. Wait until WebExtensions achieve better support for webRequest.

  2. Cancel, and not redirect, the request if it's absolutely imperative that no connection is made at all.

  3. 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 in onBeforeRequest by saving its requestId and later redirecting the same request ID in onBeforeSendHeaders. Unfortunately, the docs state that requestId is not supported either.

这篇关于Firefox插件中的OnBeforeRequest URL重定向(从Chrome扩展名转换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
发送“验证码”获取 | 15天全站免登陆