// ==UserScript== // @name 鼠标中键自动滚动(Windows 风格) // @name:en Middle Click Auto Scroll // @namespace local.autoscroll // @version 1.1.0 // @description 单击鼠标中键开启自动滚动,上下移动控制速度和方向;再次中键、左键或 Esc 停止。链接上的中键仍可在新标签页打开。 // @author Qxlei // @match *://*/* // @run-at document-start // @grant none // @license MIT // ==/UserScript== (function () { 'use strict'; const ACTIVATION_RADIUS = 6; // 按下后轻微移动仍可点击开启 const DEAD_ZONE = 12; // 中心死区,避免鼠标轻抖造成滚动 const MAX_DISTANCE = 180; // 达到此距离时使用最高速度 const MAX_SPEED = 45; // 每帧最大滚动像素 const CURSOR_SIZE = 24; let mode = 'idle'; // idle | pending | active let pressX = 0; let pressY = 0; let anchorX = 0; let anchorY = 0; let mouseX = 0; let mouseY = 0; let startTime = 0; let moved = false; let frameId = 0; let lastFrame = 0; let marker = null; function makeMarker(x, y) { removeMarker(); marker = document.createElement('div'); marker.setAttribute('data-middle-click-autoscroll', ''); marker.style.cssText = [ 'position:fixed', `left:${x - CURSOR_SIZE / 2}px`, `top:${y - CURSOR_SIZE / 2}px`, `width:${CURSOR_SIZE}px`, `height:${CURSOR_SIZE}px`, 'box-sizing:border-box', 'border:2px solid rgba(80,80,80,.75)', 'border-radius:50%', 'background:rgba(255,255,255,.35)', 'box-shadow:0 1px 4px rgba(0,0,0,.18)', 'pointer-events:none', 'z-index:2147483647' ].join(';'); const dot = document.createElement('div'); dot.style.cssText = [ 'position:absolute', 'left:8px', 'top:8px', 'width:4px', 'height:4px', 'border-radius:50%', 'background:rgba(60,60,60,.8)' ].join(';'); marker.appendChild(dot); (document.documentElement || document.body).appendChild(marker); } function removeMarker() { if (marker) marker.remove(); marker = null; } function start(x, y) { anchorX = mouseX = x; anchorY = mouseY = y; mode = 'active'; makeMarker(x, y); lastFrame = performance.now(); frameId = requestAnimationFrame(scrollFrame); } function stop() { mode = 'idle'; cancelAnimationFrame(frameId); frameId = 0; removeMarker(); } function scrollFrame(now) { if (mode !== 'active') return; // 按真实经过时间计算,兼顾高刷新率屏幕和后台标签页。 const elapsed = Math.min((now - lastFrame) / 16.667, 3); lastFrame = now; const delta = mouseY - anchorY; const distance = Math.abs(delta); if (distance > DEAD_ZONE) { const progress = Math.min((distance - DEAD_ZONE) / (MAX_DISTANCE - DEAD_ZONE), 1); const speed = MAX_SPEED * Math.pow(progress, 1.5); window.scrollBy({ left: 0, top: Math.sign(delta) * speed * elapsed, behavior: 'instant' }); } frameId = requestAnimationFrame(scrollFrame); } // 判断中键是否点在链接上。使用 composedPath 兼容 Shadow DOM。 function isOnLink(event) { if (typeof event.composedPath === 'function') { for (const node of event.composedPath()) { if (node instanceof Element && node.closest('a[href], area[href]')) return true; } return false; } return event.target instanceof Element && !!event.target.closest('a[href], area[href]'); } // 链接上完全放行浏览器默认行为(中键 = 新标签页打开)。 document.addEventListener('auxclick', function (event) { if (event.button !== 1 || isOnLink(event)) return; event.preventDefault(); event.stopImmediatePropagation(); }, true); document.addEventListener('mousedown', function (event) { if (event.button === 1) { // 链接上不接管,保留“新标签页打开”。 if (isOnLink(event)) { if (mode !== 'idle') stop(); return; } event.preventDefault(); event.stopImmediatePropagation(); if (mode === 'active') { stop(); return; } mode = 'pending'; pressX = mouseX = event.clientX; pressY = mouseY = event.clientY; startTime = performance.now(); moved = false; return; } // 左键、右键都会退出自动滚动,避免影响后续操作。 if (mode === 'active') stop(); }, true); document.addEventListener('mousemove', function (event) { mouseX = event.clientX; mouseY = event.clientY; if (mode === 'pending' && Math.hypot(mouseX - pressX, mouseY - pressY) > ACTIVATION_RADIUS) { moved = true; } }, true); document.addEventListener('mouseup', function (event) { if (event.button !== 1 || mode !== 'pending') return; event.preventDefault(); event.stopImmediatePropagation(); // 兼容 Windows:原地单击开启;中键拖动也可以直接开启。 const isClick = !moved && performance.now() - startTime < 600; if (isClick || moved) start(pressX, pressY); else stop(); }, true); document.addEventListener('keydown', function (event) { if (event.key === 'Escape' && mode !== 'idle') { event.preventDefault(); stop(); } }, true); window.addEventListener('blur', stop, true); window.addEventListener('pagehide', stop, true); })();