AutoScroll2

AutoScroll - 自动向下滚动页面,支持快捷键

目前為 2023-11-03 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        AutoScroll2
// @description AutoScroll - 自动向下滚动页面,支持快捷键
// @include     http*
// @version     1.0.0
// @author      Eilen https://github.com/EilenC
// @grant       none
// @namespace https://greasyfork.org/users/319354
// @supportURL https://github.com/EilenC/Tampermonkey-Scripts/blob/master/AutoScroll2/AutoScroll2.js
// ==/UserScript==

; (function (document) {
    'use strict';

    function roundToTwoDecimalPlaces(number) {
        return Number(number.toFixed(2));
    }
    // 存储当前通知的定时器
    let notificationTimer = null;
    let notification = document.createElement('div');
    // 显示通知函数
    function showNotification(message, duration) {
        // 如果有,清除之前的定时器
        clearTimeout(notificationTimer);

        // 创建提示元素
        notification.textContent = message;

        // 样式设置
        notification.style.position = 'fixed';
        notification.style.top = '50%';
        notification.style.left = '50%';
        notification.style.transform = 'translate(-50%, -50%)';
        notification.style.background = 'black';
        notification.style.color = 'white';
        notification.style.fontWeight = 'bold';
        notification.style.fontSize = '40px';
        notification.style.padding = '20px';
        notification.style.borderRadius = '10px';

        // 将元素添加到页面
        document.body.appendChild(notification);

        // 设置定时器,在指定的时间后移除通知
        notificationTimer = setTimeout(function () {
            document.body.removeChild(notification);
            notificationTimer = null; // 通知显示完成后清空定时器
        }, duration);
    }
    if (!Date.now) {
        Date.now = function () { return new Date().getTime(); };
    }
    //from https://github.com/darius/requestAnimationFrame/blob/master/requestAnimationFrame.js
    (function () {
        'use strict';

        var vendors = ['webkit', 'moz'];
        for (var i = 0; i < vendors.length && !window.requestAnimationFrame; ++i) {
            var vp = vendors[i];
            window.requestAnimationFrame = window[vp + 'RequestAnimationFrame'];
            window.cancelAnimationFrame = (window[vp + 'CancelAnimationFrame']
                || window[vp + 'CancelRequestAnimationFrame']);
        }
        if (/iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent) // iOS6 is buggy
            || !window.requestAnimationFrame || !window.cancelAnimationFrame) {
            var lastTime = 0;
            window.requestAnimationFrame = function (callback) {
                var now = Date.now();
                var nextTime = Math.max(lastTime + 16, now);
                return setTimeout(function () { callback(lastTime = nextTime); },
                    nextTime - now);
            };
            window.cancelAnimationFrame = clearTimeout;
        }
    }());

    'use strict';

    let keyDownCount = 0;
    let lastKeyDownTime = 0;
    let enable = false;
    let scrollSpeed = 0.55;
    let animationFrameId = 0;
    let tipsTime = 300;
    let handler = 0;
    let time = 200;
    let chromeSpeedSpeed = 0.55;
    let isEdge = navigator.userAgent.includes('Edg') ? true : false;

    console.log('isEdge:', isEdge);

    function toggleScroll() {
        enable = !enable;
        if (enable) {
            if (isEdge) {
                edgeAutoScroll();
            } else {
                autoScroll();
            }
            showNotification('AutoScroll:On', tipsTime);
        } else {
            if (isEdge) {
                cancelAnimationFrame(animationFrameId);
            } else {
                clearTimeout(handler);
            }
            showNotification('AutoScroll:Off', tipsTime);
        };
    }

    function keydownHandler(e) {
        console.log(e.key);
        let currentTime = new Date().getTime();
        switch (e.key) {
            case "`":
                if (currentTime - lastKeyDownTime < 300) {
                    keyDownCount++;
                } else {
                    keyDownCount = 1;
                }

                lastKeyDownTime = currentTime;

                if (keyDownCount === 2) {
                    toggleScroll();
                    keyDownCount = 0;
                }
                break;
            case "ArrowRight":
                if (isEdge && enable) {
                    scrollSpeed = roundToTwoDecimalPlaces(scrollSpeed += 0.02);
                    showNotification('Speed:' + scrollSpeed, tipsTime);
                }
                if (!isEdge && enable) {
                    time -= 20;
                    chromeSpeedSpeed = roundToTwoDecimalPlaces(chromeSpeedSpeed += 0.02);
                    showNotification('Speed:' + chromeSpeedSpeed, tipsTime);
                }
                break;
            case "ArrowLeft":
                if (isEdge && enable) {
                    if (scrollSpeed - 0.02 > 0) {
                        scrollSpeed -= 0.02
                    }
                    scrollSpeed = roundToTwoDecimalPlaces(scrollSpeed);
                    showNotification('Speed:' + scrollSpeed, tipsTime);
                }
                if (!isEdge && enable) {
                    time += 20;
                    if (chromeSpeedSpeed - 0.02 <= 0) {
                        chromeSpeedSpeed = 0.02
                    }else{
                        chromeSpeedSpeed = roundToTwoDecimalPlaces(chromeSpeedSpeed -= 0.02);
                    }
                    showNotification('Speed:' + chromeSpeedSpeed, tipsTime);
                }
                break;
        }
    }
    function edgeAutoScroll() {
        console.log("edgeAutoScroll: " + document.documentElement.scrollTop);
        console.log(window.scrollY, window.pageYOffset, document.documentElement.scrollTop);
        document.documentElement.scrollTop += scrollSpeed;
        animationFrameId = requestAnimationFrame(edgeAutoScroll);
    }
    function autoScroll() {
        console.log("autoScroll: " + document.documentElement.scrollTop);
        console.log(window.scrollY, window.pageYOffset, document.documentElement.scrollTop);
        document.documentElement.scrollTop += 1 / window.devicePixelRatio + 0.05;
        handler = setTimeout(autoScroll, time);
    }

    // 监听键盘事件
    document.body.removeEventListener('keydown', keydownHandler);
    document.body.addEventListener('keydown', keydownHandler);
})(document);