如何使用xpcom更改firefox代理设置 [英] how to change firefox proxy settings using xpcom

查看:113
本文介绍了如何使用xpcom更改firefox代理设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行在本地主机(127.0.0.1)的代理服务器,而且我已经厌倦了不得不培训用户如何在Firefox中切换代理来绕过被阻止的网站。

我决定写一个插件。我想知道如何使用 xpcom 来告诉firefox使用某个代理,例如:http $ http
,使用127.0.0.1端口8080.

互联网上的例子稀缺。



感谢解决方案

代理设置存储在偏好设置。您可能需要更改 network.proxy.type network.proxy.http 网络.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);

如果您需要为每个URL动态地确定代理,则可以使用 nsIProtocolProxyService 界面 - 它允许您定义一个代理过滤器。像这样的应该工作:


$ b

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

//提前创建代理信息对象,避免每次创建一个
var myProxyInfo = pps.newProxyInfo(http,127.0.0.1,8080,0,-1 ,0);
$ b var filter = {
applyFilter:function(pps,uri,proxy)
{
if(uri.spec == ...)
返回myProxyInfo;
else
返回代理;
}
};
pps.registerFilter(filter,1000);


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

解决方案

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);

这篇关于如何使用xpcom更改firefox代理设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆