-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.user.js
123 lines (117 loc) · 4.98 KB
/
main.user.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
// ==UserScript==
// @name 国家中小学智慧教育平台电子课本下载
// @namespace https://github.com/amakerlife
// @version 1.4.1
// @description 在国家中小学智慧教育平台网站中添加电子课本下载按钮,免登录下载电子课本
// @author Makerlife
// @match https://*.smartedu.cn/tchMaterial/detail*
// @match https://*.smartedu.cn/elecedu/detail*
// @match https://www.zxx.edu.cn/tchMaterial/detail*
// @icon https://basic.smartedu.cn/favicon.ico
// @license MIT
// @grant GM_download
// @compatible Chrome
// @compatible Firefox
// @compatible Edge
// @compatible Safari
// @require https://greasyfork.org/scripts/425166-elegant-alert-%E5%BA%93/code/elegant%20alert()%E5%BA%93.js?version=922763
// @require https://lib.baomitu.com/sweetalert2/11.7.22/sweetalert2.all.min.js
// ==/UserScript==
(function() {
'use strict';
var checkUrls = async function(urls, id, fileName) {
for (let url of urls) {
url = url.replace('ndr-private', 'ndr'); // 替换 "ndr-private" 为 "ndr"
try {
let response = await fetch(url, { method: 'HEAD' });
if (response.status === 200) {
localStorage.setItem(`validUrl_${id}`, url);
Swal.fire({
title: '下载选项',
text: "请选择下载方式",
icon: 'question',
showCancelButton: true,
confirmButtonText: '直接下载',
cancelButtonText: '在新标签页中打开'
}).then((result) => {
if (result.isConfirmed) {
GM_download({
url: url,
name: `${fileName}.pdf`,
});
} else if (result.dismiss === Swal.DismissReason.cancel) {
window.open(url, '_blank');
}
});
return;
}
} catch (error) {
console.error(`Failed to fetch ${url}:`, error);
}
}
alert('All URLs are invalid!');
};
var main = async function() {
var url = window.location.href;
var regex = /[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/g;
var match = regex.exec(url);
if (match) {
var id = match[0];
console.log(`ContentID: ${id}`);
var fileNameElement = document.querySelector("span.fish-breadcrumb-link");
var fileName = fileNameElement ? fileNameElement.innerText : "电子课本";
var savedUrl = localStorage.getItem(`validUrl_${id}`);
if (savedUrl) {
console.log(`Using saved URL: ${savedUrl}`);
Swal.fire({
title: '下载选项',
text: "请选择下载方式",
icon: 'question',
showCancelButton: true,
confirmButtonText: '直接下载',
cancelButtonText: '在新标签页中打开'
}).then((result) => {
if (result.isConfirmed) {
GM_download({
url: savedUrl,
name: `${fileName}.pdf`,
});
} else if (result.dismiss === Swal.DismissReason.cancel) {
window.open(savedUrl, '_blank');
}
});
return;
}
// 遍历 x=1 到 x=3 的三个链接
for (let x = 1; x <= 3; x++) {
let jsonUrl = `https://s-file-${x}.ykt.cbern.com.cn/zxx/ndrv2/resources/tch_material/details/${id}.json`;
try {
let response = await fetch(jsonUrl);
if (response.ok) {
let data = await response.json();
let tiItems = data.ti_items || [];
for (let item of tiItems) {
if (item.ti_file_flag === "source") {
let urls = item.ti_storages || [];
checkUrls(urls, id, fileName);
return;
}
}
}
} catch (error) {
console.error(`Failed to fetch ${jsonUrl}:`, error);
}
}
} else {
console.log("No ContentID Found!");
}
};
let init = setInterval(function() {
let fileNameElement = document.querySelector("span.fish-breadcrumb-link");
if (fileNameElement) {
clearInterval(init);
new ElegantAlertBox("正在解析,即将开始下载");
main();
}
}, 1000);
})();