Reputation: 83
How can I change Firefox proxy settings from javascript(using an addon)?
Upvotes: 1
Views: 3282
Reputation: 162
Nowadays(2013) it's very easy, using the latest SDK(1.14) you can do it with a few lines of code:
const {Cu} = require("chrome");
Cu.import("resource://gre/modules/Services.jsm");
Services.prefs.setCharPref("network.proxy.http", proxy_ip);
Services.prefs.setIntPref("network.proxy.http_port", proxy_port);
Services.prefs.setIntPref("network.proxy.type", 1);
You can also set ftp and ssl proxy IP and port if you want. The proxy_ip and proxy_port variables contain what their name describes.
That code snippet was copied from a new proxy add-on for Firefox, but any extension that uses the latest SDK is going to do the same.
Upvotes: 2
Reputation: 11
simple
netscape.security.PrivilegeManager
.enablePrivilege("UniversalBrowserAccess UniversalXPConnect");
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
prefs.setIntPref("network.proxy.type", 1);
prefs.setCharPref("network.proxy.http", "5.5.5.5");
prefs.setIntPref("network.proxy.http_port", "80");
prefs.setCharPref("javascript.enabled", "true");
Upvotes: 1
Reputation: 7614
As stated by others: FoxyProxy performs this role from a user perspective. It's certainly possible to edit the configurations using the nsIPrefService if you want to do it from your own extension for some other purpose - but it's very tricky not to damage the user experience this way (eg. overriding another extension unintentionally) as you are editing the browser's global state.
Upvotes: 0