Skip to content

Instantly share code, notes, and snippets.

@darktrojan
Last active August 29, 2015 14:15
Show Gist options
  • Save darktrojan/d76dd0da21f45073f1bb to your computer and use it in GitHub Desktop.
Save darktrojan/d76dd0da21f45073f1bb to your computer and use it in GitHub Desktop.
HTTPS redirector thing
Components.utils.import("resource://gre/modules/Services.jsm");
let redirects;
let pref = {
name: "extensions.something.pref",
init: function() {
this.readPref();
Services.prefs.addObserver(this.name, this, false);
},
destroy: function() {
Services.prefs.removeObserver(this.name, this);
},
readPref: function() {
let pref = [];
try {
pref = JSON.parse(Services.prefs.getCharPref(this.name));
} catch(ex) {
}
redirects = pref.map(s => {
if (s[0] == ".") {
return new RegExp("(^|\\.)" + s.substring(1).replace(/\./, "\\.") + "$");
}
return s;
});
},
observe: function() {
this.readPref();
}
};
let observer = {
observe: function(channel) {
if (!(channel instanceof Components.interfaces.nsIHttpChannel)) return;
let uri = channel.URI;
if (!uri.schemeIs("http")) return;
if (redirects.some(
(p) => (typeof p == "string" && uri.host == p) || (p instanceof RegExp && p.test(uri.host))
)) {
// Services.console.logStringMessage("redirecting " + uri.spec);
channel.redirectTo(Services.io.newURI(uri.spec.replace(/^http:/, "https:"), null, null));
}
}
};
function install() {
}
function uninstall() {
}
function startup() {
pref.init();
Services.obs.addObserver(observer, "http-on-modify-request", false);
}
function shutdown() {
pref.destroy();
Services.obs.removeObserver(observer, "http-on-modify-request");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment