// ==UserScript== // @name OSHO World · Cross-Page Collector // @namespace https://oshoworld.com/ // @version 1.0 // @description Manually paginate, collect MP3 URLs across pages, download all at once. // @author BROK // @match https://oshoworld.com/* // @match https://www.oshoworld.com/* // @grant GM_download // @run-at document-idle // ==/UserScript== (function () { 'use strict'; const CFG = { storageKey: 'osho_collector_v1', batchDelayMin: 1200, batchDelayMax: 2800, maxBatch: 100 }; 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 { return decodeURIComponent(new URL(url, location.href).pathname.split('/').pop() || 'audio.mp3'); } catch (e) { return 'audio.mp3'; } } function cleanName(f) { return f.replace(/\.mp3$/i, '').replace(/[_-]+/g, ' ').replace(/\s+/g, ' ').trim(); } // ============ STATE ============ function load() { try { return JSON.parse(sessionStorage.getItem(CFG.storageKey) || 'null') || { urls: [], page: 1 }; } catch (e) { return { urls: [], page: 1 }; } } function save(s) { sessionStorage.setItem(CFG.storageKey, JSON.stringify(s)); } let store = load(); let currentPageMp3 = []; let downloading = false; let stopFlag = false; // ============ SCAN ============ function scan() { const found = new Set(); document.querySelectorAll('a[href]').forEach(a => { if (/\.mp3(\?|$)/i.test(a.href)) found.add(a.href); }); 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); }); 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) {} } currentPageMp3 = Array.from(found).sort().map(url => ({ url, name: filenameFromUrl(url) })); return currentPageMp3; } // ============ DOWNLOAD ============ function downloadOne(item) { return new Promise(resolve => { if (typeof GM_download === 'function') { GM_download({ url: item.url, name: item.name, onload: () => resolve(true), onerror: () => { anchor(item); resolve(false); }, ontimeout: () => { anchor(item); resolve(false); } }); } else { anchor(item); resolve(true); } }); } function anchor(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); } // ============ UI ============ let panel, statusEl, collCountEl, pageCountEl, listEl; function buildPanel() { panel = document.createElement('div'); panel.id = 'osho-col'; panel.innerHTML = `
OSHO · CROSS-PAGE COLLECTOR
Ready
Page: 1 · Collected: 0 MP3
This page 0
Click Scan to see MP3s on this page.
Workflow: 1) Scan → 2) Add This Page → 3) click "Next" in the site → 4) repeat → 5) Download All.
`; document.body.appendChild(panel); statusEl = panel.querySelector('#col-status'); listEl = panel.querySelector('#col-list'); collCountEl = panel.querySelector('#col-total'); pageCountEl = panel.querySelector('#col-page-count'); panel.querySelector('#col-collapse').addEventListener('click', () => { panel.classList.toggle('col-collapsed'); panel.querySelector('#col-collapse').textContent = panel.classList.contains('col-collapsed') ? '+' : '−'; }); makeDraggable(panel, panel.querySelector('#col-drag')); panel.querySelector('#col-scan').addEventListener('click', () => { scan(); renderList(); setStatus(`Scanned · ${currentPageMp3.length} MP3 on this page`, currentPageMp3.length ? 'success' : 'error'); }); panel.querySelector('#col-add').addEventListener('click', () => { scan(); renderList(); if (currentPageMp3.length === 0) { setStatus('No MP3 on this page to add', 'error'); return; } let added = 0; for (const item of currentPageMp3) { if (!store.urls.find(u => u.url === item.url)) { store.urls.push({ url: item.url, name: item.name }); added++; } } save(store); updateTotals(); setStatus(`Added ${added} new MP3 · total ${store.urls.length}`, 'success'); }); panel.querySelector('#col-download').addEventListener('click', async () => { if (downloading) return; if (store.urls.length === 0) { setStatus('Nothing collected yet', 'error'); return; } const n = Math.min(store.urls.length, CFG.maxBatch); if (!confirm(`Download ${n} collected MP3?\n\nTotal in collection: ${store.urls.length}\nDownloading first ${n} only.`)) return; downloading = true; stopFlag = false; updateButtons(); let ok = 0; for (let i = 0; i < n; i++) { if (stopFlag) break; const item = store.urls[i]; setStatus(`⬇ ${i + 1}/${n} · ${cleanName(item.name)}`, 'loading'); await downloadOne(item); ok++; if (i < n - 1) await sleep(CFG.batchDelayMin + Math.random() * (CFG.batchDelayMax - CFG.batchDelayMin)); } downloading = false; updateButtons(); setStatus(`Done · ${ok}/${n} triggered`, 'success'); }); panel.querySelector('#col-copy').addEventListener('click', () => { if (store.urls.length === 0) { setStatus('Nothing to copy', 'error'); return; } const txt = store.urls.map(u => u.url).join('\n'); navigator.clipboard.writeText(txt).then(() => setStatus(`Copied ${store.urls.length} URLs`, 'success')); }); panel.querySelector('#col-clear').addEventListener('click', () => { if (!confirm('Clear all collected URLs?')) return; store = { urls: [], page: 1 }; save(store); updateTotals(); setStatus('Cleared', 'success'); }); listEl.addEventListener('click', e => { const btn = e.target.closest('button[data-act]'); if (!btn) return; const i = parseInt(btn.dataset.idx); const item = currentPageMp3[i]; if (!item) return; if (btn.dataset.act === 'dl') downloadOne(item).then(() => setStatus(`⬇ ${cleanName(item.name)}`, 'success')); else if (btn.dataset.act === 'copy') navigator.clipboard.writeText(item.url).then(() => { const o = btn.textContent; btn.textContent = '✓'; setTimeout(() => btn.textContent = o, 1000); }); }); updateTotals(); } function renderList() { pageCountEl.textContent = currentPageMp3.length; if (currentPageMp3.length === 0) { listEl.innerHTML = '
No MP3 on this page.
'; return; } const frag = document.createDocumentFragment(); currentPageMp3.forEach((item, i) => { const div = document.createElement('div'); div.className = 'it'; div.innerHTML = ` ${String(i + 1).padStart(2, '0')} ${esc(cleanName(item.name))} `; frag.appendChild(div); }); listEl.innerHTML = ''; listEl.appendChild(frag); } function updateTotals() { collCountEl.textContent = store.urls.length; document.getElementById('col-page').textContent = store.page || 1; } function setStatus(msg, type = '') { statusEl.textContent = msg; statusEl.className = 'st ' + type; } function updateButtons() { document.getElementById('col-download').disabled = downloading; document.getElementById('col-add').disabled = downloading; } function makeDraggable(el, handle) { let sx = 0, sy = 0, ox = 0, oy = 0, d = false; handle.addEventListener('mousedown', e => { if (e.target.tagName === 'BUTTON') return; d = true; sx = e.clientX; sy = e.clientY; const r = el.getBoundingClientRect(); ox = r.left; oy = r.top; e.preventDefault(); }); document.addEventListener('mousemove', e => { if (!d) return; el.style.left = (ox + e.clientX - sx) + 'px'; el.style.top = (oy + e.clientY - sy) + 'px'; el.style.right = 'auto'; }); document.addEventListener('mouseup', () => { d = false; }); } function init() { buildPanel(); setTimeout(() => { scan(); renderList(); setStatus(currentPageMp3.length ? `Auto-scanned · ${currentPageMp3.length} MP3` : 'No MP3 on this page. Click Scan.', currentPageMp3.length ? 'success' : ''); }, 800); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();