bilibili评论区图片快捷切换

按键盘左右方向键或<>键切换B站评论区图片,或滚动鼠标滚轮切换图片,按住alt时停用滚轮切换,并暂时隐藏下方的预览选框避免图片被遮挡。

当前为 2024-10-22 提交的版本,查看 最新版本

// ==UserScript==
// @name         bilibili评论区图片快捷切换
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  按键盘左右方向键或<>键切换B站评论区图片,或滚动鼠标滚轮切换图片,按住alt时停用滚轮切换,并暂时隐藏下方的预览选框避免图片被遮挡。
// @author       coccvo
// @match        https://www.bilibili.com/video/*
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAAAG1BMVEUAoNb////5/f7q+PzI6/ek3vGA0Os7t+AXqtoAacFbAAAAi0lEQVR42o2R3QqDIQxDE/uX93/isVVB3GTfuYo9IDQFSZobF+sBkiNVzomXckxhBSzj72xT0NWm5wq28MgCUJk5QziJEYUvKgZC+IECBVQeFFAQ5DxwQQBkPDABU3iuPdJ3YdVbddiF+sMOT8TIbq7DJjjsM++wiYM/4rL5tatru9d79AUPwknwwgt7fAWIfqzpTgAAAABJRU5ErkJggg==
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 定义一个变量来跟踪是否按住了Alt键
    let altPressed = false;

    // 监听键盘事件
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Alt') {
            altPressed = true;
        }
    });

    document.addEventListener('keyup', function(e) {
        if (e.key === 'Alt') {
            altPressed = false;
        }
    });

    // 监听键盘左右键或,.键
    document.addEventListener('keydown', function(e) {
    if (document.querySelector('bili-photoswipe')) {
        if (e.key === 'ArrowLeft' || e.key === ',') {
            e.preventDefault(); // 阻止默认行为,如页面滚动
            document.querySelector('bili-photoswipe').shadowRoot.querySelector('#prev').click();
        } else if (e.key === 'ArrowRight' || e.key === '.') {
            e.preventDefault(); // 阻止默认行为,如页面滚动
            document.querySelector('bili-photoswipe').shadowRoot.querySelector('#next').click();
        }
    }
    });

    // 监听鼠标滚轮事件
    document.addEventListener('wheel', function(e) {
        if (document.querySelector('bili-photoswipe') && !altPressed) {
            if (e.deltaY > 0) { // 向上滚动
                document.querySelector('bili-photoswipe').shadowRoot.querySelector('#next').click();
            } else if (e.deltaY < 0) { // 向下滚动
                document.querySelector('bili-photoswipe').shadowRoot.querySelector('#prev').click();
            }
        }
    });

    // 监听Alt键,隐藏或显示元素<div id="thumb">
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Alt') {
            const thumbElement = document.querySelector('bili-photoswipe').shadowRoot.querySelector('#thumb');
            if (thumbElement) {
            thumbElement.style.display = 'none';
            }
        }
    });

    document.addEventListener('keyup', function(e) {
        if (e.key === 'Alt') {
            const thumbElement = document.querySelector('bili-photoswipe').shadowRoot.querySelector('#thumb');
            if (thumbElement) {
            thumbElement.style.display = '';
        }
    }
});
})();