Skip to content

Instantly share code, notes, and snippets.

@hoangtrung99
Last active August 30, 2023 08:06
Show Gist options
  • Save hoangtrung99/228d36468013410e1dc7a24bdc4f95de to your computer and use it in GitHub Desktop.
Save hoangtrung99/228d36468013410e1dc7a24bdc4f95de to your computer and use it in GitHub Desktop.
download giaiphapykhoa
// ==UserScript==
// @name Download giaiphapykhoa pdf
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Download giaiphapykhoa.com!
// @author https://github.com/hoangtrung99
// @match https://giaiphapykhoa.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant none
// ==/UserScript==
(function () {
"use strict";
// create style tag
const style = document.createElement("style");
style.innerHTML = `.overlay {
left: 0;
top: 0;
width: 100%;
height: 100%;
position: fixed;
background: #222;
opacity: 0.8
}
.overlay__inner {
left: 0;
top: 0;
width: 100%;
height: 100%;
position: absolute;
}
.overlay__content {
left: 50%;
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
}
.spinner {
width: 75px;
height: 75px;
display: inline-block;
border-width: 2px;
border-color: rgba(255, 255, 255, 0.05);
border-top-color: #fff;
animation: spin 1s infinite linear;
border-radius: 100%;
border-style: solid;
}
@keyframes spin {
100% {
transform: rotate(360deg);
}
}
.button-19 {
appearance: button;
background-color: #1899D6;
border: solid transparent;
border-radius: 16px;
border-width: 0 0 4px;
box-sizing: border-box;
color: #FFFFFF;
cursor: pointer;
display: inline-block;
font-family: din-round,sans-serif;
font-size: 12px;
font-weight: 700;
letter-spacing: .8px;
line-height: 20px;
margin: 0;
outline: none;
overflow: visible;
padding: 8px 12px;
text-align: center;
text-transform: uppercase;
touch-action: manipulation;
transform: translateZ(0);
transition: filter .2s;
user-select: none;
-webkit-user-select: none;
vertical-align: middle;
white-space: nowrap;
width: 102px;
}
.button-19:after {
background-clip: padding-box;
background-color: #1CB0F6;
border: solid transparent;
border-radius: 16px;
border-width: 0 0 4px;
bottom: -4px;
content: "";
left: 0;
position: absolute;
right: 0;
top: 0;
z-index: -1;
}
.button-19:main,
.button-19:focus {
user-select: auto;
}
.button-19:hover:not(:disabled) {
filter: brightness(1.1);
-webkit-filter: brightness(1.1);
}
.button-19:disabled {
cursor: auto;
}
`;
document.head.appendChild(style);
// create overlay element
const overlay = document.createElement("div");
overlay.classList.add("overlay");
const overlayInner = document.createElement("div");
overlayInner.classList.add("overlay__inner");
const overlayContent = document.createElement("div");
overlayContent.classList.add("overlay__content");
const spinner = document.createElement("span");
spinner.classList.add("spinner");
overlayContent.appendChild(spinner);
overlayInner.appendChild(overlayContent);
overlay.appendChild(overlayInner);
// Your code here...
let jspdf = document.createElement("script");
jspdf.src =
"https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js";
document.body.appendChild(jspdf);
const pdfTitle = document.querySelector("title").innerText;
const pageNumber = document.querySelector(".page-numbers.current")?.innerText;
function downloadPdf() {
const { jsPDF } = window.jspdf;
let pdf = new jsPDF();
let elements = document.getElementsByTagName("img");
const pdfWidth = pdf.internal.pageSize.width;
const pdfHeight = pdf.internal.pageSize.height;
for (let i in elements) {
let img = elements[i];
const canvasWidth = img.width;
const canvasHeight = img.height;
if (!/^data:image/.test(img.src)) {
continue;
}
let canvasElement = document.createElement("canvas");
let con = canvasElement.getContext("2d");
const widthRatio = pdfWidth / canvasWidth;
const sWidth = canvasWidth;
const sHeight =
pdfHeight + (pdfHeight - pdfHeight * widthRatio) / widthRatio;
canvasElement.width = sWidth;
canvasElement.height = sHeight;
con.drawImage(img, 0, 0, img.width, img.height);
let imgData = canvasElement.toDataURL("image/png", 1.0);
pdf.addImage(
imgData,
"PNG",
0,
0,
img.width * widthRatio,
sHeight * widthRatio
);
pdf.addPage();
}
pdf.save(`${pdfTitle}-${pageNumber}.pdf`);
}
const right = document.querySelector(".mona-wrap-right");
// const button = document.createElement("button");
// button.innerHTML = "Download";
// button.addEventListener("click",downloadPdf);
const scrollButton = document.createElement("button");
scrollButton.innerHTML = "Download";
// css style button
scrollButton.classList.add("button-19");
scrollButton.addEventListener("click", function () {
const contentEl = document.querySelector("#content-data");
const bookReader = document.querySelector("#book-reader");
/*
setInterval to scroll to bottom
and clean it when it reaches the bottom
check if has class loading-child in element with id book-reader
*/
document.body.appendChild(overlay);
const interval = setInterval(() => {
if (bookReader.classList.contains("loading-child")) {
contentEl.scrollTo(0, contentEl.scrollHeight);
} else {
clearInterval(interval);
downloadPdf();
overlay.remove();
}
}, 1000);
});
// append first child to right el
right.insertBefore(scrollButton, right.firstChild);
// right.appendChild(button);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment