Skip to content

Instantly share code, notes, and snippets.

@shinshin86
Last active October 2, 2024 01:44
Show Gist options
  • Save shinshin86/365ae2b841d1a37cf4e2317c13bff971 to your computer and use it in GitHub Desktop.
Save shinshin86/365ae2b841d1a37cf4e2317c13bff971 to your computer and use it in GitHub Desktop.
A script to display the contents of the OPFS (intended to be executed in the console of Google Chrome's Developer Tools)
/**
* Script for displaying the contents of the OPFS
* (Intended to be executed in the console of Google Chrome's Developer Tools)
*
* Only the OPFS data related to the origin of the web page running this script can be retrieved
*/
// Get the root directory of OPFS
async function getOPFSRoot() {
return await navigator.storage.getDirectory();
}
// Recursively display directory contents
async function inspectDirectory(dirHandle, path = '') {
for await (const [name, handle] of dirHandle.entries()) {
const newPath = path ? `${path}/${name}` : name;
if (handle.kind === 'file') {
console.log(`File: ${newPath}`);
// Uncomment the following to read the contents of the file
// const file = await handle.getFile();
// const contents = await file.text();
// console.log(`Contents: ${contents}`);
} else if (handle.kind === 'directory') {
console.log(`Directory: ${newPath}`);
await inspectDirectory(handle, newPath);
}
}
}
async function inspectOPFS() {
try {
const root = await getOPFSRoot();
console.log('OPFS Root Directory:');
await inspectDirectory(root);
} catch (error) {
console.error('Error inspecting OPFS:', error);
}
}
// run
inspectOPFS();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment