简体   繁体   English

chrome.webRequest.onBeforeRedirect不停止重定向?

[英]chrome.webRequest.onBeforeRedirect not stopping redirect?

I am developing a chrome extension, and in part of the extension I need to detect redirection and ask the user whether he/she wishes to proceed. 我正在开发chrome扩展程序,在扩展程序的一部分中,我需要检测重定向并询问用户他/她是否希望继续。 I tried doing this using chrome's webRequest API, but it doesn't seem to work properly, even though I'm sure my syntax is right. 我尝试使用chrome的webRequest API进行此操作,但是即使我确定语法正确,它也似乎无法正常工作。 Here is the relevant code: 以下是相关代码:

chrome.webRequest.onBeforeRedirect.addListener(function (details) {
    if(details.frameId == 0){
        if (confirm("This link is a redirect. Do you want to continue?")){
            /* do nothing and let them redirect */
        }
        else {
            /* stop the redirect from happening */
            return {cancel: true};
        }
    }
}, {urls: ["<all_urls>"], types: ["main_frame"]}, ["blocking"]);

Why isn't the method blocking properly? 为什么方法不能正确阻塞?

According to official guide , you're not allowed to cancel the redirect 根据官方指南 ,您不能取消重定向

onBeforeRedirect onBeforeRedirect

Fires when a redirect is about to be executed. 当将要执行重定向时触发。 A redirection can be triggered by an HTTP response code or by an extension. 重定向可以由HTTP响应代码或扩展名触发。 This event is informational and handled asynchronously. 此事件是信息性的,并且是异步处理的。 It does not allow you to modify or cancel the request. 它不允许您修改或取消请求。

Haibara is perfectly correct in that onBeforeRedirect is purely informational, but let me add to that. Haibara是完全正确的,因为onBeforeRedirect纯粹是信息性的,但让我补充一下。

onBeforeRequest and onBeforeSendHeaders are the only events that can - ie cancelling is only possible before the request is actually made. onBeforeRequestonBeforeSendHeaders是唯一可以进行的事件-即只有在实际发出请求之前才可以取消 It's a question of defining "cancel", since the request is already out. 这是定义“取消”的问题,因为请求已经发出。


Note that you can use onHeadersReceived for your purpose - headers provided in that event are the ones used to redirect the request, and you can override it by redirecting (which is allowed at that point) to your own extension page. 请注意,您可以将onHeadersReceived用于您的目的-该事件中提供的标头是用于重定向请求的标头,并且可以通过重定向(此时允许)到自己的扩展页来覆盖它。


However, I must also say that you may be chasing the wrong suspect here. 但是,我还必须说,您可能在这里追逐了错误的嫌疑人。 As far as I understand webRequest , this will only catch HTTP 3** redirects ; 据我了解webRequest ,这只会捕获HTTP 3 **重定向 any other redirect method, such as changing window.location , will mean that this particular network request succeeds, and then generates a subsequent navigation, which is transparent to webRequest . 任何其他重定向方法(例如更改window.location )都将意味着此特定网络请求成功,然后生成后续导航,这对于webRequest是透明的。

All the while the HTTP 3** redirects are most often used for benign purposes of rewriting http to https or modifying the URL to a canonical one. 一直以来,HTTP 3 **重定向通常用于将http重写为https或将URL修改为规范URL的良性目的。

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

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