Chrome extension not loading properly after adding redirectUrl on webRequest.onBeforeRequest

251 Views Asked by At

I am trying to develop a chrome extension using chrome.webRequest API. My extension will redirect https://google.com to https://stackoverflow.com. Functionality wise its working fine but after adding redirectUrl in chrome webRequest API my chrome extension is not loading properly.

Here is my manifest.json file:

{
    "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>

backgroundPage.js

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

If I change redirectUrl in backgroundPage.js like below(No redirect)

redirectUrl: details.url

the popup is opening properly. Like this,

before changing redirectUrl

However after changing redirectUrl like below,

redirectUrl: "https://stackoverflow.com"

the popup is not opening properly but the redirect is working fine.

after adding redirect

Am I missing something here? When will these type of behaviour occur usually? Thanks.

1

There are 1 best solutions below

1
wolf On

I also encountered this problem and solved it. You can replace the onBeforeRequest with onHeadersReceived.