Greasy Fork

自动滚动页面

自动滚动页面并具有速度调整功能的油猴插件

目前为 2023-04-25 提交的版本。查看 最新版本

// ==UserScript==
// @name         自动滚动页面
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  自动滚动页面并具有速度调整功能的油猴插件
// @author       Leeyw
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    let scrolling = false;
    let scrollSpeed = 1;
    let lastTimestamp = null;

    const controlPanel = document.createElement('div');
    controlPanel.style.position = 'fixed';
    controlPanel.style.right = '10px';
    controlPanel.style.bottom = '10px';
    controlPanel.style.zIndex = '1000';
    controlPanel.style.padding = '10px';
    controlPanel.style.backgroundColor = 'rgba(0, 0, 0, 0.6)';
    controlPanel.style.borderRadius = '5px';
    controlPanel.style.color = 'white';

    const speedInput = document.createElement('input');
    speedInput.type = 'number';
    speedInput.value = scrollSpeed;
    speedInput.style.width = '50px';

    const startStopButton = document.createElement('button');
    startStopButton.textContent = '开始';

    controlPanel.appendChild(document.createTextNode('滚动速度: '));
    controlPanel.appendChild(speedInput);
    controlPanel.appendChild(document.createTextNode(' px/s'));
    controlPanel.appendChild(document.createElement('br'));
    controlPanel.appendChild(startStopButton);

    document.body.appendChild(controlPanel);

    startStopButton.addEventListener('click', () => {
        scrolling = !scrolling;
        startStopButton.textContent = scrolling ? '停止' : '开始';
        if (scrolling) {
            window.requestAnimationFrame(autoScroll);
        }
    });

    speedInput.addEventListener('change', () => {
        scrollSpeed = parseInt(speedInput.value, 10);
    });
    let frame = 0

    function autoScroll() {
        if (frame >= 3) {
          const scrollAmount = scrollSpeed;
          window.scrollBy(0, scrollAmount);
          frame = 0;
        }
        console.log('intooo')
        frame++

        if (scrolling) {
            window.requestAnimationFrame(autoScroll);
        } else {
            frame = null;
        }
    }
})();