nukiko12
nukiko12

Reputation: 159

Chrome proxy api - Uncaught TypeError: Invalid invocation

I want to use the chrome proxy API. I have this code in my background script but it will not work

const proxyData = []

const fetchProxyData = () => {
  axios({
    method: "GET",
    baseURL: "https://api.getproxylist.com/proxy",
    params: {
      protocol: "http",
      allowsPost: 1,
      allowsHttps: 1,
      anonimity: "high anonymity"
    }
  }).then( (response) => {
    console.log(response.data)
    proxyData.push(response.data)
  })
}

fetchProxyData();

var config = {
  mode: "fixed_servers",
  rules: {
    singleProxy: {
      host: proxyData.ip,
      port: proxyData.port
    }
  }
}

chrome.proxy.settings.set({
   value: config,
   scope: "regular"
 }, () => {
   console.log(`proxy configured with data: ${proxyData}`)
})

I get this error in background page console: background.js:55 Uncaught TypeError: Invalid invocation

I've tried with the example provided with the proxy api documentation and the error will not occur. Maybe it's caused by the config object? To set the proxy as you can see in the code, I'm using an ajax call, maybe is this the problem?

is there any fix?

Upvotes: 3

Views: 1875

Answers (2)

Satender K
Satender K

Reputation: 581

I have also faced the same problem when I find the solution, it was silly mistake.

I had passed string value to port.

Please make sure you are passing integer value to port

Upvotes: 2

Kyle
Kyle

Reputation: 765

Close. Couple things.

One, you need to fetch your data before calling Chrome's proxy API. And two, your getting the properties for your config from the proxyData array, not the JSON object from your axios call.

You need to do something like this:

const proxyData = [];
const fetchProxyData = () => {
    axios({
        method: "GET",
        baseURL: "https://api.getproxylist.com/proxy",
        params: {
            protocol: "http",
            allowsPost: 1,
            allowsHttps: 1,
            anonimity: "high anonymity"
        }
    }).then((response) => {
        const {data} = response;

        proxyData.push(data);

        const config = {
            mode: "fixed_servers",
            rules: {
                singleProxy: {
                    host: data.ip,
                    port: data.port
                }
            }
        };

        chrome.proxy.settings.set({
            value: config,
            scope: "regular"
        }, () => {
            console.log(`proxy configured with data: ${data}`)
        });
    })
};

fetchProxyData();

What's happening with your code...The properties host and port in singleProxy (in config) are being assigned undefined because those properties don't exist in the array proxyData. They exist in the object returned from your axios call (above, data).

The undefined keys caused Chrome to throw an Invalid Invocation error.

For anyone else getting the Invalid Invocation issue, it seems to me the problem usually lies within the config your passing into Chrome. In particular the types of each key.

In my case, I was passing in the port as a string when it needed to be a number.

Upvotes: 0

Related Questions