Skip to content

Instantly share code, notes, and snippets.

@jld
Last active October 26, 2017 21:36
Show Gist options
  • Select an option

  • Save jld/09a40d8b919a67cf70b0cae98c2fa7a2 to your computer and use it in GitHub Desktop.

Select an option

Save jld/09a40d8b919a67cf70b0cae98c2fa7a2 to your computer and use it in GitHub Desktop.
function urlWrangle(aUrlStr) {
const ioService = Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService);
let uri = ioService.newURI(aUrlStr);
if (uri.schemeIs("resource")) {
const ph = ioService.getProtocolHandler("resource")
.QueryInterface(Ci.nsISubstitutingProtocolHandler);
uri = ioService.newURI(ph.resolveURI(uri));
} else if (uri.schemeIs("chrome")) {
const reg = Cc["@mozilla.org/chrome/chrome-registry;1"]
.getService(Ci.nsIChromeRegistry);
uri = reg.convertChromeURL(uri);
}
if (uri.schemeIs("file")) {
let nsif = uri.QueryInterface(Ci.nsIFileURL).file;
return File.createFromNsIFile(nsif)
.then(URL.createObjectURL);
}
if (uri.schemeIs("jar")) {
return fetch(uri.spec)
.then(resp => resp.ok ? resp.blob()
: Promise.reject(new Error(resp.statusText)))
.then(URL.createObjectURL);
}
return Promise.resolve(uri.spec);
}
function loadFrameScriptSafe(mm, url, delayed) {
urlWrangle(url).then(safeUrl => mm.loadFrameScript(safeUrl, delayed));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment