简体   繁体   English

Firefox Addon中的OnBeforeRequest URL重定向(从Chrome扩展转换)

[英]OnBeforeRequest URL redirect in Firefox Addon (Conversion from Chrome Extension)

I want to convert a Chrome extension of mine to Firefox. 我想将我的Chrome扩展程序转换为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 . 到目前为止一切都那么好,除了我在Chrome扩展程序中的webRequest.onBeforeRequest中有一个url重定向,这在Firefox WebExtensions中不允许的

Now I am unsure how to implement this in Firefox. 现在我不确定如何在Firefox中实现它。
In the Chrome background.js it looked something like this: 在Chrome background.js它看起来像这样:

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 : 您引用了WebExtensions文档

Requests can be: 请求可以是:

  • canceled only in onBeforeRequest 仅在onBeforeRequest取消
  • modified/redirected only in onBeforeSendHeaders 仅在onBeforeSendHeaders修改/重定向

... ...

Redirection is not allowed in onBeforeRequest or onHeadersReceived , but is allowed in onBeforeSendHeaders . onBeforeRequestonHeadersReceived不允许重定向,但onHeadersReceived中允许onBeforeSendHeaders

Well, that explains the situation pretty well. 嗯,这很好地解释了情况。 Your options are: 你的选择是:

  1. Wait until WebExtensions achieve better support for webRequest . 等到WebExtensions获得对webRequest更好支持。

EDIT (Dec 2018) : This is indeed now possible. 编辑(2018年12月) :现在确实可以。 Citing the documentation : 引用文档

On some of these events, you can modify the request. 在其中一些事件中,您可以修改请求。 Specifically, you can: 具体来说,您可以:

  • cancel the request in: 取消请求:
    • onBeforeRequest
    • onBeforeSendHeaders
    • onAuthRequired
  • redirect the request in: 将请求重定向到:
    • onBeforeRequest
    • onHeadersReceived

[...] [...]

  1. Cancel, and not redirect, the request if it's absolutely imperative that no connection is made at all. 取消,而不是重定向请求,如果绝对必须根本没有连接。

  2. Redirect in onBeforeSendHeaders instead. 改为在onBeforeSendHeaders重定向。 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. 考虑到您只检查URL以及该事件中可用的URL,除了在重定向之前可能已经建立TCP连接之外,它应该没有区别。

    Note that the same code wouldn't work in Chrome - it doesn't expect a redirect in this request. 请注意,相同的代码在Chrome中不起作用 - 它不希望在此请求中进行重定向。

    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). 正如您所提到的,与Chrome不同,重定向到相同的URL会产生循环(因为此时需要从头开始重新创建请求)。

    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 . 在一般情况下,如果你需要检查可用的东西在前面的事件,后来采取行动,你便可以标志请求onBeforeRequest通过保存它requestId后来重定向相同的请求ID在onBeforeSendHeaders Unfortunately, the docs state that requestId is not supported either. 不幸的是,文档声明也不支持requestId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM