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

标签 javascript google-chrome google-chrome-extension webrequest

我正在开发一个 chrome 扩展程序,在扩展程序的一部分中,我需要检测重定向并询问用户他/她是否希望继续。我尝试使用 chrome 的 webRequest API 来执行此操作,但它似乎无法正常工作,尽管我确定我的语法是正确的。相关代码如下:

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"]);

为什么方法没有正确阻塞?

最佳答案

根据 official guide , 你不能取消重定向

onBeforeRedirect

Fires when a redirect is about to be executed. A redirection can be triggered by an HTTP response code or by an extension. This event is informational and handled asynchronously. It does not allow you to modify or cancel the request.

关于javascript - chrome.webRequest.onBeforeRedirect 不停止重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38270321/

相关文章:

javascript - if 语句中的一部分 JavaScript 代码运行,而同一 block 中的其余代码不执行。我在这里错过了什么?

javascript - 如何测试呈现 View 的 Express Routes

javascript - 使用 Jquery 的表单元素的占位符

javascript - 删除特定域的本地存储

google-chrome - Chrome 扩展命令(键盘快捷键)不起作用

google-chrome - Chrome 扩展程序上下文菜单在更新后不起作用

javascript - 可重用的 javascript 对象、原型(prototype)和范围

google-chrome - Chrome MediaRecorder API 共享预选标签

css - 为什么 blur(0) 不删除视网膜屏幕上 Webkit/Chrome 中的所有文本模糊?

javascript - Chrome 扩展程序 : storage listener for only one stored variable