-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartEduCNTextbookDownloader.user.js
More file actions
99 lines (85 loc) · 3.99 KB
/
SmartEduCNTextbookDownloader.user.js
File metadata and controls
99 lines (85 loc) · 3.99 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
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
// ==UserScript==
// @name 国家中小学智慧教育平台 - 获取教材PDF直链
// @version 0.1.1
// @description 在教材详情页添加一个按钮,提取当前教材的PDF直链并带上Token跳转。
// @author zeroHYH
// @match https://basic.smartedu.cn/tchMaterial/detail*
// @icon https://basic.smartedu.cn/favicon.ico
// @grant none
// @downloadURL https://raw.githubusercontent.com/zeroHYH/SmartEduCNTextbookDownloader/main/SmartEduCNTextbookDownloader.user.js
// @updateURL https://raw.githubusercontent.com/zeroHYH/SmartEduCNTextbookDownloader/main/SmartEduCNTextbookDownloader.user.js
// ==/UserScript==
(function () {
'use strict';
// 创建并添加按钮的函数
function initButton() {
// 如果按钮已经存在,则不重复添加
if (document.getElementById('xedu-pdf-btn')) return;
const btn = document.createElement('button');
btn.id = 'xedu-pdf-btn';
btn.textContent = '获取PDF直链';
// 设置按钮样式 (悬浮在页面右下角)
btn.style.position = 'fixed';
btn.style.bottom = '100px';
btn.style.right = '30px';
btn.style.zIndex = '999999';
btn.style.padding = '12px 20px';
btn.style.backgroundColor = '#1890ff';
btn.style.color = 'white';
btn.style.border = 'none';
btn.style.borderRadius = '8px';
btn.style.fontSize = '14px';
btn.style.fontWeight = 'bold';
btn.style.cursor = 'pointer';
btn.style.boxShadow = '0 4px 12px rgba(0,0,0,0.15)';
btn.style.transition = 'background-color 0.3s';
// 鼠标悬浮效果
btn.onmouseover = () => btn.style.backgroundColor = '#40a9ff';
btn.onmouseout = () => btn.style.backgroundColor = '#1890ff';
// 绑定点击事件
btn.addEventListener('click', () => {
try {
// 1. 获取 iframe 的 src 属性
const pdfPlayer = document.querySelector("#pdfPlayerFirefox");
if (!pdfPlayer || !pdfPlayer.src) {
alert("未找到PDF播放器,请等待教材加载完毕。");
return;
}
// 2. 解析出 file 参数 (使用标准的 new URL)
const fileUrl = new URL(pdfPlayer.src).searchParams.get("file");
if (!fileUrl) {
alert("未能从播放器提取到 file 参数!");
return;
}
// 3. 从 localStorage 获取并解析 Token
const storageKey = "ND_UC_AUTH-e5649925-441d-4a53-b525-51a2f1c4e0a8&ncet-xedu&token";
const storageStr = window.localStorage.getItem(storageKey);
if (!storageStr) {
alert("未找到登录凭证(Token),请确认您是否已登录!");
return;
}
// 按照要求进行两次 JSON parse
const firstParse = JSON.parse(storageStr);
const secondParse = JSON.parse(firstParse.value);
const accessToken = secondParse.access_token;
// 4. 拼接最终链接并跳转 (在新标签页打开)
const finalUrl = `${fileUrl}?accessToken=${accessToken}`;
window.open(finalUrl, '_blank');
} catch (error) {
console.error("解析链接失败:", error);
alert("解析失败,请按F12打开控制台查看具体报错信息。可能是登录状态失效。");
}
});
document.body.appendChild(btn);
}
// 由于网站可能是单页面应用(SPA),使用定时器检测页面并加载按钮
setInterval(() => {
if (location.href.includes('/tchMaterial/detail')) {
initButton();
} else {
// 如果离开了详情页,移除按钮
const btn = document.getElementById('xedu-pdf-btn');
if (btn) btn.remove();
}
}, 1000);
})();