-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoriginalCode.js
70 lines (59 loc) · 2.21 KB
/
originalCode.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
//self invoked function (so it doesn't pollute the global scope)
(async function () {
const fetchLoadedImages = (querySelector, expectedNumberOfImages) => {
console.log("Waiting for images to load...");
return new Promise((resolve) => {
const intervalId = setInterval(() => {
const images = [...document.querySelectorAll(querySelector)]; //arrays are easier to work with
if (images.length === expectedNumberOfImages && images.every(image => image.src && image.complete)) { //Musescore sometimes initializes the image without a src
clearInterval(intervalId);
resolve(images);
}
}, 500);
})
}
const scrollViewSelector = "#jmuse-scroller-component";
const pageElementSelector = `${scrollViewSelector}>.F16e6`;
const imageElementSelector = `${pageElementSelector}>img`;
const pageContainer = document.querySelector(scrollViewSelector);
const allChildren = document.querySelectorAll(scrollViewSelector + ">*");
const pages = document.querySelectorAll(pageElementSelector);
const SCROLL_HEIGHT_PX = 10000;
const VIEWPORT_HEIGHT_PX = 9999;
//so all images are "visible" on the page
for (const el of allChildren) {
el.style.display = "none";
}
pageContainer.insertAdjacentHTML("beforeend", `<div style="height:${SCROLL_HEIGHT_PX}px"></div>`);
pageContainer.style.height = VIEWPORT_HEIGHT_PX + "px";
pageContainer.scrollTo(0, 0); //just to reset scroll real quick
pageContainer.scrollTo(0, 1);
const images = await fetchLoadedImages(imageElementSelector, pages.length);
document.getElementsByTagName("html")[0].innerHTML = "";
const style = document.createElement("style");
style.textContent = `
body{
margin:0;
}
img{
height:296mm; /* sometimes it overflows to the next page if it's 297mm */
}
@page {
size: A4;
margin: 0;
}
@media print {
html, body {
width: 210mm;
height: 297mm;
}
}
`;
document.head.appendChild(style);
for (const image of images) {
const imageClone = document.createElement("img");
imageClone.src = image.src;
document.body.appendChild(imageClone);
}
fetchLoadedImages("img", pages.length).then(() => window.print());
})();