油猴脚本——页面自动滚动

这是我写的第一个脚本,实现起来很简单,有兴趣的话可以下载玩玩。添加链接描述
下面是它的源代码:



    // ==UserScript==
    // @name         autoScroll
    // @namespace    eyes
    // @version      0.1
    // @description  It allows the page to scroll on its own
    // @author       eyes
    // @match        *://*/*
    // @grant        none
    // ==/UserScript==
     
    (function() {
        'use strict';
                var speed = 0;
            // 获取滑动位置
            function getScrollTop() {
                var scrollTop = 0,
                    bodyScrollTop = 0,
                    documentScrollTop = 0;
                if (document.body) {
                    bodyScrollTop = document.body.scrollTop;
                }
                if (document.documentElement) {
                    documentScrollTop = document.documentElement.scrollTop;
                }
                scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
                return scrollTop;
            }
            //文档的总高度
            function getScrollHeight() {
                var scrollHeight = 0,
                    bodyScrollHeight = 0,
                    documentScrollHeight = 0;
                if (document.body) {
                    bodyScrollHeight = document.body.scrollHeight;
                }
                if (document.documentElement) {
                    documentScrollHeight = document.documentElement.scrollHeight;
                }
                scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
                return scrollHeight;
            }
            //浏览器视口的高度
            function getWindowHeight() {
                var windowHeight = 0;
                if (document.compatMode == 'CSS1Compat') {
                    windowHeight = document.documentElement.clientHeight;
                } else {
                    windowHeight = document.body.clientHeight;
                }
                return windowHeight;
            }
     
            // 滚动事件
            setInterval(() => {
                // 判断页面是否滑到底部
                let bottomFlag = (getScrollTop() + getWindowHeight() == getScrollHeight()) ? true : false;
                if (bottomFlag) {
                    speed = 0;
                } else {
                    document.documentElement.scrollTop += speed;
                }
            }, 5)
     
            // 判断是否需要滚动
            document.onkeydown = (e) => {
                e = event || window.event;
                if (e && e.keyCode == 38 && e.altKey) { // 上键
                    let bottomFlag = (getScrollTop() + getWindowHeight() == getScrollHeight()) ? true : false;
                    if (bottomFlag) {
                        document.documentElement.scrollTop += -1;
                    }
                    speed -= 1.5;
                }
                if (e && e.keyCode == 40 && e.altKey) { // 下键
                    speed += 1.5;
                }
            }
     
            // 单击页面停止滚动
            document.onclick = () => {
                speed = 0;
            }
     
            // 滑动滚轮页面停止滚动
            document.onmousewheel = () => {
                speed = 0;
            }
            document.addEventListener("DOMMouseScroll", () => {
                speed = 0;
            })
    })();


上一篇:dearpygui-0.6.6 以前的蛇变成了这个样子


下一篇:21.5.25 制作人物移动脚本