Skip to content

Instantly share code, notes, and snippets.

@lpnam0201
Created January 30, 2020 16:44
Show Gist options
  • Save lpnam0201/e0ca6a19c123b93a439714c8f4f7f6b0 to your computer and use it in GitHub Desktop.
Save lpnam0201/e0ca6a19c123b93a439714c8f4f7f6b0 to your computer and use it in GitHub Desktop.
FileSystemDirectoryReader.readEntries() only returns 100 entries a time, must be called repeatedly to enumerate all entries.
function readEntriesAsync(reader) {
return new Promise((resolve, reject) => {
reader.readEntries(entries => {
resolve(entries);
}, error => reject(error));
})
}
async function enumerateDirectoryWithManyFiles(directoryEntry) {
let reader = directoryEntry.createReader();
let resultEntries = [];
let read = async function() {
let entries = await readEntriesAsync(reader);
if (entries.length > 0) {
resultEntries = resultEntries.concat(entries);
await read();
}
}
await read();
return resultEntries;
}
// Run in chrome extension's background page's console
chrome.runtime.getPackageDirectoryEntry(async rootEntry => {
let entries = await enumerateDirectoryWithManyFiles(rootEntry);
console.log(entries);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment