Problems with chrome.proxy API

I am trying to use the chrome proxy api but i keep getting this error for some reason. I thought i was doing everything right but i guess not :/

here is my code...

var proxy_type;
var proxy_port;
var proxy_ip;
chrome.storage.sync.get(['proxy_type', 'proxy_port', 'proxy_ip', 'proxy'], function(items) {
    console.log('Settings retrieved', Object.values(items));
    proxy_type = items.proxy_type; //this is equal to socks5
    proxy_ip = items.proxy_ip; //this is equal to 66.78.44.221
    proxy_port = parseInt(items.proxy_port); //this is equal to 58124
});
var config = {
  mode: "fixed_servers",
  rules: {
    singleProxy: {
      scheme: proxy_type,
      host: proxy_ip,
      port:proxy_port
    },
    bypassList: ["foobar.com"]
  }
};

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");  
    if (request.greeting == "connect"){
        console.log('setting proxy');
        console.log(proxy_ip); //shows 66.78.44.221 in the log...
        chrome.proxy.settings.set(
          {value: config, scope: 'regular'},
          function() {});
    }
  });

Whenever i run this code though, i get the error: Error in event handler for runtime.onMessage: Error: Invalid value for argument 1. Property 'value.rules.singleProxy.host': Property is required.

If i set the values of scheme, host, and port by just typing the values in instead of using a variable it works.

So why am i getting this error?

1 answer

  • answered 2017-11-14 23:59 user650881

    The error indicates that the host field in the config may be undefined. Since the chrome.storage.sync.get is asynchronous, I expect that config object is constructed before the proxy parameters are defined.

    You could construct config in the callback and check that it is defined in the message handler:

    var config;
    
    chrome.storage.sync.get(['proxy_type', 'proxy_port', 'proxy_ip', 'proxy'], function(items) {
        console.log('Settings retrieved', Object.values(items));
        config = {
          mode: "fixed_servers",
          rules: {
            singleProxy: {
              scheme: items.proxy_type,
              host: items.proxy_ip,
              port: parseInt(items.proxy_port)
            },
            bypassList: ["foobar.com"]
          }
        };
    });
    
    chrome.runtime.onMessage.addListener(
      function(request, sender, sendResponse) {
        if (!config) {
            console.log('proxy config not defined');
            return;
        }
        console.log(sender.tab ?
                    "from a content script:" + sender.tab.url :
                    "from the extension");  
        if (request.greeting == "connect"){
            console.log('setting proxy');
            console.log(config); //shows 66.78.44.221 in the log...
            chrome.proxy.settings.set(
              {value: config, scope: 'regular'},
              function() {});
        }
    });