-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript
More file actions
275 lines (250 loc) · 9.14 KB
/
script
File metadata and controls
275 lines (250 loc) · 9.14 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// ==UserScript==
// @name 小宇宙FM音频下载器
// @namespace http://tampermonkey.net/
// @version 1.1
// @description 为小宇宙FM添加MP3音频下载功能,支持进度显示和格式验证
// @author Zane
// @match https://www.xiaoyuzhoufm.com/episode/*
// @grant GM_download
// @grant GM_xmlhttpRequest
// @license MIT
// ==/UserScript==
(function() {
'use strict';
// 添加下载按钮的样式
const style = document.createElement('style');
style.textContent = `
.xiaoyuzhou-download-btn {
background-color: #1890ff;
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
margin: 10px;
font-size: 14px;
display: flex;
align-items: center;
gap: 5px;
transition: all 0.3s ease;
}
.xiaoyuzhou-download-btn:hover {
background-color: #40a9ff;
transform: translateY(-1px);
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.xiaoyuzhou-download-btn.searching {
background-color: #f0f0f0;
color: #666;
cursor: default;
}
.xiaoyuzhou-download-btn.error {
background-color: #ff4d4f;
}
.xiaoyuzhou-download-btn i {
font-size: 16px;
}
.xiaoyuzhou-download-status {
position: fixed;
top: 20px;
right: 20px;
padding: 15px 20px;
background: #1890ff;
color: white;
border-radius: 8px;
z-index: 9999;
box-shadow: 0 2px 8px rgba(0,0,0,0.15);
display: flex;
align-items: center;
gap: 8px;
animation: slideIn 0.3s ease;
}
@keyframes slideIn {
from {
transform: translateX(100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
`;
document.head.appendChild(style);
// 主函数
function init() {
console.log('初始化小宇宙FM下载器...');
// 避免重复添加按钮
const existingButton = document.querySelector('.xiaoyuzhou-download-btn');
if (existingButton) {
existingButton.remove();
}
// 创建状态显示按钮
const statusButton = document.createElement('button');
statusButton.className = 'xiaoyuzhou-download-btn searching';
statusButton.innerHTML = `
<i>🔍</i>
<span>正在查找音频...</span>
`;
// 插入到标题下方
const title = document.querySelector('.jsx-399326063.title') ||
document.querySelector('.episode-title');
if (title) {
title.parentNode.insertBefore(statusButton, title.nextSibling);
}
let attempts = 0;
const maxAttempts = 5;
const checkInterval = setInterval(() => {
attempts++;
statusButton.innerHTML = `
<i>🔍</i>
<span>正在查找音频...${attempts}/${maxAttempts}</span>
`;
const audioElement = document.querySelector('audio');
if (audioElement) {
clearInterval(checkInterval);
statusButton.innerHTML = `
<i>⬇️</i>
<span>下载MP3音频</span>
`;
statusButton.style.backgroundColor = '#1890ff';
statusButton.style.color = 'white';
statusButton.style.cursor = 'pointer';
statusButton.classList.remove('searching');
statusButton.addEventListener('click', () => {
if (audioElement.src) {
downloadAudio(audioElement.src);
} else {
const audioUrl = findAudioUrlFromPage();
if (audioUrl) {
downloadAudio(audioUrl);
} else {
showNotification('未能找到音频文件,请确保音频已开始播放', 'error');
}
}
});
}
if (attempts >= maxAttempts) {
clearInterval(checkInterval);
statusButton.innerHTML = `
<i>⚠️</i>
<span>未找到音频,请先播放</span>
`;
statusButton.classList.add('error');
}
}, 1000);
}
// 显示通知
function showNotification(message, type = 'info') {
const notification = document.createElement('div');
notification.className = 'xiaoyuzhou-download-status';
notification.style.backgroundColor = type === 'error' ? '#ff4d4f' : '#1890ff';
notification.innerHTML = `
<i>${type === 'error' ? '⚠️' : 'ℹ️'}</i>
<span>${message}</span>
`;
document.body.appendChild(notification);
setTimeout(() => {
notification.style.animation = 'slideIn 0.3s ease reverse';
setTimeout(() => document.body.removeChild(notification), 300);
}, 3000);
}
// 从页面数据中查找音频URL
function findAudioUrlFromPage() {
const scripts = document.getElementsByTagName('script');
for (const script of scripts) {
const content = script.textContent;
if (content && content.includes('audioUrl')) {
const match = content.match(/"audioUrl":"([^"]+)"/);
if (match && match[1]) {
return match[1];
}
}
}
return null;
}
// 下载音频文件
function downloadAudio(audioUrl) {
if (!audioUrl) {
showNotification('未找到音频文件', 'error');
return;
}
// 检查音频URL的格式
if (!audioUrl.includes('.mp3')) {
console.log('音频链接不是MP3格式:', audioUrl);
showNotification('当前音频不是MP3格式,无法下载', 'error');
return;
}
// 获取播客标题作为文件名
const title = document.querySelector('.episode-title')?.textContent ||
document.title ||
'小宇宙FM音频';
const fileName = `${title}.mp3`.replace(/[\\/:*?"<>|]/g, '_');
// 显示下载状态
const downloadStatus = document.createElement('div');
downloadStatus.className = 'xiaoyuzhou-download-status';
downloadStatus.innerHTML = `
<i>⏬</i>
<span>正在下载MP3...0%</span>
`;
document.body.appendChild(downloadStatus);
// 使用GM_xmlhttpRequest下载文件
GM_xmlhttpRequest({
method: 'GET',
url: audioUrl,
responseType: 'blob',
headers: {
'Accept': 'audio/mpeg',
},
onprogress: (progress) => {
if (progress.total) {
const percent = Math.round((progress.loaded / progress.total) * 100);
downloadStatus.innerHTML = `
<i>⏬</i>
<span>正在下载MP3...${percent}%</span>
`;
}
},
onload: (response) => {
const contentType = response.response.type;
if (!contentType.includes('audio/mpeg') && !contentType.includes('audio/mp3')) {
showNotification('下载失败:文件格式不是MP3', 'error');
document.body.removeChild(downloadStatus);
return;
}
const blob = new Blob([response.response], { type: 'audio/mpeg' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
document.body.removeChild(downloadStatus);
showNotification('MP3下载完成!');
}, 1000);
console.log('MP3下载完成');
},
onerror: (error) => {
console.error('下载失败:', error);
showNotification('下载失败,请重试', 'error');
document.body.removeChild(downloadStatus);
}
});
}
// 监听URL变化,支持SPA
let lastUrl = location.href;
new MutationObserver(() => {
const url = location.href;
if (url !== lastUrl) {
lastUrl = url;
if (url.includes('xiaoyuzhoufm.com/episode/')) {
init();
}
}
}).observe(document, {subtree: true, childList: true});
// 初始化
init();
})();