I've done some research and I now know it's not possible to send a request with a changed referrer in Google Chrome because the browser will overwrite the change, but is there anyway/any permissions in a Google Chrome Extension that would disable this, or make it so that you could send a request to a certain domain with a different referrer?
3
-
Lets say its possible, how would you handle this, ask users to change their Chrome settings? – Grumpy Jun 23 '15 at 9:39
-
It's for personal use, I simply want to be able to send a request from a certain referrer – Sam Jun 23 '15 at 9:40
-
Is it perhaps possible, however, to change the browser settings to not overwrite the referrer? If so, how can this be done? – Sam Jun 23 '15 at 9:40
Add a comment
|
chrome.webRequest is what you're looking for, specifically thee onBeforeSendHeaders
event. It will allow you to change any headers (even unsafe ones) before sending the request, but can only be used in a background script.
You'll need to add webRequest
and webRequestBlocking
to your permissions list in the manifest.
chrome.webRequest.onBeforeSendHeaders.addEventListener(handle(details), filterObject, extraInfoArray);
Here's an example:
chrome.webRequest.onBeforeSendHeaders.addListener(function(details){
var newRef = "http://referer.domain/helloworld.example";
var gotRef = false;
for(var n in details.requestHeaders){
gotRef = details.requestHeaders[n].name.toLowerCase()=="referer";
if(gotRef){
details.requestHeaders[n].value = newRef;
break;
}
}
if(!gotRef){
details.requestHeaders.push({name:"Referer",value:newRef});
}
return {requestHeaders:details.requestHeaders};
},{
urls:["http://target.domain/*"]
},[
"requestHeaders",
"blocking",
"extraHeaders"
]);
The filterObject
tells it to only fire the handle for any with the urls matching ones in the list.
The extraInfoArray
tells it you want to get requestHeaders
, and blocking
tells it to pause the request until the handle is finished.
-
4As of chrome 72 "extraHeaders" must also be added to extraInfoArray in order to set the following headers: Accept-Language, Accept-Encoding, Referer, Cookie – WebGL3D Mar 18 '19 at 14:52
-
I have implement this code and console in background.js which reflect the changed referrer, but how can i check same in page when opens developers tool or clicked on some link. – Hemant Kumar Apr 24 '19 at 6:36
-
I pasted the above code (with mentioned permissions) in my
background.js
file as it is. Just changed thetarget_urls
. This does not work. Also, I cannot set backgroundpersistent
tofalse
, as it does not run on event pages. Have to set it totrue
to make it run. Any help on this please. – PythonEnthusiast May 29 '19 at 14:50