在Chrome Extension服务工作者获取请求中更改了来源

发布于 2025-01-22 17:27:18 字数 896 浏览 0 评论 0原文

嗨,我正在开发社交媒体营销的Chrome扩展。我正在使用service_worker中的获取来在Facebook上发送请求,但这对我不起作用。

当我将此请求复制为网络请求的提取请求时,然后将其粘贴到Facebook页面上时,它可以很好地工作。

不同之处在于,在服务工人中发送此请求时,原始人头被覆盖,这就是为什么它对我不起作用。

我正在使用此代码来更改标题,但这也对我不起作用。

await chrome.webRequest.onBeforeSendHeaders.addListener(
 (details) => {
       let { requestHeaders } = details;
       const headerList = {};

       for (let i = 0; i < requestHeaders.length; i += 1) {
           if (requestHeaders[i].name === 'Origin') {
               requestHeaders[i].value = 'https://m.facebook.com';
               break;
           }
       }

       headerList.requestHeaders = requestHeaders;

       return headerList;
   }, { urls: ["<all_urls>"] }, ["requestHeaders", "extraHeaders"]) 

您能否详细说明我应该如何使用webrequest.onbeforesendheaders更改标题。如果我必须使用dectarativenetrequest,我应该如何使用它。

我正在使用Manivest版本3 BTW。

Hi I am developing chrome extension for social media marketing. I am using fetch in service_worker to send a request on facebook but but it is not working for me.

When I copy this request as fetch Request from network requests then paste it on facebook page, it works perfectly.

The difference is that origin-header is overridden in while sending this request in service-worker thats why it doesn't work for me.

I am using this code to change the headers but this is also not working for me.

await chrome.webRequest.onBeforeSendHeaders.addListener(
 (details) => {
       let { requestHeaders } = details;
       const headerList = {};

       for (let i = 0; i < requestHeaders.length; i += 1) {
           if (requestHeaders[i].name === 'Origin') {
               requestHeaders[i].value = 'https://m.facebook.com';
               break;
           }
       }

       headerList.requestHeaders = requestHeaders;

       return headerList;
   }, { urls: ["<all_urls>"] }, ["requestHeaders", "extraHeaders"]) 

Can you please elaborate how should I use webRequest.onbeforeSendHeaders to change headers. If I have to use declarativeNetRequest, how should I use it.

I am using Manivest Version 3 BTW.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雾里花 2025-01-29 17:27:18

我使用chrome.declarativenetrequest.updatedynamicrules覆盖标题

是代码。

await chrome.declarativeNetRequest.updateDynamicRules(
    {
        addRules: [{
            "id": 1,
            "priority": 1,
            "action": {
                type: 'modifyHeaders',
                requestHeaders: [
                    { header: 'origin', operation: 'set', value: "https://www.facebook.com" }
                ],
            },
            "condition": { "urlFilter": "facebook.com", "resourceTypes": ["xmlhttprequest"] }
        }
        ],
        removeRuleIds: [1]
    },
)

您还必须提供在清单中使用dectarativenetrequest的权限。

I used chrome.declarativeNetRequest.updateDynamicRules to override the headers

Here is the code.

await chrome.declarativeNetRequest.updateDynamicRules(
    {
        addRules: [{
            "id": 1,
            "priority": 1,
            "action": {
                type: 'modifyHeaders',
                requestHeaders: [
                    { header: 'origin', operation: 'set', value: "https://www.facebook.com" }
                ],
            },
            "condition": { "urlFilter": "facebook.com", "resourceTypes": ["xmlhttprequest"] }
        }
        ],
        removeRuleIds: [1]
    },
)

You also have to provide permissions to use declarativeNetRequest in manifest.json

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文