Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将自定义用户代理与特定的Google Chrome页面/选项卡关联_Javascript_Google Chrome_Google Chrome Extension_Http Headers_User Agent - Fatal编程技术网

Javascript 将自定义用户代理与特定的Google Chrome页面/选项卡关联

Javascript 将自定义用户代理与特定的Google Chrome页面/选项卡关联,javascript,google-chrome,google-chrome-extension,http-headers,user-agent,Javascript,Google Chrome,Google Chrome Extension,Http Headers,User Agent,我正在开发一个Google Chrome扩展,我想在不影响其他页面或选项卡的情况下,为选项卡/页面或弹出窗口(iframe显示为“气泡弹出窗口”)设置一个特定的用户代理 是否可能?可用于修改用户代理标头。 注意:开发者工具的网络选项卡显示旧的标题。我已经使用(nc-l 127.0.0.1-p 6789)验证了标题设置是否正确 在下面的示例中,代码在所有选项卡上激活。根据您的要求调整。添加tabId以限制此过滤器的功能,并使用选项卡的tabId(特别是可通过各种API获得) background.

我正在开发一个Google Chrome扩展,我想在不影响其他页面或选项卡的情况下,为选项卡/页面或弹出窗口(iframe显示为“气泡弹出窗口”)设置一个特定的用户代理

是否可能?

可用于修改用户代理标头。
注意:开发者工具的网络选项卡显示旧的标题。我已经使用(
nc-l 127.0.0.1-p 6789
)验证了标题设置是否正确

在下面的示例中,代码在所有选项卡上激活。根据您的要求调整。添加
tabId
以限制此过滤器的功能,并使用选项卡的tabId(特别是可通过各种API获得)

background.js
manifest.json
文档
  • 事件
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: [
            "http://stackoverflow.com/*",
            "http://127.0.0.1:6789/*"
        ],
        // In the main window and frames
        types: ["main_frame", "sub_frame"]
    },
    ["blocking", "requestHeaders"]
);
{
  "name": "WebRequest UA test",
  "version": "1.0",
  "permissions": ["webRequest", "webRequestBlocking", "http://*/*"],
  "background": {
    "scripts": ["background.js"]
  },
  "manifest_version": 2
}