Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get a list of files from a chrome URL.
/*
* This code is in the public domain.
*
* ChromeFiles.get("chrome://browser/content/")
* => [ "chrome://browser/content/NetworkPanel.xhtml",
* "chrome://browser/content/aboutDialog.css",
* ...,
* "chrome://browser/content/browser.css",
* "chrome://browser/content/browser.js",
* "chrome://browser/content/browser.xul",
* ... ]
*/
var ChromeFiles = {
get: function CF_get(spec) {
const ios = Cc['@mozilla.org/network/io-service;1'].
getService(Ci.nsIIOService);
let uri = ios.newURI(spec, null, null);
return this.getByURI(uri);
},
getByURI: function CF_getByURI(uri) {
let baseURI = uri.clone().QueryInterface(Ci.nsIURL);
baseURI.path = baseURI.directory;
const registry = Cc['@mozilla.org/chrome/chrome-registry;1'].
getService(Ci.nsIChromeRegistry);
let localURI = registry.convertChromeURL(baseURI);
let leafNames = null;
if (localURI instanceof Ci.nsIFileURL) {
leafNames = this.getLeafNamesByDirectory(localURI.file);
} else if (localURI instanceof Ci.nsIJARURI) {
leafNames = this.getLeafNamesByJARURI(localURI);
} else {
throw new Error('Unknown URI: ' + localURI.spec);
}
let baseSpec = baseURI.spec;
return leafNames.sort().map(function (leafName) baseSpec + leafName);
},
getLeafNamesByDirectory: function CF_getLeafNamesByDirectory(dir) {
let files = dir.directoryEntries;
let leafNames = [];
while (files.hasMoreElements()) {
let file = files.getNext().QueryInterface(Ci.nsIFile);
if (file.isFile())
leafNames.push(file.leafName);
}
return leafNames;
},
getLeafNamesByJARURI: function CF_getLeafNamesByJARURI(jarURI) {
let zip = this.openZipReader(jarURI.JARFile);
try {
let baseEntry = jarURI.JAREntry;
let pattern = baseEntry + '?*~' + baseEntry + '?*/*';
let entries = zip.findEntries(pattern);
let leafNames = [];
while (entries.hasMore())
leafNames.push(entries.getNext().substring(baseEntry.length));
return leafNames;
} finally {
zip.close();
}
},
openZipReader: function CF_openZipReader(uri) {
let zip = Cc['@mozilla.org/libjar/zip-reader;1'].
createInstance(Ci.nsIZipReader);
if (uri instanceof Ci.nsIFileURL) {
zip.open(uri.file);
} else if (uri instanceof Ci.nsIJARURI) {
let innerZip = this.openZipReader(uri.JARFile);
zip.openInner(innerZip, uri.JAREntry);
} else {
throw new Error('Unknown URI: ' + uri.spec);
}
return zip;
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment