简体   繁体   English

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

[英]how to change firefox proxy settings using xpcom

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. 我有一个在本地主机(127.0.0.1)上运行的代理服务器,我已经厌倦了必须培训用户如何在firefox中切换代理以绕过被阻止的网站的麻烦。
I decided to write an addon. 我决定写一个插件。 I wonder how to use xpcom to tell firefox to use a certain proxy eg 我想知道如何使用xpcom告诉firefox使用某个代理
for http, use 127.0.0.1 port 8080. 对于http,请使用127.0.0.1端口8080。
Examples on the internet are scarce. 互联网上的例子很少。

Thanks 谢谢

Proxy settings are stored in the preferences . 代理服务器设置存储在首选项中 You probably want to change network.proxy.type , network.proxy.http and network.proxy.http_port ( documentation ). 您可能想要更改network.proxy.typenetwork.proxy.httpnetwork.proxy.http_port文档 )。 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". 如果需要动态确定每个URL的代理,则可以通过nsIProtocolProxyService接口使用功能提供程序-它允许您定义“代理过滤器”。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM