-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
38 lines (32 loc) · 1.4 KB
/
script.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
const precision = 4;
const cpi = 2.54; // centimeters per inch
const dpi = 96; // dots per inch
const ppd = window.devicePixelRatio; // pixels per dot
const a4Height = 29.7; // height of an a4 in cm
const mainPadding = 5;
function pxToCm(px) {
return (px * cpi / (dpi * ppd)).toFixed(precision);
}
document.addEventListener("DOMContentLoaded", () => {
const header = document.querySelector("header").getBoundingClientRect();
const main = document.querySelector("main").getBoundingClientRect();
const footer = document.querySelector("footer").getBoundingClientRect();
const coutner = document.querySelector("#counter");
const availableMainHeight = a4Height - pxToCm(header.height) - pxToCm(footer.height) - 2 * pxToCm(mainPadding);
const pagesCount = Math.ceil(pxToCm(main.height) / availableMainHeight);
for (let index = 1; index <= pagesCount; index++) {
const pageNumberElement = document.createElement("div");
pageNumberElement.innerHTML = `<span>${index}</span> / <span>${pagesCount}</span>`;
coutner.append(pageNumberElement);
}
// Set title
const title = document.querySelector("header div");
const titleElement = document.createElement("span");
titleElement.innerText = document.title;
title.append(titleElement);
// Set time
const times = document.querySelectorAll("footer time");
times.forEach((time) => {
time.innerText = new Date().toLocaleDateString();
})
});