// ==UserScript== // @name 有谱么自动翻谱(Auto Scroll) // @namespace https://github.com/Longtermism0704/youpume-auto-turn // @version 2.3.1 // @description 在 yopu.co 曲谱页自动向下翻谱滚动,支持调速、暂停/继续,以整首用时为标尺。纯本地运行,不收集任何数据。 // @author lishuxin // @license MIT // @match https://yopu.co/* // @match https://www.yopu.co/* // @icon data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMjggMTI4Ij48cmVjdCB3aWR0aD0iMTI4IiBoZWlnaHQ9IjEyOCIgcng9IjI4IiBmaWxsPSIjNGE3ZGZmIi8+PHBhdGggZD0iTTQwIDI4aDQ4djhoLTh2NTJsLTE2IDhWNDBINDh2NTJsLTE2LThWMzZoLTh6IiBmaWxsPSIjZmZmIi8+PC9zdmc+ // @grant GM_getValue // @grant GM_setValue // @run-at document-idle // ==/UserScript== (function () { 'use strict'; if (window.__youpuAutoScrollTM) return; window.__youpuAutoScrollTM = true; // ---------- 配置持久化(GM 优先,降级 localStorage)---------- const KEY = 'ys_pxPerSecond'; function loadSpeed() { try { if (typeof GM_getValue === 'function') { const v = parseFloat(GM_getValue(KEY, '40')); return (!isNaN(v) && v > 0) ? v : 40; } const v = parseFloat(localStorage.getItem(KEY) || '40'); return (!isNaN(v) && v > 0) ? v : 40; } catch (e) { return 40; } } function saveSpeed(pps) { try { if (typeof GM_setValue === 'function') GM_setValue(KEY, String(pps)); else localStorage.setItem(KEY, String(pps)); } catch (e) {} } // ---------- 状态 ---------- const CONTAINER_ID = 'nier-scroll-view'; const state = { playing: false, container: null, worker: null, timerId: null, timerRunning: false, lastTs: 0, pxPerSecond: loadSpeed(), reachedBottom: false, }; // ---------- 容器探测 ---------- function isScrollable(el) { if (!el) return false; const oy = getComputedStyle(el).overflowY; if (oy !== 'auto' && oy !== 'scroll') return false; return el.scrollHeight > el.clientHeight + 20; } function findContainer() { const byId = document.getElementById(CONTAINER_ID); if (byId && byId.scrollHeight > byId.clientHeight + 20) return byId; let best = null, bestRange = 0; document.querySelectorAll('div,main,section').forEach((el) => { if (!isScrollable(el)) return; const range = el.scrollHeight - el.clientHeight; if (range > bestRange) { bestRange = range; best = el; } }); if (best) return best; if (document.documentElement.scrollHeight > window.innerHeight + 20) return document.scrollingElement || document.documentElement; return null; } function isDoc(el) { return el === document.scrollingElement || el === document.documentElement; } function getScrollTop(el) { return isDoc(el) ? window.scrollY : el.scrollTop; } function setScrollTop(el, v) { if (isDoc(el)) window.scrollTo(0, v); else el.scrollTop = v; } function getMaxScroll(el) { const sh = isDoc(el) ? document.documentElement.scrollHeight : el.scrollHeight; const ch = isDoc(el) ? window.innerHeight : el.clientHeight; return Math.max(0, sh - ch); } // ---------- 滚动引擎(Worker 计时规避后台冻结)---------- const TICK_MS = 50; function createWorker() { if (state.worker) return state.worker; try { const src = 'var t=null;onmessage=function(e){if(e.data.cmd==="start"){if(t)clearInterval(t);t=setInterval(function(){postMessage("tick");},' + TICK_MS + ');}else if(e.data.cmd==="stop"){if(t){clearInterval(t);t=null;}}};'; const url = URL.createObjectURL(new Blob([src], { type: 'application/javascript' })); const w = new Worker(url); w.onmessage = function (e) { if (e.data === 'tick') doTick(); }; state.worker = w; } catch (e) { state.worker = null; } return state.worker; } function doTick() { if (!state.playing || !state.container) { stopTimer(); return; } const now = (performance && performance.now) ? performance.now() : Date.now(); if (!state.lastTs) state.lastTs = now; let dt = (now - state.lastTs) / 1000; state.lastTs = now; if (dt > 0.5) dt = 0.5; const el = state.container; const max = getMaxScroll(el), cur = getScrollTop(el); if (cur >= max - 0.5) { state.reachedBottom = true; state.playing = false; stopTimer(); updateUI(); return; } setScrollTop(el, Math.min(cur + state.pxPerSecond * dt, max)); } function startTimer() { if (state.timerRunning) return; state.timerRunning = true; state.lastTs = 0; const w = createWorker(); if (w) w.postMessage({ cmd: 'start' }); else state.timerId = setInterval(doTick, TICK_MS); } function stopTimer() { state.timerRunning = false; state.lastTs = 0; if (state.worker) { try { state.worker.postMessage({ cmd: 'stop' }); } catch (e) {} } if (state.timerId != null) { clearInterval(state.timerId); state.timerId = null; } } function start() { const el = findContainer(); if (!el) return false; state.container = el; if (state.reachedBottom || getScrollTop(el) >= getMaxScroll(el) - 0.5) { setScrollTop(el, 0); state.reachedBottom = false; } state.playing = true; state.lastTs = 0; startTimer(); updateUI(); return true; } function pause() { state.playing = false; stopTimer(); updateUI(); } function reset() { state.playing = false; stopTimer(); state.reachedBottom = false; if (state.container) setScrollTop(state.container, 0); updateUI(); } // ---------- 悬浮控制面板 ---------- let ui = null; function buildUI() { if (document.getElementById('youpu-tm-panel')) return true; if (!document.body) return false; // body 未就绪,交给调用方重试 const wrap = document.createElement('div'); wrap.id = 'youpu-tm-panel'; wrap.style.cssText = 'position:fixed;right:16px;bottom:80px;z-index:999999;font-family:-apple-system,"Microsoft YaHei",sans-serif;'; wrap.innerHTML = `
整首谱滚动时长5.0 分钟
请打开曲谱页
`; document.body.appendChild(wrap); ui = { box: wrap.querySelector('#yp-box'), fab: wrap.querySelector('#yp-fab'), range: wrap.querySelector('#yp-range'), dur: wrap.querySelector('#yp-dur'), slower: wrap.querySelector('#yp-slower'), faster: wrap.querySelector('#yp-faster'), toggle: wrap.querySelector('#yp-toggle'), reset: wrap.querySelector('#yp-reset'), status: wrap.querySelector('#yp-status'), }; let fine = 1.0; ui.fab.onclick = () => { ui.box.classList.toggle('show'); updateUI(); }; ui.range.oninput = () => { const m = parseFloat(ui.range.value) || 5; ui.dur.textContent = m.toFixed(1); applySpeed(fine); }; ui.slower.onclick = () => { fine = Math.max(0.25, fine * 0.85); applySpeed(fine); }; ui.faster.onclick = () => { fine = Math.min(4, fine * 1.18); applySpeed(fine); }; ui.toggle.onclick = () => { if (state.playing) pause(); else { if (!start()) { ui.status.textContent = '未找到曲谱滚动区域'; return; } } }; ui.reset.onclick = reset; applySpeed(fine); updateUI(); return true; } function applySpeed(fine) { if (!ui) return; const el = findContainer(); const maxScroll = el ? getMaxScroll(el) : 0; const minutes = parseFloat(ui.range.value) || 5; if (maxScroll <= 0) { ui.status.textContent = '请打开曲谱页'; return; } state.pxPerSecond = (maxScroll / (minutes * 60)) * (fine || 1); saveSpeed(state.pxPerSecond); ui.status.textContent = state.pxPerSecond.toFixed(1) + ' px/s'; } function updateUI() { if (!ui) return; if (state.playing) { ui.toggle.textContent = '暂停'; ui.toggle.classList.remove('yp-primary'); ui.toggle.classList.add('yp-ghost'); } else { ui.toggle.textContent = '开始滚动'; ui.toggle.classList.remove('yp-ghost'); ui.toggle.classList.add('yp-primary'); } } // 空格快捷键 document.addEventListener('keydown', function (e) { if (e.code !== 'Space') return; const t = e.target; if (t && (t.tagName === 'INPUT' || t.tagName === 'TEXTAREA' || t.isContentEditable)) return; if (!state.playing && !state.timerRunning) return; e.preventDefault(); if (state.playing) pause(); else start(); }, true); // SPA 路由变化重置 let lastUrl = location.href; setInterval(function () { try { if (typeof chrome !== 'undefined' && chrome.runtime && !chrome.runtime.id) return; } catch (e) {} if (location.href !== lastUrl) { lastUrl = location.href; state.container = null; state.playing = false; state.reachedBottom = false; stopTimer(); } }, 800); // 启动:确保面板挂上(等待 body、重试、SPA 变化守护) function ensureUI(retries) { if (buildUI()) return; if (retries > 0) { setTimeout(function () { ensureUI(retries - 1); }, 300); } } function boot() { ensureUI(20); // 最多重试 20 次(约 6 秒) } // 首次启动:等 body 就绪 if (document.body) { boot(); } else { const obs = new MutationObserver(function () { if (document.body) { obs.disconnect(); boot(); } }); obs.observe(document.documentElement, { childList: true, subtree: true }); // 兜底 window.addEventListener('DOMContentLoaded', boot); } // SPA 守护:面板被路由切换移除时自动重建(每 1.5 秒检查一次) setInterval(function () { try { if (typeof chrome !== 'undefined' && chrome.runtime && !chrome.runtime.id) return; if (document.body && !document.getElementById('youpu-tm-panel')) buildUI(); } catch (e) {} }, 1500); })();