user2098016
user2098016

Reputation: 93

Set proxy using google chrome extension

I am trying to build a chrome extension which can change proxy settings when the fire up the browser. I have followed the chrome extension documentation but still no success.

manifest.json

   {

      "manifest_version": 2,

      "name": "Proxy",
      "description": "Proxy on 127.0.0.1:8080",
      "version": "1.1",
      "background": {
      "scripts":["background.js"]
      },
      "browser_action": {
            "default_icon": "icon.png",
            "popup":"popup.html"
        },
    "permissions": [
        "tabs",
        "http://*/*",
        "https://*/*",
        "notifications",
        "contextMenus",
        "history",
        "background",
        "proxy"
    ],
    "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
    }

backround.js

chrome.windows.onCreated.addListener(function() {

var config = {
  mode: "fixed_servers",
  rules: {
    proxyForHttp: {
      scheme: "http",
      host: "127.0.0.1",
      port:"8080"
    },
    bypassList: ["foobar.com"]
  }
};
chrome.proxy.settings.set(
    {value: config, scope: 'regular'},
    function() {});

});

The above code doesn't works...

Upvotes: 7

Views: 30508

Answers (6)

Hasyidan Paramananda
Hasyidan Paramananda

Reputation: 188

"when the fire up the browser"

You Should use Chrome.Runtime instead chrome.windows.onCreated. Use chrome.runtime.onStartup, So When Browser is open from nothing hrome.runtime.onStartup will fired.

onStartup Fired when a profile that has this extension installed first starts up. This event is not fired when an incognito profile is started, even if this extension is operating in 'split' incognito mode.

It Will Be Like This

chrome.runtime.onStartup.addListener(function() {
var config = {
  mode: "fixed_servers",
  rules: {
    proxyForHttp: {
      scheme: "http",
      host: "127.0.0.1",
      port:"8080"
    },
    bypassList: ["foobar.com"]
  }
};
chrome.proxy.settings.set(
    {value: config, scope: 'regular'},
    function() {});

});

also you can use onInstalled for when you install / update the extension...

Upvotes: 0

arif
arif

Reputation: 31

I've been trying everything since yesterday, finally found my issue; I had to change

proxyForHttp: --> singleProxy:

and

port:'8080' --> port: 8080

Upvotes: 3

Pacerier
Pacerier

Reputation: 89783

Change the line port:"8080" to port:8080 and it'll work.


Nicety

You can check effective settings at chrome://net-internals/#proxy.


Nicety

If via PAC script, on script code error, chrome.proxy.settings.set handler still runs even as the proxy fails silently. This can be detected at chrome://net-internals/#events.


Nicety

This page claims that console.log messages in your PAC script can be found in net log, but it doesn't seem to work.

Upvotes: 3

user5721763
user5721763

Reputation: 1

I tried your code ,chrome give me tips that the port num is to be integer ,but not string and change this port:"8080" to port:8080,it worked but i did not follow all of your codes, and delete this chrome.windows.onCreated.addListener(function() { ,leaving only the contents in this function 3ks for your question!

Upvotes: 0

Kinlan
Kinlan

Reputation: 16605

It shouldn't be a problem with the background page. Your code is in the onCreated event on the window object. You are not guaranteed to have the extension loaded by the time the first window is created.

Simply remvove the event and have the code run, you should then have it run once when the extension is initialised.

Upvotes: 1

abisib
abisib

Reputation: 1

i don't think you can use the chrome.proxy API from Background.js. i did the same with a pop-up extension (used the code samples from the documentation) and it works perfectly...

Upvotes: 0

Related Questions