-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYuKeTang_Print2PDF_v2.js
92 lines (87 loc) · 3.16 KB
/
YuKeTang_Print2PDF_v2.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
// ==UserScript==
// @name 雨课堂课件导出为pdf(无后端版本)
// @namespace http://tampermonkey.net/
// @version 2
// @description 雨课堂课件ppt导出为pdf,详见https://github.com/ShevonKuan/yuektang_ppt2pdf
// @author shevon
// @match https://changjiang.yuketang.cn/web/print
// @require https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.debug.js
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function () {
"use strict";
setTimeout(function () {
console.log("开始执行");
}, 3000);
$(".controls-btns").append(
'<p class="print fuckyuketang2" title="打印">导出课件</p>'
);
// loadImage
function loadImage(url) {
return new Promise((resolve, reject) => {
var img = new Image();
var data;
img.setAttribute("crossOrigin", "Anonymous");
img.src = url;
img.onError = function () {
throw new Error('Cannot load image: "' + url + '"');
};
img.onload = function () {
var canvas = document.createElement("canvas");
document.body.appendChild(canvas);
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
// Grab the image as a jpeg encoded in base64, but only the data
data = canvas
.toDataURL("image/jpeg")
.slice("data:image/jpeg;base64,".length);
// Convert the data to binary form
data = atob(data);
document.body.removeChild(canvas);
resolve(data);
};
});
}
async function download() {
var imgData = new Array();
var imgList = new Array();
$(".fuckyuketang2").html("正在获取图片信息");
$.each($("img.pptimg"), function () {
imgList.push($(this).attr("src"));
});
for (var i = 0; i < imgList.length; i++) {
var link = imgList[i];
$(".fuckyuketang2").html("获取第" + i + "张图片");
await loadImage(link).then((data) => {
imgData.push(data);
});
}
var height = $("img.pptimg")[0].height;
var width = $("img.pptimg")[0].width;
$(".fuckyuketang2").html("创建pdf");
var doc = new jsPDF({
orientation: "landscape",
unit: "px",
format: [width, height],
});
const output = $(document).attr("title") + ".pdf";
let idx = 0;
imgData.forEach((e) => {
idx++;
$(".fuckyuketang2").html("合并第" + idx + "张图片");
doc.addImage(e, "JPG", 0, 0, width, height);
if (idx < imgData.length) {
doc.addPage();
}
});
$(".fuckyuketang2").html("导出课件");
doc.save(output);
// };
}
$(".fuckyuketang2").on("click", function () {
download();
});
})();