-
Notifications
You must be signed in to change notification settings - Fork 31
/
PerlegoOutline.js
33 lines (28 loc) · 1.03 KB
/
PerlegoOutline.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
function download(filename, text) {
const pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
if (document.createEvent) {
const event = document.createEvent('MouseEvents');
event.initEvent('click', true, true);
pom.dispatchEvent(event);
} else {
pom.click();
}
}
const table = Array.from(document.querySelectorAll('h2')).find(el=>el.textContent === 'Table of contents').parentNode;
const labels = table.querySelectorAll("a");
let output = "";
labels.forEach((label)=>{
let parentLayer = 0;
for (let e = label; e !== table; e = e.parentNode) {
parentLayer++;
}
const layer = Math.floor((parentLayer - 5) / 2);
const linkElems = label.getAttribute("href").split('/');
const pageNum = linkElems[linkElems.length - 1];
const textContent = label.innerText;
output += `${layer} ${pageNum} ${textContent}\n`;
}
);
download('outline.txt', output);