-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.user.js
78 lines (71 loc) · 2.74 KB
/
script.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
// ==UserScript==
// @name 畅课平台资源下载
// @namespace https://bobliu.tech/
// @version 1.1.1
// @license MIT
// @supportURL https://github.com/BobLiu0518/TronClass-Resource-Download/issues
// @description 下载畅课(一网畅学)平台的课程资源,即使老师设置了不可下载~
// @author BobLiu
// @match https://*/course/*/learning-activity*
// @icon https://tronclass.com.cn/favicon.ico
// @grant none
// ==/UserScript==
(async function () {
'use strict';
let host = window.location.origin;
let activityId = window.location.hash.replace('#/', '');
let res = await fetch(
`${host}/api/activities/${activityId}/upload_references`
);
let resources = (await res.json()).references;
if (!resources.length) {
console.log('No resources found.');
return;
}
for (let id in resources) {
let resource = resources[id];
console.log(`[${parseInt(id) + 1}] ${resource.name}`);
}
console.log('Execute downloadResource(n) to download!');
window.downloadResource = function (i) {
i = parseInt(i) - 1;
console.log(`Downloading ${resources[i].name}...`);
let downloadUrl = `${host}/api/uploads/reference/${resources[i].id}/blob`;
window.open(downloadUrl, '_blank');
};
let inject = async function (retry) {
if (typeof retry == 'object') retry = 0;
let dom = document.getElementsByClassName('attachment-body')[0];
if (!dom) {
if (retry < 5) {
console.log('Cannot get file list dom, retrying in 1s...');
setTimeout(() => inject(retry + 1), 1000);
} else {
console.log('Cannot get file list dom, inject button failed.');
}
return;
}
let fileList = dom.children;
for (let row of fileList) {
let filename = row.children[0].children[0].textContent
.replaceAll(/\s*\n\s*/g, '')
.trim();
for (let i in resources) {
let resource = resources[i];
if (filename == resource.name) {
let downloadBtn = document.createElement('button');
downloadBtn.textContent = '下载';
downloadBtn.onclick = function (event) {
event.preventDefault();
event.stopPropagation();
window.downloadResource(parseInt(i) + 1);
};
row.children[0].appendChild(downloadBtn);
break;
}
}
}
console.log('Download button injected.');
};
addEventListener('load', inject);
})();