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

How can I use fetch in chrome-extension that I change the origin?(如何在更改原点的 chrome-extension 中使用 fetch?)

本文介绍了如何在更改原点的 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

这篇关于如何在更改原点的 chrome-extension 中使用 fetch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在更改原点的 chrome-extension 中使用 fetch?

基础教程推荐