墨水屏划动优化

消除划动动画 可设置划动倍率

当前为 2025-10-05 提交的版本,查看 最新版本

// ==UserScript==
// @name         墨水屏划动优化
// @namespace    http://tampermonkey.net/
// @version      1.02
// @description  消除划动动画  可设置划动倍率
// @match        *://*/*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// @run-at       document-start
// @license      MIT
// @author       cxuan.cc
// ==/UserScript==

(function(){
    let N = GM_getValue('multiplier',2);
    GM_registerMenuCommand('设置滑动倍率',()=>{
        let v=prompt('输入滑动倍率:',N);
        v=parseFloat(v);
        if(!isNaN(v))GM_setValue('multiplier',N=v);
    });

    let sy, el;
    // 0. 用一个对象存每根手指的数据
    const touchMap = {};

    // 1. touchstart:循环 changedTouches,分别记录 sy 和 el
    document.addEventListener('touchstart', e => {
        for (const t of e.changedTouches) {
            let el = t.target;
            while (el && el!==document && !(el.scrollHeight>el.clientHeight && /auto|scroll/.test(getComputedStyle(el).overflowY))) {
                el = el.parentNode;
            }
            if (el===document) el = document.scrollingElement||document.documentElement;
            touchMap[t.identifier] = { sy: t.clientY, el };
        }
    }, { passive: false });

    // 2. touchmove:遍历 changedTouches,只在非顶端下拉刷新时阻止默认滚动
    document.addEventListener('touchmove', e => {
        let doPrevent = false;
        for (const t of e.changedTouches) {
            const info = touchMap[t.identifier];
            if (!info) continue;
            const dy = t.clientY - info.sy;
            // 如果是页面最顶部的 el、scrollTop=0 且在往下拉,就放行,让浏览器下拉刷新
            if (!(info.el === (document.scrollingElement||document.documentElement)
                  && info.el.scrollTop === 0
                  && dy > 0)) {
                doPrevent = true;
            }
        }
        if (doPrevent) e.preventDefault();
    }, { passive: false });

    // 3. touchend/touchcancel:遍历 changedTouches,按 N 倍滑动距离去滚
    function onEnd(e) {
        for (const t of e.changedTouches) {
            const info = touchMap[t.identifier];
            if (!info) continue;
            const dy = (info.sy - t.clientY) * N;
            info.el.scrollTop += dy;
            delete touchMap[t.identifier];
        }
    }
    document.addEventListener('touchend', onEnd, { passive: false });
    document.addEventListener('touchcancel', onEnd, { passive: false });

})();