【问题标题】:how to use proxy in chrome extension on specific domain and url如何在特定域和 url 的 chrome 扩展中使用代理
【发布时间】:2021-05-05 12:37:23
【问题描述】:

如何强制浏览器为特定域(例如 google.com 或 facebook.com)使用代理服务器

用例是如果 google.com 或 facebook.com 被阻止,我想通过代理连接强制连接到这些站点,而其他所有内容都直接访问 Internet。

我的代码是:

backgroud.js

function FindProxyForURL(url, host) {

    // use proxy for specific domains
    if (shExpMatch(host, "*.google.com|*.facebook.com"))
        return "PROXY yourproxy:8080";

    // by default use no proxy
    return "DIRECT";
}

menifest.json

{
       "background": {
          "scripts": [ "background.js" ]
       },
      
       
       "description": "This chrome extension use for proxy.",
       
    
     
       "author": "no one",
    
     
       "manifest_version": 2,
       "minimum_chrome_version": "26.0",
       "name": "proxy",
       "permissions": [  "<all_urls>", "webRequest", "webRequestBlocking", "storage", "tabs", "proxy", "cookies", "management", "http://*/*", "https://*/*" ],
       "update_url": "https://clients2.google.com/service/update2/crx",
       "version": "0.10"
    }

【问题讨论】:

    标签: javascript google-chrome-extension proxy extension-methods


    【解决方案1】:

    您需要将 FindProxyForURL() 函数作为文本注入 PAC 配置,然后通过 chrome.proxy.settings.set() 传递。所以在你的情况下background.js 应该是:

    function FindProxyForURL(url, host) {
        // use proxy for specific domains
        if (shExpMatch(host, "*google.com|*facebook.com"))
            return "PROXY yourproxy:8080";
    
        // by default use no proxy
        return "DIRECT";
    }
    
    config = {
      mode: "pac_script",
      pacScript: {}
    };
    
    config.pacScript.data = FindProxyForURL.toString();
    
    chrome.proxy.settings.set(
      {value: config, scope: "regular"},
      function() {}
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-11
      • 1970-01-01
      • 2020-08-25
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多