// ==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 = `