// ==UserScript== // @name JS 下载云听电台往期音频 再优化 // @namespace https://www.radio.cn/ // @version 1.4 // @description 一键下载当前页面所有录音文件 // @match https://www.radio.cn/* // @grant none // @license MIT // @downloadURL none // ==/UserScript== (function() { 'use strict'; function formatDate(dateStr) { const date = new Date(dateStr); const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); return year + '-' + (month < 10 ? '0' : '') + month + '-' + (day < 10 ? '0' : '') + day; } function downloadAll() { const downloadLinks = []; for (const a of document.getElementsByTagName('a')) { if (!a.hasAttribute('onclick')) continue; const onclick = a.getAttribute('onclick'); const matches = onclick.match(/downLiveRecord\(['"](.*?)['"]/); if (matches) { let href = matches[1].replace(/\[|\]|"/g, ''); const dateMatch = href.match(/\/(\d{4})(\d{2})(\d{2})\//); if (dateMatch) { const dateStr = dateMatch[1] + '-' + dateMatch[2] + '-' + dateMatch[3]; const fileName = formatDate(dateStr) + '.mp3'; if (href.startsWith('https://')) { downloadLinks.push({href: href, fileName: fileName}); } else { href = 'https://' + href.replace(/^http:\/\//i, ''); downloadLinks.push({href: href, fileName: fileName}); } } } } downloadLinks.sort(function(a, b){ return a.fileName.localeCompare(b.fileName); }); const progressEl = document.createElement('div'); progressEl.style.position = 'fixed'; progressEl.style.bottom = '100px'; progressEl.style.right = '50px'; progressEl.style.padding = '10px 20px'; progressEl.style.fontSize = '16px'; progressEl.style.borderRadius = '5px'; progressEl.style.color = '#fff'; progressEl.style.backgroundColor = '#f00'; progressEl.style.cursor = 'default'; document.body.appendChild(progressEl); let downloadCount = 0; function downloadNext() { if (downloadCount >= downloadLinks.length) { progressEl.textContent = '爬取进度:' + downloadCount + '/' + downloadLinks.length + ',全部完成'; return; } const link = downloadLinks[downloadCount]; const xhr = new XMLHttpRequest(); xhr.open('GET', link.href); xhr.responseType = 'blob'; xhr.onload = function() { if (xhr.status === 200) { downloadCount++; const blob = new Blob([xhr.response], {type: 'audio/mp3'}); URL.revokeObjectURL(link.href); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = link.fileName; document.body.appendChild(a); a.click(); a.remove(); progressEl.textContent = '爬取进度:' + downloadCount + '/' + downloadLinks.length; setTimeout(downloadNext, 500); } else { console.log(xhr.statusText); setTimeout(downloadNext, 500); } }; xhr.onerror = function() { console.log(xhr.statusText); setTimeout(downloadNext, 500); }; xhr.send(); } downloadNext(); } const button = document.createElement('button'); button.textContent = "下载当前页面所有录音文件"; button.style.position = 'fixed'; button.style.bottom = '50px'; button.style.right = '50px'; button.style.padding = '10px 20px'; button.style.fontSize = '16px'; button.style.borderRadius = '5px'; button.style.color = '#fff'; button.style.backgroundColor = '#f00'; button.style.cursor = 'pointer'; button.addEventListener('click', function() { downloadAll(); }); document.body.appendChild(button); })();