如何使用xpcom更改firefox代理设置

Dr *_*Deo 2 javascript xpcom firefox-addon

我有一个在localhost(127.0.0.1)上运行的代理服务器,我已经厌倦了培训用户如何在firefox中切换代理以绕过被阻止的网站.
我决定写一个插件.我想知道如何使用xpcom告诉firefox使用某个代理例如
http,使用127.0.0.1端口8080.
互联网上的例子很少.

谢谢

Wla*_*ant 6

代理设置存储在首选项中.你可能想改变network.proxy.type,network.proxy.httpnetwork.proxy.http_port(文档).像这样:

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);
Run Code Online (Sandbox Code Playgroud)

如果需要为每个URL动态确定代理,可以通过nsIProtocolProxyService接口使用功能提供程序- 它允许您定义"代理过滤器".这样的事情应该有效:

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);
Run Code Online (Sandbox Code Playgroud)