相关疑难解决方法(0)

从Chrome扩展程序更改XMLHttpRequest中的User-Agent

我正在尝试从扩展中发送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一起使用以防止锁定.

google-chrome-extension

8
推荐指数
1
解决办法
6276
查看次数

标签 统计

google-chrome-extension ×1