chrome 不允许修改 XMLHttprequest 的 User-Agent?


console 里报错 Refused to set unsafe header "User-Agent"

chrome 插件内代码如下:

var req = new XMLHttpRequest();
req.open('GET', _URL_, false);
req.setRequestHeader("User-Agent","UA_test_string");
req.send();
console.log(req.responseText);

chrome user-agent

我家的软妹 11 years, 5 months ago

事实上你可以使用 webRequest 来实现修改 User Agent 这个需要的权限比较高,而且似乎上传到商店要审核的。

参考: https://github.com/greatghoul/mobivie...

var MOBIVIEW_URL = chrome.extension.getURL('mobiview.html');
var IPHONE4 = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5';
chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.windows.create({ url: MOBIVIEW_URL, width: 320, height: 480, type: 'panel' },
        function(win) {
            var tabId = win.tabs[0].id;
            chrome.webRequest.onBeforeSendHeaders.addListener(
                function(details) {
                    for (var i = 0; i < details.requestHeaders.length; ++i) {
                        if (details.requestHeaders[i].name === 'User-Agent') {
                            details.requestHeaders[i].value = IPHONE4;
                        }
                        if (details.requestHeaders[i].name === 'X-Frame-Options') {
                            details.requestHeaders[i].value = "GOFORIT";
                        }
                    }
                    return { requestHeaders: details.requestHeaders };
                },
                {urls: ["<all_urls>"], tabId: tabId },
                ["blocking", "requestHeaders"]
            );
        }
    );
});
二小姐的⑨ answered 11 years, 5 months ago

Your Answer