如何在更改原点的 chrome-extension 中使用 fetch?

When I use fetch,it gets the request origin like this: Origin: chrome-extension://hhchkohknefpngiknmlkelgmnhokjnef

I want the request origin like this: Origin: www.xxxxxx.com (the now website page url)

解决方案

You can do this using the onBeforeSendHeaders event.

chrome.webRequest.onBeforeSendHeaders.addListener(
    function(details) {
        for (var i = 0; i < details.requestHeaders.length; ++i) {
            if (details.requestHeaders[i].name === 'Origin')
                details.requestHeaders[i].value = 'https://www.xxxxxx.com';
        }

        return {
            requestHeaders: details.requestHeaders
        };
    }, {
        urls: ["*://www.xxxxxx.com/*"]
    },
    ["blocking", "requestHeaders", "extraHeaders"]);

extraHeaders option is required for changing changing Origin starting chrome 79.

You will need the following permissions in your manifest file:

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

Reference: https://developer.chrome.com/extensions/webRequest#implementation

相关文章