解决方法
webRequest
API可用于修改User Agent标头.
注意:Developer工具的Network选项卡显示旧标题.我已经使用 netcat(nc -l 127.0.0.1 -p 6789)验证了标题设置正确.
在下面的示例中,代码在所有选项卡上激活.调整request filter以满足您的要求.添加tabId以限制此过滤器的功能,使用选项卡的tabId(可通过各种API获取,特别是chrome.tabs
).
background.js
chrome.webRequest.onBeforeSendHeaders.addListener( function(info) { // Replace the User-Agent header var headers = info.requestHeaders; headers.forEach(function(header,i) { if (header.name.toLowerCase() == 'user-agent') { header.value = 'Spoofed UA'; } }); return {requestHeaders: headers}; },// Request filter { // Modify the headers for these pages urls: [ "https://stackoverflow.com/*","http://127.0.0.1:6789/*" ],// In the main window and frames types: ["main_frame","sub_frame"] },["blocking","requestHeaders"] );
的manifest.json
{ "name": "WebRequest UA test","version": "1.0","permissions": ["webRequest","webRequestBlocking","http://*/*"],"background": { "scripts": ["background.js"] },"manifest_version": 2 }
文档