Universal Auto Scroll

Press Alt+X to start auto-scrolling. Use Alt+Up/Down to adjust speed. Press Esc, Space, or Enter to stop.

当前为 2025-02-17 提交的版本,查看 最新版本

// ==UserScript==
// @name           Universal Auto Scroll
// @version        1.0
// @namespace      MedX-AA
// @author         MedX
// @license        MIT
// @description    Press Alt+X to start auto-scrolling. Use Alt+Up/Down to adjust speed. Press Esc, Space, or Enter to stop.
// @icon           data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAABGUlEQVR4nO3VsS4EURQG4E9oRCEh0ZDQi3gDjV7lHXTeAYVo9gl0ahregEapoKEh0SFRbVbIyiSjEYnd2TtzZ5PzJSeZYjbzn/2LQwghlSN8oP/PFO8caqHeAOF/pni3dfpDTuvEArlFA7lFA7lFA7lFA7lFA7lFA7lFA7lFA7m1uoE9dCuErDrd8pvJvDYYvl/OS8oFOhkW6KRcYBKnDYY/x5TEpnHZQPhrzKjJLG5qDH+LOTVbxGMN4Z+xrCGreEsY/h3rGraR6Db0sCmTLXyOEP4L2zLbGWGBXS1xUCH8vhaZwPEQ4U/K37RKca3PBgh/UceVTXmtr3Jd2VTmcfdH+HssGBNLePp1ZVeMmTU8lP988RxCkN43yek7CExEuggAAAAASUVORK5CYII=
// @grant          none
// @include        *
// ==/UserScript==

// === CONFIGURATION ===
const INITIAL_SPEED = 30; // Starting speed in pixels per second
const RESET_THRESHOLD = 10; // Sensitivity for manual scroll detection

// === KEY BINDINGS ===
const START_KEY = 88; // Alt + X starts scrolling
const SPEED_UP_KEY = 40; // Alt + Down (only works during scrolling)
const SPEED_DOWN_KEY = 38; // Alt + Up
const STOP_KEYS = new Set([27, 32, 13]); // Escape, Space, Enter
const ALLOWED_KEYS = new Set([33, 34, 38, 40]); // Page Up, Page Down, Up Arrow, Down Arrow

// === STATE VARIABLES ===
let isScrolling = false, scrollSpeed = 0, lastUpdateTime, realX, realY, hudTimeout;
const scrollElement = document.scrollingElement || document.documentElement;

// === HUD DISPLAY (Speed Indicator) ===
const hud = Object.assign(document.createElement("div"), {
    style: `position:fixed; bottom:10px; right:10px; padding:5px 10px; background:rgba(0,0,0,0.7);
            color:#fff; font-size:14px; font-family:Arial; border-radius:5px; z-index:9999; display:none`
});
document.body.appendChild(hud);

// === LISTENER: Start scrolling with Alt + X (Only if Ctrl/Shift are NOT pressed) ===
window.addEventListener('keydown', (e) => {
    if (e.keyCode !== START_KEY || !e.altKey || e.ctrlKey || e.shiftKey) return; // ✅ Exit early if conditions aren't met
    if (!isScrolling) {
        startScrolling();
        e.preventDefault();
    }
}, true);

// === LISTENER: Adjust speed or stop scrolling (Only active when scrolling) ===
function handleKeydown(e) {
    if (!isScrolling) return; // ✅ Ignore input if not scrolling

    if (e.altKey && e.keyCode === SPEED_UP_KEY) changeSpeed(1.1); // Increase speed
    else if (e.altKey && e.keyCode === SPEED_DOWN_KEY) changeSpeed(0.9); // Decrease speed
    else if (STOP_KEYS.has(e.keyCode)) {
        stopScrolling();
        if (e.keyCode === 32) e.preventDefault(); // ✅ Prevent Spacebar from triggering page scroll
    }

    // ✅ Allow Page Up, Page Down, Up Arrow, Down Arrow to work normally
    if (!ALLOWED_KEYS.has(e.keyCode)) e.preventDefault();
}

// === DETECT MANUAL SCROLLING ===
function updateScrollPosition() {
    realX = scrollElement.scrollLeft;
    realY = scrollElement.scrollTop;
}

// === CHANGE SCROLL SPEED ===
function changeSpeed(multiplier) {
    scrollSpeed = Math.max(5, (scrollSpeed || INITIAL_SPEED) * multiplier); // Ensures speed never drops too low

    // ✅ Show HUD immediately with new speed
    hud.textContent = `Speed: ${scrollSpeed.toFixed(1)} px/s`;
    hud.style.display = "block";
    if (hudTimeout) clearTimeout(hudTimeout);
    hudTimeout = setTimeout(() => (hud.style.display = "none"), 2000);
}

// === START AUTO-SCROLLING ===
function startScrolling() {
    isScrolling = true;
    scrollSpeed = INITIAL_SPEED;
    updateScrollPosition();
    lastUpdateTime = performance.now();

    // ✅ Show HUD instantly
    changeSpeed(1);

    window.addEventListener('keydown', handleKeydown, true);
    window.addEventListener('scroll', updateScrollPosition, { passive: true });

    requestAnimationFrame(scrollLoop);
}

// === STOP AUTO-SCROLLING ===
function stopScrolling() {
    isScrolling = false;
    scrollSpeed = 0;
    hud.style.display = "none";

    window.removeEventListener('keydown', handleKeydown, true);
    window.removeEventListener('scroll', updateScrollPosition, { passive: true });
}

// === MAIN SCROLL LOOP (Smooth & Efficient) ===
function scrollLoop(timestamp) {
    if (!isScrolling) return;

    let elapsed = timestamp - lastUpdateTime;
    realY += (scrollSpeed * elapsed) / 1000; // Adjust position based on elapsed time
    scrollElement.scrollTo(realX, Math.floor(realY));
    lastUpdateTime = timestamp;

    requestAnimationFrame(scrollLoop);
}