How do I get a list of filenames in a subfolder of a Chrome extension?

时光毁灭记忆、已成空白 提交于 2019-12-20 05:59:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!