如何判断chrome.proxy.settings.set是否正确设置了我的代理

时间:2015-08-13 13:31:36

标签: javascript google-chrome-extension

在我的Google Chrome扩展程序中,我的popup.js包括:

function setProxyToUse() {
  var enable = {
    mode: "fixed_servers",
    rules: {
      proxyForHttps: {host: localStorage["surveyHTTPS"]},
      proxyForHttp: {host: localStorage["surveyHTTP"]}
    }
  };
  var disable = {mode: "system"}
  if (localStorage["proxyOn"] == true) {var config = enable} else {var config = disable}
  chrome.proxy.settings.set({value: config, scope: 'regular'},function() {});
  chrome.proxy.settings.get({}, function(config) {console.log(config.value.host);} ); 
}

最后一行只是将undefined写入控制台。如何查看我正在使用的代理主机?

1 个答案:

答案 0 :(得分:0)

默认代理值是一个字符串"system",它没有您尝试显示的属性(.host)。

如果您想要阅读刚刚设置的值,请​​在.set的回调中执行此操作,因为chrome API是异步的:

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