-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjc.js
137 lines (121 loc) · 4.68 KB
/
jc.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// ==UserScript==
// @name 智慧教育教材PDF链接复制
// @namespace http://github.com/byhooi
// @version 1.0
// @description 复制智慧教育平台教材PDF的直接下载链接
// @match https://basic.smartedu.cn/tchMaterial/*
// @grant GM_setClipboard
// @downloadURL https://raw.githubusercontent.com/byhooi/JS/master/jc.js
// @updateURL https://raw.githubusercontent.com/byhooi/JS/master/jc.js
// ==/UserScript==
(function() {
'use strict';
// 创建复制按钮
const copyButton = document.createElement('button');
copyButton.textContent = '复制PDF链接';
copyButton.style.position = 'fixed';
copyButton.style.top = '10px';
copyButton.style.right = '10px';
copyButton.style.zIndex = '9999';
copyButton.style.padding = '10px 15px';
copyButton.style.backgroundColor = '#4CAF50';
copyButton.style.color = 'white';
copyButton.style.border = 'none';
copyButton.style.borderRadius = '5px';
copyButton.style.cursor = 'pointer';
copyButton.style.display = 'none'; // 初始状态为隐藏
copyButton.style.transition = 'all 0.3s ease';
// 修改复制功能
copyButton.addEventListener('click', function() {
const decodedLink = extractDirectPDFLink(pdfLink);
GM_setClipboard(decodedLink);
console.log('PDF直接链接已复制到剪贴板');
copyButton.textContent = '已复制';
copyButton.style.backgroundColor = '#333';
setTimeout(() => {
copyButton.style.display = 'none';
copyButton.textContent = '复制PDF链接';
copyButton.style.backgroundColor = '#4CAF50';
}, 2000);
});
let pdfLink = '';
// 在XHR拦截代码中更新pdfLink
const originalXHR = window.XMLHttpRequest;
window.XMLHttpRequest = function() {
const xhr = new originalXHR();
const originalOpen = xhr.open;
xhr.open = function() {
console.log('拦截到XHR请求:', arguments[1]);
const potentialPDFLink = extractPDFLink(arguments[1]);
if (potentialPDFLink.includes('.pdf')) {
pdfLink = potentialPDFLink;
console.log('找到PDF链接:', pdfLink);
copyButton.style.display = 'block';
}
originalOpen.apply(this, arguments);
};
return xhr;
};
// 修改Fetch API拦截部分
const originalFetch = window.fetch;
window.fetch = function() {
console.log('拦截到Fetch请求:', arguments[0]);
const potentialPDFLink = extractPDFLink(arguments[0]);
if (potentialPDFLink.includes('.pdf')) {
pdfLink = potentialPDFLink;
console.log('找到PDF链接 (Fetch):', pdfLink);
copyButton.style.display = 'block';
}
return originalFetch.apply(this, arguments);
};
document.body.appendChild(copyButton);
function findPDFLinkInDOM() {
const links = document.querySelectorAll('a[href*=".pdf"], iframe[src*=".pdf"], embed[src*=".pdf"], object[data*=".pdf"]');
for (const link of links) {
const href = link.href || link.src || link.data;
if (href) {
pdfLink = decodeURIComponent(href);
console.log('在DOM中找到PDF链接:', pdfLink);
copyButton.style.display = 'block';
return true;
}
}
return false;
}
let checkCount = 0;
const maxChecks = 10;
const checkInterval = setInterval(() => {
checkCount++;
console.log(`第${checkCount}次检查PDF链接`);
if (pdfLink || findPDFLinkInDOM() || checkCount >= maxChecks) {
clearInterval(checkInterval);
if (!pdfLink) {
console.log('未能找到PDF链接');
alert('未能找到PDF链接,请刷新页面后重试');
}
}
}, 2000);
window.addEventListener('load', function() {
if (!pdfLink) {
console.log('页面加载完成,尝试从DOM中查找PDF链接');
findPDFLinkInDOM();
}
});
})();
function extractPDFLink(url) {
if (url.includes('viewer.html?file=')) {
return decodeURIComponent(url.split('viewer.html?file=')[1].split('&')[0]);
}
return url;
}
function extractDirectPDFLink(url) {
// 移除 -private 部分
url = url.replace(/-private/g, '');
// 如果链接包含 "viewer.html?file=",提取实际的PDF链接
if (url.includes('viewer.html?file=')) {
url = decodeURIComponent(url.split('viewer.html?file=')[1].split('&')[0]);
}
// 移除链接中的 headers 参数
url = url.split('&headers=')[0];
return url;
}