-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbook.js
More file actions
37 lines (31 loc) · 1.48 KB
/
book.js
File metadata and controls
37 lines (31 loc) · 1.48 KB
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
// This will provide library work utilities in the future.
function saveAsStendhalFile() {
let stendhalText = `title: ${document.querySelector('[property="og:title"]').content}\n`
+ `author: ${document.querySelector('[class="signee-name"]').innerText}\n`
+ "pages:\n";
Array.from(document.getElementsByClassName("page-content")).forEach(page => {
stendhalText += `#- ${page.textContent}\n`;
});
stendhalText += "#- ";
// output to file, download, and cleanup
const element = document.createElement('a');
element.href = window.URL.createObjectURL(new Blob([stendhalText]));
// use page title as filename. Consider using `book title-author-iteration` instead
element.download = `${document.getElementsByTagName("title")[0].innerText}.stendhal`;
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function insertStendhalDownloadButton() {
var buttonNode = document.createElement('button');
buttonNode.type = "button";
buttonNode.onclick = () => saveAsStendhalFile();
buttonNode.innerText = "Download as .Stendhal";
var stendhalDownloadNode = document.createElement('div');
stendhalDownloadNode.classList.add('download-button');
stendhalDownloadNode.appendChild(buttonNode);
let homeNode = document.getElementsByClassName("back-home")[0];
homeNode.parentNode.insertBefore(stendhalDownloadNode, homeNode.nextSibling);
}
insertStendhalDownloadButton();