如何获取Chrome扩展程序子文件夹中的文件名列表? [英] How do I get a list of filenames in a subfolder of a Chrome extension?
问题描述
我想知道是否可以获取Chrome扩展的子文件夹中所有文件的文件名列表。
I was wondering if it is possible to get a list of filenames of all files in a subfolder of a Chrome extension.
谢谢!
推荐答案
使用 chrome.runtime.getPackageDirectoryEntry
方法为Chrome扩展程序获取 DirectoryEntry
,然后使用 FileSystem API 查询其内容
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);
}
});
})();
});
这是一个基本的例子,列出了所有文件和目录的名称您的Chrome扩展程序包的根目录。如果您想查询特定目录的内容,那么您需要先获取该条目。例如。列出 _locales
目录的内容。第二个参数应该是Object实例:
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.
});
这篇关于如何获取Chrome扩展程序子文件夹中的文件名列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!