滚动到顶部和底部按钮

在所有页面上添加滚动到顶部和底部的按钮

// ==UserScript==
// @name         滚动到顶部和底部按钮
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  在所有页面上添加滚动到顶部和底部的按钮
// @author       Mr.R.J WOO
// @match        *://*/*
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAABcSAAAXEgFnn9JSAAAA50lEQVQ4ja3UOwrCQBCA4T9gY+0lrER7sfFCuYB4BMVCGxsbwQPY2NnkBDY5gqRREZ/gWOhiXJN9JC5Mkxk+MszuICKoMJwLEOYlvwwHcAkIcPsH2HljKuKy4E4DBRgWBecZmIqmL1g3YAIcfcGtBRQgcgWnDpiKng0ce2AqRmkjSLcaBEEbqAEboIr5nIEWkIjIWn2saEUP4A6cLBjvmj2vV/Q5WsuDAi1PbEPpe2Czn5/KuTaRA5Zkdml4KUcL2PAFWwZskTsHy3LIGtJBL/Ldh7EGdsuC8FquAqyykkXAELjmJdPGE8Xnz3OB0iSwAAAAAElFTkSuQmCC
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    console.log('脚本开始执行');

    // 检查是否已经有滚动按钮
    if (document.querySelector('.scroll-top') || document.querySelector('.scroll-bottom')) {
        console.log('滚动按钮已存在,脚本终止执行');
        return;
    }

    const buttonStyle = `
        background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAABmNAAAZjQEn17ZGAAAAB3RJTUUH3woJCyUQ4RV8EwAABuhJREFUaN7VWltsG1Ua/v4z06ahCY1QqpaWWxOVRYgljWfsGLc8uKgC1I3SVmAQ4to+LLsPoJVWoOVOuUgFJMQLF4kWWi5ii2gJLRQWUT/QmtiecZJVhQTdZBcEod22KGlSGrsz8/PQsTlx7HjGTtpyJCvOf47PfN/81/nPEDPj9zzUmdiUiISmaQtUVVXq6+sPxeNxa6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4e/8GrjlcavRDBCbddPcIAAAAAASUVORK5CYII=) no-repeat;
        background-size: 40px 40px;
        height: 40px;
        width: 40px;
        position: fixed;
        right: 10px;
        cursor: pointer;
        z-index: 9999;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.2s linear, visibility 0.2s linear;
    `;

    console.log('创建滚动到顶部按钮');
    var scrollTopButton = document.createElement('div');
    scrollTopButton.className = 'scroll-top';
    scrollTopButton.style.cssText = buttonStyle + 'bottom: 48px;';

    console.log('创建滚动到底部按钮');
    var scrollBottomButton = document.createElement('div');
    scrollBottomButton.className = 'scroll-bottom';
    scrollBottomButton.style.cssText = buttonStyle + 'bottom: 5px; transform: scaleY(-1);';

    // 添加点击事件
    scrollTopButton.addEventListener('click', function() {
        console.log('点击滚动到顶部按钮');
        window.scrollTo({ top: 0, behavior: 'smooth' });
    });

    scrollBottomButton.addEventListener('click', function() {
        console.log('点击滚动到底部按钮');
        window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
    });

    // 监听页面滚动,控制按钮的显示和隐藏
    window.addEventListener('scroll', function() {
        var scrollHeight = window.pageYOffset || document.documentElement.scrollTop;
        var maxScroll = document.body.scrollHeight - window.innerHeight;

        if (scrollHeight > 160) {
            scrollTopButton.style.opacity = '1';
            scrollTopButton.style.visibility = 'visible';
            console.log('滚动超过160px,显示滚动到顶部按钮');
        } else {
            scrollTopButton.style.opacity = '0';
            scrollTopButton.style.visibility = 'hidden';
            console.log('滚动小于160px,隐藏滚动到顶部按钮');
        }

        if (scrollHeight < maxScroll - 160) {
            scrollBottomButton.style.opacity = '1';
            scrollBottomButton.style.visibility = 'visible';
            console.log('滚动未到底部,显示滚动到底部按钮');
        } else {
            scrollBottomButton.style.opacity = '0';
            scrollBottomButton.style.visibility = 'hidden';
            console.log('滚动到底部,隐藏滚动到底部按钮');
        }
    });

    // 将按钮添加到页面
    document.body.appendChild(scrollTopButton);
    document.body.appendChild(scrollBottomButton);
    console.log('按钮已添加到页面');
})();