HyL*_*ian 8 google-chrome-extension
我正在尝试从扩展中发送HTTP请求,我需要更改用户代理.
我的代码看起来像这样:
function getXMLHttpRequest(method, url, extraHeaders) {
var xhr = new XMLHttpRequest();
xhr.open(method, url, true)
for (var headerKey in extraHeaders) {
xhr.setRequestHeader(headerKey, extraHeaders[headerKey]);
}
return xhr;
}
//....
getXMLHttpRequest("POST", "....", { "User-Agent": "Blahblahblah" })
Run Code Online (Sandbox Code Playgroud)
然后,我收到错误"拒绝设置不安全的标头:UserAgent"
我需要改变它,因为我的后端需要有一个特殊的用户代理,是否可以通过扩展来做到这一点?
我尝试了webRequest API,在发送请求之前更改标头,但它说它不能与扩展中的XMLHttpRequest一起使用以防止锁定.
您可以使用webRequest
API 轻松更改User-Agent标头.
有关示例代码,请参阅将自定义用户代理与特定Google Chrome页面/标签相关联.
就拿从这个答案的代码,并更改"main_frame", "sub_frame"
到"xmlhttprequest"
修改通过发起的网络请求XMLHttpRequest
.
显然,要防止死锁,这种方法并没有与工作同步请求(即当第三个参数xhr.open
设置为false
).
归档时间: |
|
查看次数: |
6276 次 |
最近记录: |