Last active
August 29, 2015 14:15
-
-
Save darktrojan/d76dd0da21f45073f1bb to your computer and use it in GitHub Desktop.
HTTPS redirector thing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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