-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoffscreen.js
More file actions
45 lines (42 loc) · 1.59 KB
/
offscreen.js
File metadata and controls
45 lines (42 loc) · 1.59 KB
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
chrome.runtime.onMessage.addListener(async (request) => {
if (request.action === 'createPdfOffscreen') {
const { imageUrls, mangaTitle, chapter } = request;
const { jsPDF } = window.jspdf;
const pdf = new jsPDF();
for (let i = 0; i < imageUrls.length; i++) {
const dataUrl = imageUrls[i];
await new Promise(resolve => {
const img = new Image();
img.src = dataUrl;
img.onload = function () {
const pageWidth = pdf.internal.pageSize.getWidth();
const pageHeight = pdf.internal.pageSize.getHeight();
const ratio = Math.min(pageWidth / img.width, pageHeight / img.height);
const imgWidth = img.width * ratio;
const imgHeight = img.height * ratio;
const x = (pageWidth - imgWidth) / 2;
const y = (pageHeight - imgHeight) / 2;
if (i > 0) {
pdf.addPage();
}
// The format (e.g., 'PNG', 'JPEG') is part of the data URL, but jsPDF can handle it.
pdf.addImage(img, 'PNG', x, y, imgWidth, imgHeight);
resolve();
}
img.onerror = () => resolve(); // Continue if an image fails to load
});
}
const pdfBlob = pdf.output('blob');
const pdfFilename = `${mangaTitle} - ${chapter.name}.pdf`;
const reader = new FileReader();
reader.onload = function () {
// Send the data URL back to the service worker to handle the download
chrome.runtime.sendMessage({
action: 'downloadPdf',
url: reader.result,
filename: pdfFilename
});
};
reader.readAsDataURL(pdfBlob);
}
});