-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.html
97 lines (84 loc) · 3.11 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<html lang="en-US">
<body style="display: flex; flex-direction: column">
<div style="padding-bottom: 10px; border-bottom: 2px solid black">
<h3>Step1: Click the file picker, choose StorableSidebar.json</h3>
<input type="file" id="file" />
</div>
<div style="padding-bottom: 10px; border-bottom: 2px solid black">
<div style="display: flex; flex-direction: column; width: 300px">
<h3>Step2: Click download button</h3>
<textarea id="result" rows="10" cols="30"></textarea>
<button id="download" style="margin-top: 10px">download</button>
</div>
</div>
<div style="padding-bottom: 10px; border-bottom: 2px solid black">
<h3>Step3: Import to Chrome</h3>
Select the file you just downloaded in Chrome's Import Bookmarks feature
</div>
<script>
const formatItems = (items) => {
const bookmarks = []
items.forEach((item) => {
if (typeof item !== 'string') {
const { data } = item.value
if (data?.tab) {
const { savedTitle, savedURL, timeLastActiveAt } = data.tab
bookmarks.push(
`<DT><A HREF="${savedURL}" ADD_DATE="${Math.floor(
timeLastActiveAt,
)}">${savedTitle}</A></DT>`,
)
} else {
// 书签的层级结构,忽略掉
}
}
})
const html = `<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
${bookmarks.join('\n')}
</DL><p>`
document.querySelector('#result').value = html
}
const readFile = () => {
var selectedFile = document.querySelector('#file').files[0]
var reader = new FileReader()
reader.readAsText(selectedFile)
reader.onload = () => {
let json = JSON.parse(reader.result)
// let items = json.sidebar.containers.[1].items // maybe it's work, not sure
let items = json.sidebarSyncState.items
formatItems(items)
}
}
const downloadFile = () => {
const textarea = document.querySelector('#result')
const quicklyDownload = (content, filename) => {
var eleLink = document.createElement('a')
eleLink.download = filename
eleLink.style.display = 'none'
var blob = new Blob([content])
eleLink.href = URL.createObjectURL(blob)
document.body.appendChild(eleLink)
eleLink.click()
document.body.removeChild(eleLink)
}
quicklyDownload(textarea.value, 'bookmarks.html')
}
const bindFileReader = () => {
const inputElement = document.querySelector('#file')
inputElement.addEventListener('change', readFile, false)
}
const bindDownload = () => {
const downloadButton = document.querySelector('#download')
downloadButton.addEventListener('click', downloadFile)
}
const __main = () => {
bindFileReader()
bindDownload()
}
__main()
</script>
</body>
</html>