2

我正在尝试使用chrome.webRequest API 开发 chrome 扩展。我的扩展程序会将https://google.com重定向到https://stackoverflow.com。功能方面它工作正常,但在 chrome webRequest API 中添加 redirectUrl 后,我的 chrome 扩展程序无法正确加载。

这是我的manifest.json文件:

{
    "manifest_version": 2,
    "name": "Test",
    "version": "1.0",
    "description": "Test",
    "browser_action":
    {
        "default_popup": "popup.html"
    },

    "permissions": [
        "webRequest",
        "webRequestBlocking",
        "*://*.google.com/"
    ],

    "background":{
        "scripts": ["backgroundPage.js"]
    }
}

popup.html

<!DOCTYPE html>
<html>
<head><title>Hello World!</title></head>
<body>
    <h2>Hello</h2>
    <input type="text" id="name">
</body>
</html>

背景页面.js

chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        return { redirectUrl: "https://stackoverflow.com"};
    }, { urls: ["<all_urls>"] }, ["blocking"]);

如果我在 backgroundPage.js 中更改 redirectUrl,如下所示(无重定向)

重定向网址:details.url

弹出窗口正常打开。像这样,

在更改 redirectUrl 之前

但是,如下所示更改 redirectUrl 后,

重定向网址:“ https://stackoverflow.com

弹出窗口未正确打开,但重定向工作正常。

添加重定向后

我在这里错过了什么吗?这些类型的行为通常会在什么时候发生?谢谢。

4

1 回答 1

1

我也遇到了这个问题并解决了。您可以将 替换onBeforeRequestonHeadersReceived

于 2018-08-22T08:35:56.627 回答