[英]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.