admin管理员组文章数量:1425815
I want to create a Chrome Extension that can send AJAX calls with XMLHttpRequest
. The website that I send the request to it not mine.
When the website gets AJAX call, it check the Referer
header of the request. If I send the AJAX from my background-page (Chrome Extension) no Referer
header sends, and the request denied.
How can I change the Referer
header from the background-page?
I want to create a Chrome Extension that can send AJAX calls with XMLHttpRequest
. The website that I send the request to it not mine.
When the website gets AJAX call, it check the Referer
header of the request. If I send the AJAX from my background-page (Chrome Extension) no Referer
header sends, and the request denied.
How can I change the Referer
header from the background-page?
2 Answers
Reset to default 3You should be able to intercept your own request with webRequest
API and modify request headers.
Specifically, listen to chrome.webRequest.onBeforeSendHeaders
in a blocking manner, edit the headers object, and return it to override headers.
Not all headers can be modified in this way, but Referer
can.
As stated in Xan's answer, you need to use the webRequest API. Since I love minimal examples in answers, here is one:
manifest.json permissions need to contain:
"permissions": [
"*://target.site/",
"webRequest",
"webRequestBlocking"
]
The js code to modify requests to contain an arbitrary referer:
callback = function(details) {
details.requestHeaders.push({
name: 'Referer',
value: 'http://your.arbitrary.referer'
});
return {
requestHeaders: details.requestHeaders
};
};
filter = { urls: ["*://target.site/target.html"] };
opts = ['blocking', 'requestHeaders']
chrome.webRequest.onBeforeSendHeaders.addListener(callback, filter, opts);
Whenever the browser makes a request to *://target.site/target.html
now, the referer will be set to http://your.arbitrary.referer
. This takes effect for requests done by the user (by clicking on links or being on a site that performs AJAX-requests) as well as AJAX-requests by your own extension.
本文标签: javascriptSending AJAX from Chrome ExtensionStack Overflow
版权声明:本文标题:javascript - Sending AJAX from Chrome Extension - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745452803a2658964.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论