Skip to content

Instantly share code, notes, and snippets.

@2S1one
Created July 9, 2025 09:33
Show Gist options
  • Select an option

  • Save 2S1one/cbb3aed94f3e6c3c3772a4bd5d28cc7d to your computer and use it in GitHub Desktop.

Select an option

Save 2S1one/cbb3aed94f3e6c3c3772a4bd5d28cc7d to your computer and use it in GitHub Desktop.
<!--
OPFS Recursive Viewer
This page recursively lists all files stored in the browser's Origin Private File System (OPFS),
using the File System Access API. If a specific file path (defined in `targetPath`) is found,
its contents are read and displayed in the <pre> block. Useful for debugging or inspecting
OPFS contents in a human-readable form.
NOTE: `targetPath` should be replaced with a specific OPFS file path to auto-display its content.
-->
<!DOCTYPE html>
<html>
<head>
<title>OPFS Recursive Viewer</title>
</head>
<body>
<h2>Files in OPFS (All Levels)</h2>
<ul id="fileList"></ul>
<pre id="fileContent"></pre>
<script>
const targetPath = '<OPFS_TARGET_FILE>';
async function listRecursive(dirHandle, path = '') {
const list = document.getElementById("fileList");
for await (const [name, handle] of dirHandle.entries()) {
const fullPath = path + name;
if (handle.kind === 'file') {
const item = document.createElement("li");
item.textContent = fullPath;
list.appendChild(item);
if (fullPath === targetPath) {
const pathArray = fullPath.split('/');
readFile(pathArray);
}
} else if (handle.kind === 'directory') {
await listRecursive(
await dirHandle.getDirectoryHandle(name),
fullPath + '/'
);
}
}
}
async function readFile(pathArray) {
let currentDir = await navigator.storage.getDirectory();
for (let i = 0; i < pathArray.length - 1; i++) {
currentDir = await currentDir.getDirectoryHandle(pathArray[i]);
}
const fileHandle = await currentDir.getFileHandle(pathArray[pathArray.length - 1]);
const file = await fileHandle.getFile();
const text = await file.text();
document.getElementById("fileContent").textContent = text;
}
async function main() {
const root = await navigator.storage.getDirectory();
await listRecursive(root);
}
main();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment