问题
I was wondering if it is possible to get a list of filenames of all files in a subfolder of a Chrome extension.
Thank you!
回答1:
Use the chrome.runtime.getPackageDirectoryEntry method to get a DirectoryEntry
for the Chrome extension, then use the FileSystem API to query its contents
chrome.runtime.getPackageDirectoryEntry(function(directoryEntry) {
var directoryReader = directoryEntry.createReader();
// List of DirectoryEntry and/or FileEntry objects.
var filenames = [];
(function readNext() {
directoryReader.readEntries(function(entries) {
if (entries.length) {
for (var i = 0; i < entries.length; ++i) {
filenames.push(entries[i].name);
}
readNext();
} else {
// No more entries, so all files in the directory are known.
// Do something, e.g. print all file names:
console.log(filenames);
}
});
})();
});
This is a basic example that lists the names of all files and directories of the root in your Chrome extension package. If you want to query the contents of a specific directory, then you need to get that entry first. E.g. to list the contents of the _locales
directory. Second parameter should be the Object instance:
directoryEntry.getDirectory('_locales', {}, function(subDirectoryEntry) {
var directoryReader = subDirectoryEntry.createReader();
// etc.. same code as in previous snippet.
});
来源:https://stackoverflow.com/questions/24720903/how-do-i-get-a-list-of-filenames-in-a-subfolder-of-a-chrome-extension