AJAX sending from Chrome extension
I want to create a Chrome extension that can make AJAX calls using XMLHttpRequest
. The website I'm submitting the request to is not mine.
When the website receives an AJAX call, it checks the Referer
request header . If I send AJAX from my main page (Chrome extension), the no header is Referer
sent and the request is denied.
How do I change the title Referer
on the original page?
source to share
You can intercept your request using the webRequest
API and change the request headers.
In particular, listen in a chrome.webRequest.onBeforeSendHeaders
blocking way, edit the headers object and return it to override the headers.
Not all titles can be changed this way, but they Referer
can.
source to share
As stated in Xan's answer, you need to use the webRequest API . Since I like the minimal example answers, here's one:
manifest.json permissions must contain:
"permissions": [
"*://target.site/",
"webRequest",
"webRequestBlocking"
]
Js code for modifying requests containing an arbitrary referent:
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 *://target.site/target.html
, the referent will be set to http://your.arbitrary.referer
. This takes effect for requests made by the user (by clicking on links or being on a site that makes AJAX requests), as well as AJAX requests by its own extension.
source to share
The W3 specification explicitly forbids changing the "Referer" header. If you try to set Referer manually, the request will be rejected.
Source: http://www.w3.org/TR/XMLHttpRequest/#dom-xmlhttprequest-setrequestheader
source to share