简体   繁体   English

在 chrome 扩展中使用 fetch 发送 null origin header

[英]Using fetch in chrome extension sends null origin header

When sending cross origin requests using the fetch API the origin request header is being set to null instead of the chrome://xxxxx that I was expecting.当使用 fetch API 发送跨源请求时, origin请求 header 被设置为 null 而不是我期望的chrome://xxxxx Using fetch in both the background.js context as well as the injected iframe context result in the same behavior.在 background.js 上下文和注入的 iframe 上下文中使用 fetch 会导致相同的行为。

When I use XMLHttpRequest instead it sends the expected origin header.当我改用 XMLHttpRequest 时,它会发送预期的来源 header。

Is there some control in the manifest.json (or something else) that is preventing fetch from behaving as expected? manifest.json(或其他东西)中是否有一些控制阻止 fetch 按预期运行? My understanding is that the origin header is "protected", so trying to set it manually obviously doesn't work.我的理解是origin header 是“受保护的”,因此尝试手动设置它显然不起作用。

I noticed the same issue. 我注意到了同样的问题。 My workaround is to use XMLHttpRequest instead of fetch like what OP mentioned. 我的解决方法是使用XMLHttpRequest而不是像OP提到的那样获取。 With XHR, it does send origin as chrome://**extension-id** but not referer header. 使用XHR,它确实发送源为chrome://**extension-id**但不是referer头。 No referer header with fetch too. 没有带有fetch的 referer标头。

In case somebody wonders how to use XMLHttpRequest . 如果有人想知道如何使用XMLHttpRequest The following code snippet taken from MDN XMLHttpRequest readState document : 以下代码片段取自MDN XMLHttpRequest readState文档

var xhr = new XMLHttpRequest();
console.log('UNSENT', xhr.readyState); // readyState will be 0

xhr.open('GET', '/api', true);
console.log('OPENED', xhr.readyState); // readyState will be 1

xhr.onprogress = function () {
    console.log('LOADING', xhr.readyState); // readyState will be 3
};

xhr.onload = function () {
    console.log('DONE', xhr.readyState); // readyState will be 4
};

xhr.send(null);

If you prefer to use a promise, you can use a promise to wrap XMLHttpRequest. 如果您更喜欢使用promise,则可以使用promise来包装XMLHttpRequest。 You can check out this article on how to turn XMLHttpRequest into promise for more information. 您可以查看本文,了解如何将XMLHttpRequest转换为承诺以获取更多信息。

function xhr(params) {
  return new Promise(function(resolve, reject) {
    let xhr = new XMLHttpRequest();
    let url = '';

    xhr.open('POST', url + params);

    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.responseType = '';

    xhr.onerror = function() {
      reject(new Error('error'));
    };

    xhr.onload = function() {
      // do something with response
      let res = xhr.response;
      resolve(res);
    };

    // send request
    body = JSON.stringify({ params });
    xhr.send(body);
  })
}

There should be some way to make fetch send the Origin header when executed from the extension background worker (with manifest V3)... What about using specific referrerPolicy ?从扩展后台工作程序(使用清单 V3)执行时,应该有某种方法可以使提取发送 Origin header ...使用特定的referrerPolicy怎么样? ( https://googlechrome.github.io/samples/fetch-api/fetch-referrer-policy.html ) ( https://googlechrome.github.io/samples/fetch-api/fetch-referrer-policy.html )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Chrome扩展程序中使用提取功能在请求中不包含引荐标头 - Using fetch in chrome extension doesn't include referer header in the request 没有Origin标头的Chrome扩展AJAX请求 - Chrome extension AJAX request without Origin header Chrome扩展程序:如何更改AJAX请求标头中的来源? - Chrome Extension: how to change origin in AJAX request header? Cross Origin Chrome扩展程序 - Cross Origin Chrome Extension 为什么我的 fetch 正文总是在 Google Chrome 扩展程序中返回 null - why my fetch body always return null in Google Chrome Extension 如何在更改原点的 chrome-extension 中使用 fetch? - How can I use fetch in chrome-extension that I change the origin? fetch() 发送小写 header 键 - fetch() sends lower case header keys Chrome扩展程序20小时后没有“Access-Control-Allow-Origin”标题 - Chrome extension No 'Access-Control-Allow-Origin' header after 20 hours 解决 Chrome 扩展程序的“不存在‘Access-Control-Allow-Origin’标头”问题 - Resolve "No 'Access-Control-Allow-Origin' header is present" issue with Chrome Extension “尝试与Chrome扩展程序中的Google Contacts API进行交互时,不会出现”Access-Control-Allow-Origin“标题 - “No 'Access-Control-Allow-Origin' header is present” when trying to interact with Google Contacts API from Chrome extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM