最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set proxy using google chrome extension - Stack Overflow

programmeradmin1浏览0评论

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"]
  }
};
chrome.proxy.settings.set(
    {value: config, scope: 'regular'},
    function() {});

});

The above code doesn't works...

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...

Share Improve this question edited Jul 9, 2019 at 12:19 Cœur 38.7k26 gold badges202 silver badges277 bronze badges asked Feb 22, 2013 at 9:04 user2098016user2098016 931 gold badge1 silver badge4 bronze badges 0
Add a comment  | 

6 Answers 6

Reset to default 3

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.

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

proxyForHttp: --> singleProxy:

and

port:'8080' --> port: 8080

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.

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...

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!

"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...

发布评论

评论列表(0)

  1. 暂无评论