-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
164 lines (134 loc) · 5.27 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// ==UserScript==
// @name DepEd2Tambayan
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Scrape PDF modules from DepEd Tambayan in one click.
// @author Rene Andre Bedonia Jocsing
// @icon https://external-content.duckduckgo.com/ip3/depedtambayan.net.ico
// @match https://depedtambayan.net/*
// @grant GM_registerMenuCommand
// @grant GM_addStyle
// @require https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js
// ==/UserScript==
(function () {
'use strict';
// Add menu entry to Tampermonkey for cleaner UI
GM_registerMenuCommand("Scrape PDF", function(MouseEvent) {
runScriptPipeline();
}, {
accessKey: "f",
autoClose: true
});
(async function() {
await waitForContainer();
const canvasBar = document.querySelector('.pdfemb-toolbar');
// Create a floating button element
const floatingButton = document.createElement("button");
//floatingButton.class.append('pdfemb-download');
floatingButton.textContent = "DOWNLOAD";
floatingButton.style.position = "absolute";
floatingButton.style.top = "0";
floatingButton.style.right = "20px";
floatingButton.style.zIndex = "9999";
// Attach a click event listener to the button
floatingButton.addEventListener("click", runScriptPipeline);
floatingButton.addEventListener("click", () => {
floatingButton.disabled = true; // Disable the button
floatingButton.textContent = "CONGRATS!";
});
// Append the button to the body of the page
canvasBar.appendChild(floatingButton);
})();
async function runScriptPipeline() {
try {
// Wait until the container div is loaded
await waitForContainer();
const images = [];
let hasMorePages = true;
let pageNumber = 1;
while (hasMorePages) {
// Capture the image from the current page
const image = await capturePageImage(pageNumber);
if (image) {
images.push(image);
}
// Check if there are more pages to navigate
hasMorePages = await navigateToNextPage();
pageNumber++;
}
if (images.length === 0) {
alert("No pages found to scrape. Wait for them to load?");
return;
}
// Initialize jsPDF
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
// Add each canvas image to the PDF
images.forEach((imgDataUrl, index) => {
if (index > 0) {
doc.addPage(); // Add a new page after the first one
}
// TODO: Need to fetch image dimensions dynamically, but this seems to work fine for now
doc.addImage(imgDataUrl, 'JPEG', 0, 0, 180, 240);
});
// Get the filename from the entry-title class
const titleElement = document.querySelector('.entry-title');
const filename = titleElement ? titleElement.textContent.trim().toUpperCase().replace(/\s+/g, '_') : 'MODULE';
// Save the generated PDF
doc.save(`${filename}.pdf`);
} catch (err) {
console.error('Error creating PDF:', err);
}
}
async function waitForContainer() {
return new Promise((resolve) => {
const interval = setInterval(() => {
const container = document.querySelector('.pdfemb-pagescontainer.grab-to-pan-grab');
if (container) {
clearInterval(interval);
resolve();
}
}, 1000); // Check every 1 second
});
}
async function capturePageImage(pageNumber) {
const container = document.querySelector('.pdfemb-pagescontainer.grab-to-pan-grab');
if (!container) {
console.error('Container div not found.');
return null;
}
const div = container.querySelector(`.pdfemb-inner-div.pdfemb-page${pageNumber}`);
if (!div) {
console.error('No .pdfemb-inner-div found.');
return null;
}
const canvas = div.querySelector('.pdfemb-the-canvas');
if (!canvas || !isCanvasRendered(canvas)) {
console.warn('Canvas not rendered or found.');
return null;
}
return await getCanvasDataUrl(canvas);
}
function isCanvasRendered(canvas) {
return canvas.width > 0 && canvas.height > 0;
}
function getCanvasDataUrl(canvas) {
const dataUrl = canvas.toDataURL('image/jpeg');
return dataUrl
}
async function navigateToNextPage() {
const nextButton = document.querySelector('.pdfemb-next');
if (!nextButton) {
console.warn('Next button not found.');
return false; // No more pages to navigate
}
const isDisabled = nextButton.classList.contains('pdfemb-btndisabled');
if (isDisabled) {
return false; // No more pages to navigate
}
// Click the next button and wait for the next page to load
nextButton.click();
await new Promise(resolve => setTimeout(resolve, 100)); // Wait for a short time to allow the next page to load
return true;
}
})();