陈建华的博客
专注web开发
火狐扩展如何使用xpcom来修改火狐的代理配置
2014-11-11 14:44:01   阅读901次

查找了很多的资料,都没有中文资料来解决这个问题。今天终于查到了英文的资料,以此记录


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

share|improve this questionasked May 15 '12 at 16:38Dr Deo2,07222139


add a comment

1 Answer



Proxy settings are stored in the preferences. You probably want to change network.proxy.typenetwork.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);
share|improve this answeredited May 15 '12 at 20:28answered May 15 '12 at 17:13Wladimir Palant35k73664

   Thanks. I have seen this somewhere. But it has a problem of changing the proxy globally, yet i prefer it does it per page –  Dr Deo May 15 '12 at 17:531 @DrDeo: It's generally a good idea to put this kind of thing into the question ;) I think that the only way is changing the global proxy settings when the page starts loading, from what I remember there is no proxy setting for a load group. But if the proxy selection logic is fixed then you could put it into a PAC file. – Wladimir Palant May 15 '12 at 18:41    sorry about that –  Dr Deo May 15 '12 at 19:54   @DrDeo: Looked into this, apparently I was wrong - there is a way to set the proxy dynamically for each URL. Edited my answer. –  Wladimir Palant May 15 '12 at 20:28

add a comment




-----------------------------------------------------
转载请注明来源此处
原地址:#

-----网友评论----
暂无评论
-----发表评论----
微网聚博客乐园 ©2014 blog.mn886.net 鲁ICP备14012923号   网站导航