Created
January 11, 2011 16:40
-
-
Save nanto/774684 to your computer and use it in GitHub Desktop.
Get a list of files from a chrome URL.
This file contains 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
/* | |
* 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