how to change firefox proxy settings using xpcom

眉间皱痕 提交于 2019-12-03 14:12:38

问题


I have a proxy server running on localhost (127.0.0.1) and i have grown tired of having to train users on how to switch proxies in firefox to bypass blocked websites.
I decided to write an addon. I wonder how to use xpcom to tell firefox to use a certain proxy eg
for http, use 127.0.0.1 port 8080.
Examples on the internet are scarce.

Thanks


回答1:


Proxy settings are stored in the preferences. You probably want to change network.proxy.type, network.proxy.http and network.proxy.http_port (documentation). Like this:

Components.utils.import("resource://gre/modules/Services.jsm");
Services.prefs.setIntPref("network.proxy.type", 1);
Services.prefs.setCharPref("network.proxy.http", "127.0.0.1");
Services.prefs.setIntPref("network.proxy.http_port", 8080);

If you need to determine the proxy dynamically for each URL, you can use the functionality provider by nsIProtocolProxyService interface - it allows you to define a "proxy filter". Something like this should work:

var pps = Components.classes["@mozilla.org/network/protocol-proxy-service;1"]
          .getService(Components.interfaces.nsIProtocolProxyService);

// Create the proxy info object in advance to avoid creating one every time
var myProxyInfo = pps.newProxyInfo("http", "127.0.0.1", 8080, 0, -1, 0);

var filter = {
  applyFilter: function(pps, uri, proxy)
  {
    if (uri.spec == ...)
      return myProxyInfo;
    else
      return proxy;
  }
};
pps.registerFilter(filter, 1000);


来源:https://stackoverflow.com/questions/10605183/how-to-change-firefox-proxy-settings-using-xpcom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!