admin管理员组文章数量:1290231
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?
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?
Share Improve this question asked Jun 23, 2015 at 9:37 SamSam 2213 silver badges12 bronze badges 3- Lets say its possible, how would you handle this, ask users to change their Chrome settings? – Grumpy Commented Jun 23, 2015 at 9:39
- It's for personal use, I simply want to be able to send a request from a certain referrer – Sam Commented Jun 23, 2015 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 Commented Jun 23, 2015 at 9:40
2 Answers
Reset to default 19chrome.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.
now in manifest v3 webRequestBlocking
is no more there .. instead create a rule to dynamically set the referrer ref
permission:
"permissions": [
"declarativeNetRequest"
]
in backgroud script
chrome.declarativeNetRequest.updateDynamicRules({
addRules: [{
"id": 1,
"priority": 1,
"action": {
"type": "modifyHeaders",
"requestHeaders": [
{ "header": "Referer", "operation": "set", "value": "htts://targetHost" }
]
},
"condition": {
"urlFilter": "https://condidtionUrl",
"resourceTypes": ["xmlhttprequest"] // see available https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/#type-ResourceType
}
}],
})
operation apend
having some know bugs ref instead use set
本文标签:
版权声明:本文标题:javascript - In Chrome Extension, change referrer for ajax requests sent to certain domain? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738495396a2089955.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论