// ==UserScript== // @name OSHO World · Auto Paginate Downloader // @namespace https://oshoworld.com/ // @version 3.0 // @description Auto-download MP3s page by page. Clicks "Next" automatically until the last page. // @author BROK // @match https://oshoworld.com/* // @match https://www.oshoworld.com/* // @grant GM_download // @run-at document-idle // ==/UserScript== (function () { 'use strict'; const CFG = { autoScanDelay: 1000, betweenPagesDelay: 4000, // 下载触发后等多久再跳转 maxPages: 200, // 安全上限 storageKey: 'osho_autopaginate_state' }; // ============ STATE (跨页面) ============ function loadState() { try { const raw = sessionStorage.getItem(CFG.storageKey); return raw ? JSON.parse(raw) : null; } catch (e) { return null; } } function saveState(s) { sessionStorage.setItem(CFG.storageKey, JSON.stringify(s)); } function clearState() { sessionStorage.removeItem(CFG.storageKey); } let state = loadState(); // null = 未启动;{active:true, page:1, downloaded:[], visited:[]} let isRunning = false; let mp3List = []; // ============ HELPERS ============ const sleep = ms => new Promise(r => setTimeout(r, ms)); const esc = t => { const d = document.createElement('div'); d.appendChild(document.createTextNode(String(t))); return d.innerHTML; }; function filenameFromUrl(url) { try { const u = new URL(url, location.href); return decodeURIComponent(u.pathname.split('/').pop() || 'audio.mp3'); } catch (e) { return 'audio.mp3'; } } function cleanName(f) { return f.replace(/\.mp3$/i, '').replace(/[_-]+/g, ' ').replace(/\s+/g, ' ').trim(); } // ============ SCAN ============ function scan() { const found = new Set(); document.querySelectorAll('a[href]').forEach(a => { const h = a.href; if (/\.mp3(\?|$)/i.test(h)) found.add(h); }); document.querySelectorAll('audio[src], audio source[src]').forEach(el => { const s = el.src || el.getAttribute('src'); if (s && /\.mp3(\?|$)/i.test(s)) found.add(new URL(s, location.href).href); }); // 裸 URL in HTML const html = document.documentElement.innerHTML; const re = /https?:\/\/[^\s"'<>()\\]+?\.mp3(?:\?[^\s"'<>()\\]*)?/gi; let m; while ((m = re.exec(html)) !== null) found.add(m[0].replace(/\\\//g, '/')); // 相对路径 const relRe = /["']([^"']*?\/wp-content\/uploads\/[^"']*?\.mp3)["']/gi; while ((m = relRe.exec(html)) !== null) { try { found.add(new URL(m[1], location.href).href); } catch (e) {} } mp3List = Array.from(found).sort().map(url => ({ url, name: filenameFromUrl(url) })); return mp3List; } // ============ NEXT PAGE DETECTION ============ function findNextPage() { // 多种策略,按优先级 const candidates = [ // rel=next ...document.querySelectorAll('a[rel="next"]'), // 常见类名 ...document.querySelectorAll('a.next, a.next-page, a.pagination-next, .pagination a:last-child, .nav-next a'), // 文字匹配 ...Array.from(document.querySelectorAll('a')).filter(a => { const t = (a.textContent || '').trim().toLowerCase(); const t2 = (a.getAttribute('aria-label') || '').toLowerCase(); const t3 = (a.getAttribute('title') || '').toLowerCase(); return /^(next|next page|next →|→|›|»|下一页|下一頁)$/.test(t) || /next/.test(t2) || /next/.test(t3); }) ]; for (const a of candidates) { const href = a.href; if (!href) continue; if (!href.startsWith(location.origin)) continue; // 同站 if (href === location.href) continue; // 不是自己 if (/#/.test(href)) continue; // 跳过锚点 // 避免误判"首页"等导航链接 const path = new URL(href).pathname; if (path === '/' || path === '') continue; return href; } return null; } // ============ DOWNLOAD ============ function downloadOne(item) { return new Promise(resolve => { try { if (typeof GM_download === 'function') { GM_download({ url: item.url, name: item.name, onload: () => resolve(true), onerror: () => { fallbackAnchor(item); resolve(false); }, ontimeout: () => { fallbackAnchor(item); resolve(false); } }); } else { fallbackAnchor(item); resolve(true); } } catch (e) { fallbackAnchor(item); resolve(false); } }); } function fallbackAnchor(item) { const a = document.createElement('a'); a.href = item.url; a.download = item.name; a.style.display = 'none'; document.body.appendChild(a); a.click(); setTimeout(() => a.remove(), 100); } // ============ AUTO PAGINATE ============ async function runAutoPaginate() { if (!state) { state = { active: true, page: 1, downloaded: [], visited: [location.href] }; saveState(state); console.log('[AutoPaginate] started'); } isRunning = true; updateButtons(); const currentPage = state.page; setStatus(`Page ${currentPage} · scanning...`, 'loading'); // 1. 扫描 await sleep(300); scan(); renderList(); setStatus(`Page ${currentPage} · found ${mp3List.length} MP3`, 'loading'); // 2. 下载本页(跳过已下载的) let newCount = 0; for (const item of mp3List) { if (state.downloaded.includes(item.url)) continue; setStatus(`Page ${currentPage} · downloading ${cleanName(item.name)}`, 'loading'); await downloadOne(item); state.downloaded.push(item.url); saveState(state); newCount++; await sleep(800); } setStatus(`Page ${currentPage} · downloaded ${newCount} new MP3`, 'success'); // 3. 找下一页 await sleep(CFG.betweenPagesDelay); // 给浏览器时间开始下载 const next = findNextPage(); if (!next) { setStatus(`Done · last page reached. Total ${state.downloaded.length} MP3 downloaded.`, 'success'); state.active = false; saveState(state); isRunning = false; updateButtons(); return; } if (state.visited.includes(next)) { setStatus(`Stopped · next page already visited (loop detected).`, 'error'); state.active = false; saveState(state); isRunning = false; updateButtons(); return; } if (state.page >= CFG.maxPages) { setStatus(`Stopped · max pages (${CFG.maxPages}) reached.`, 'error'); state.active = false; saveState(state); return; } // 4. 跳转 state.page += 1; state.visited.push(next); saveState(state); setStatus(`Page ${currentPage} done · going to page ${state.page}...`, 'loading'); await sleep(1000); location.href = next; } // ============ UI ============ let panel, listEl, statusEl, countEl, btnAuto, btnStop, btnResume, btnClear; function buildPanel() { panel = document.createElement('div'); panel.id = 'osho-ap'; panel.innerHTML = `