-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
61 lines (54 loc) · 1.63 KB
/
main.js
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
window.onload = () => {
setInterval(refresh, 500);
};
let currentHTML = "";
function toNode(htmlRawString) {
const doc = document.createElement("div");
doc.innerHTML = htmlRawString;
return doc;
}
const STORAGE_KEY = "better-blinkist";
function addTOC(readerElem) {
const headers = readerElem.getElementsByClassName("chapter chapter");
const toc = document.getElementById("toc");
let tableOfContents = "";
for (let h of headers) {
h.id = h.getAttribute("data-chapterno");
const chapterTitle = h.getElementsByTagName("h1")[0].innerText;
tableOfContents += `<p><a href='#${h.id}'>${chapterTitle}</a></p>`;
}
toc.innerHTML = tableOfContents;
}
function refresh() {
const out = document.getElementById("out");
const inbox = document.getElementById("inbox");
// if input is blank, try to get from localstorage
const raw = inbox.value || localStorage.getItem(STORAGE_KEY);
if (!raw?.length) {
// skip render if empty input
// console.log("empty");
return;
}
// clear input
inbox.value = "";
if (currentHTML === raw) {
// skip render if same input
// console.log("same");
return;
}
currentHTML = raw;
// rough processing
const start = raw.indexOf("<main");
const end = raw.indexOf("</main>") + 7;
const processed = raw.slice(start, end);
const node = toNode(processed);
// fine processing
const reader = node.getElementsByClassName("reader__container__right")[0];
if (!reader) {
out.innerHTML = "Invalid HTML. Please copy the full HTML from Blinkist.";
return;
}
addTOC(reader);
localStorage.setItem(STORAGE_KEY, currentHTML);
out.innerHTML = reader.innerHTML;
}