forked from danieltyukov/studocuhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view-doc.js
92 lines (77 loc) · 3.54 KB
/
view-doc.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
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
function generatePDF() {
const docHead = document.querySelector("head").innerHTML;
const docTitle = document.querySelector("h1").innerHTML;
const pageNodes = document.querySelector('#page-container').childNodes;
let pageWidth = pageNodes[0].offsetWidth;
let pageHeight = pageNodes[0].offsetHeight;
let printSettings;
if (pageWidth > pageHeight) {
printSettings = "{@page {size: A5 landscape;} body {zoom: 90%;}";
} else {
printSettings = "{@page {size: A5 portrait;}";
}
Array.from(pageNodes).forEach(node => {
node.childNodes[0].style = "display: block;";
});
const pdfContent = pageNodes[0].parentNode.parentNode.parentNode.innerHTML;
let pdfWindow = window.open("", "Document", "height=865,width=625,status=yes,toolbar=no,menubar=no");
pdfWindow.document.querySelector("head").innerHTML = `${docHead} <style>.nofilter{filter: none !important;}</style><style>@media print ${printSettings}</style>`;
pdfWindow.document.title = docTitle;
pdfWindow.document.querySelector("body").innerHTML = pdfContent;
const PdfNodes = parseNodes(pdfWindow.document.body.firstChild.firstChild.childNodes)
pdfWindow.document.querySelector("body").firstChild.firstChild.innerHTML = ''
PdfNodes.forEach(child => pdfWindow.document.querySelector("body").firstChild.firstChild.appendChild(child))
pdfWindow.document.querySelector("body").childNodes[0].style = "";
}
function createDownloadButton() {
let downloadBtn = document.createElement("button");
downloadBtn.classList.add("download-button-1");
downloadBtn.innerHTML = '<svg aria-hidden="true" focusable="false" data-prefix="fas" class="svg-inline--fa" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><path fill="currentColor" d="..."></path></svg><span class="download-text">Download</span>';
downloadBtn.addEventListener('click', (event) => {
event.preventDefault();
event.stopPropagation();
generatePDF();
});
return downloadBtn;
}
function refreshButtons() {
let originalButtons = document.querySelectorAll('[data-test-selector="document-viewer-download-button-topbar"]');
originalButtons.forEach(button => {
if (!button.querySelector('.download-button-1')) {
button.remove();
}
});
let container = document.querySelector("#viewer-wrapper");
if (container && !container.querySelector('.download-button-1')) {
let newButton = createDownloadButton();
container.prepend(newButton);
}
const dialog = document.querySelector('#modal-overlay');
if (dialog) {
dialog.style.display = 'none';
}
}
const mutationObserver = new MutationObserver(() => {
refreshButtons();
});
window.addEventListener('load', () => {
let monitorElement = document.querySelector("#viewer-wrapper");
if (monitorElement) {
mutationObserver.observe(monitorElement, { attributes: true, childList: true, subtree: true });
}
refreshButtons();
});
function parseNodes(nodes) {
const blueprint = String(nodes[1].innerHTML);
nodes = Array.from(nodes).filter(
(node) => !node.classList.contains("banner-wrapper")
);
nodes.forEach((node) => {
if (node?.firstChild?.firstChild?.classList.contains('blurred-container')) {
console.log(node);
const page = node.dataset.pageNo;
node.innerHTML = blueprint.replace("bg2.png", `bg${page}.png`);
}
});
return nodes;
}