javascript 使用谷歌浏览器扩展设置代理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15020387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:11:42  来源:igfitidea点击:

Set proxy using google chrome extension

javascriptjsongoogle-chromegoogle-chrome-extensiongoogle-chrome-devtools

提问by user2098016

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.

我正在尝试构建一个 chrome 扩展,它可以在启动浏览器时更改代理设置。我遵循了 chrome 扩展文档,但仍然没有成功。

manifest.json

清单文件.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

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

上面的代码不起作用...

回答by arif

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

从昨天开始我一直在尝试一切,终于找到了我的问题;我不得不改变

proxyForHttp:--> singleProxy:

proxyForHttp:--> singleProxy:

and

port:'8080'--> port: 8080

port:'8080'--> port: 8080

回答by Pacerier

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

将线路更改port:"8080"port:8080,它将起作用。



Nicety

尼斯

您可以在 chrome://net-internals/#proxy 上检查有效设置。



Nicety

尼斯

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

如果通过 PAC 脚本,在脚本代码错误时,chrome.proxy.settings.set即使代理静默失败,处理程序仍会运行。这可以在 chrome://net-internals/#events 上检测到。



Nicety

尼斯

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

该页面声称可以在网络日志中找到 PAC 脚本中的 console.log 消息,但它似乎不起作用。

回答by Kinlan

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.

后台页面应该没有问题。您的代码位于 window 对象上的 onCreated 事件中。不能保证在创建第一个窗口时加载扩展。

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

只需删除事件并运行代码,然后在扩展初始化时运行一次。

回答by user5721763

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!

我试过你的代码,chrome 给我提示端口 num 是整数,而不是字符串并将这个端口:“8080”更改为端口:8080,它有效,但我没有遵循你的所有代码,并删除这个 chrome .windows.onCreated.addListener(function() { ,只留下这个函数中的内容 3ks 给你的问题!

回答by abisib

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

我认为您不能使用 Background.js 中的 chrome.proxy API。我对弹出式扩展程序做了同样的事情(使用了文档中的代码示例)并且它完美地工作......

回答by Hasyidan Paramananda

"when the fire up the browser"

“当浏览器启动时”

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

您应该使用Chrome.Runtime而不是 chrome.windows.onCreated。使用 chrome.runtime.onStartup,所以当浏览器从空打开时 hrome.runtime.onStartup 将被触发。

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.

onStartup 当安装了此扩展的配置文件第一次启动时触发。启动隐身配置文件时,不会触发此事件,即使此扩展程序在“拆分”隐身模式下运行。

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

您也可以在安装/更新扩展时使用 onInstalled ...