首页 > 解决方案 > Chrome extensions proxy API not working

问题描述

My code:

function setproxy() {


    var config = {
            mode: "fixed_servers",
            rules: {
              singleProxy: {
                scheme: "http",
                host: "67.205.148.246",
                port:  53
              },
              bypassList: ["*site1.com", "*site2.com", "*ifconfig.co"]
            }
          };


    alert(JSON.stringify(config));

    chrome.proxy.settings.set(
      {value: config, scope: 'regular'},
      function() {});

}

document.getElementById('setproxy').addEventListener('click', setproxy);

When I click on setproxy button proxy does not work.

In chrome://net-internals/#proxy I see this:

Effective proxy settings
Proxy server: 67.205.148.246:53
Bypass list: 
  *site1.com
  *site2.com
  *ifconfig.co

But when I check IP with ifconfig.co it is still mine.

In manifest.json I set permissioms:

"permissions": ["proxy", "tabs", "http:///","https:///"]

But it does not work yet. Why?

67.205.148.246:53 is example proxy, founded in internet. It works (I try it in Firefox)

标签: google-chromegoogle-chrome-extension

解决方案


var proxyConfig = function (proxyAddress) {
    if (proxyAddress === null || proxyAddress === "") {
        return {
            mode: "direct"
        };
    }
    return {
        mode: "fixed_servers",
        rules: {
            proxyForHttp: {
                host: proxyAddress.split(":")[0],
                port: parseInt(proxyAddress.split(":")[1])
            },
            proxyForHttps: {
                host: proxyAddress.split(":")[0],
                port: parseInt(proxyAddress.split(":")[1])
            },
            bypassList: ["www.google.com"]
        }
    }
};  

试试看:

  • 替换 singleProxy -> proxyForHttp || proxyForHttps
  • 并删除 {scheme: "https"...}

推荐阅读