-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileViewer.html
51 lines (47 loc) · 1.42 KB
/
FileViewer.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>File Viewer</title>
</head>
<body>
<button>打開文件夾</button>
<script>
const btn = document.querySelector("button");
btn.onclick = async function () {
try {
const handle = await showDirectoryPicker();
const root = await processHandle(handle);
await openFile(root.children[1]);
console.log(root);
// 可以使用 highlight.js 去高光化程式碼
// https://github.com/highlightjs/highlight.js
} catch {
console.error('Error occur');
}
};
async function processHandle(handle) {
if (handle.kind === "file") {
return handle;
}
handle.children = [];
const iterators = await handle.entries(); // 取得文件夾中所有的內容
// iter 是一個異步迭代器
for await (const iterator of iterators) {
// 遞迴的處理每一個 handle
const subHandle = await processHandle(iterator[1]);
handle.children.push(subHandle);
}
return handle;
}
async function openFile(handle) {
const file = await handle.getFile();
const reader = new FileReader();
reader.onload = (e) => {
console.log(e.target.result);
};
reader.readAsText(file, "utf-8");
}
</script>
</body>
</html>