-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
ggdrive_downloadDoc.js
96 lines (84 loc) · 2.96 KB
/
ggdrive_downloadDoc.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
export default {
icon: "https://drive-thirdparty.googleusercontent.com/32/type/application/vnd.google-apps.document",
name: {
en: "GG Drive - Download Document (to PDF)",
vi: "GG Drive - Tải Document (sang PDF)",
},
description: {
en: "Download google drive Document file that dont have download button. Pages will be convert to PDF, cannot edit.",
vi: "Tải file Doc không có nút download trên google drive. Tải về định dạng PDF, không thể sửa nội dung.",
},
whiteList: ["https://docs.google.com/document/*"],
onClick: () => {
UfsGlobal.DOM.injectScriptSrc(
"https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js",
async (success, error) => {
if (!success) {
alert("Inject FAILED: " + error);
return;
}
const { jsPDF } = window.jspdf || {};
if (!jsPDF) {
alert("jspdf not found");
return;
}
const [currentPage, totalPage] =
document
.querySelector(".jfk-tooltip-contentId")
?.textContent?.split(" / ") ?? [];
const sleep = (ms = 0) =>
new Promise((resolve) => setTimeout(resolve, ms));
// scroll to first page
const contanier = document.querySelector(
".kix-rotatingtilemanager.docs-ui-hit-region-surface"
);
contanier?.scrollIntoView?.({ block: "start", behavior: "instant" });
await sleep(500);
// get first page
let canvas = document.querySelector("canvas");
if (!canvas) {
alert("Docs page not found");
return;
}
let pdf;
while (canvas) {
// scroll current page into view => to load next page
canvas.scrollIntoView({ block: "start", behavior: "instant" });
await sleep(500);
// capture canvas page to pdf
if (canvas.width && canvas.height) {
let imgData = canvas.toDataURL("image/jpeg", 1.0);
if (!pdf) {
let ori = canvas.width > canvas.height ? "l" : "p";
pdf = new jsPDF({
orientation: ori,
unit: "px",
format: [canvas.width, canvas.height],
hotfixes: ["px_scaling"],
});
}
pdf.addImage(imgData, "JPEG", 0, 0);
}
// query next canvas
let listCanvas = Array.from(document.querySelectorAll("canvas"));
let nextCanvas = null;
for (let c of listCanvas) {
if (
c != canvas &&
c.getBoundingClientRect().top > canvas.getBoundingClientRect().top
) {
nextCanvas = c;
break;
}
}
canvas = nextCanvas;
// prepare next pdf page
if (canvas) {
pdf.addPage([canvas.width, canvas.height]);
}
}
pdf.save((document.title || "download") + ".pdf");
}
);
},
};