bilibili评论区图片快捷切换

按esc或者backspace,或者鼠标点击空白处,快捷关闭b站评论区图片。按左右键或<>键切换图片,或滑动鼠标滚轮切换图片,按住alt停止滚轮切换。该脚本使用chatgpt完成。

目前為 2023-04-18 提交的版本,檢視 最新版本

// ==UserScript==
// @name         bilibili评论区图片快捷切换
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  按esc或者backspace,或者鼠标点击空白处,快捷关闭b站评论区图片。按左右键或<>键切换图片,或滑动鼠标滚轮切换图片,按住alt停止滚轮切换。该脚本使用chatgpt完成。
// @author       coccvo
// @match        https://www.bilibili.com/video/*
// @icon         https://www.bilibili.com/favicon.ico
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
  document.body.addEventListener('keydown', function(event) {
    // 检查按下的键是否为backspace或esc
    if (event.key === 'Backspace' || event.key === 'Escape') {
      // 查找评论区图片关闭按钮并模拟点击
      const closeButton = document.querySelector('.svg-icon.close.use-color');
      if (closeButton) {
        closeButton.click();
      }
    }
    if (event.key === 'ArrowRight' || event.key === '.') {
      // 模拟点击右箭头
      const rightArrow = document.querySelector('.operation-btn-icon.next-image');
      if (rightArrow) {
        rightArrow.click();
      }
    }
    if (event.key === 'ArrowLeft' || event.key === ',') {
      // 模拟点击左箭头
      const leftArrow = document.querySelector('.operation-btn-icon.last-image');
      if (leftArrow) {
        leftArrow.click();
      }
    }
  });
  document.body.addEventListener('wheel', function(event) {
  // 检查滚轮滚动方向
   if (event.deltaY > 0 && !event.altKey) {
    // 模拟点击右箭头
    const rightArrow = document.querySelector('.operation-btn-icon.next-image');
    if (rightArrow) {
      rightArrow.click();
    }
  } else if (event.deltaY < 0) {
    // 模拟点击左箭头
    const leftArrow = document.querySelector('.operation-btn-icon.last-image');
    if (leftArrow) {
      leftArrow.click();
    }
  }
});
  document.body.addEventListener('click', function(event) {
    //点击空白处关闭图片
      if (event.target.closest('.show-image-wrap') &&
    document.querySelector('.operation-btn-icon.close-container')) {
    const closeButton = document.querySelector('.operation-btn-icon.close-container');
    if (closeButton) {
    closeButton.click();
    }
    } else if (!event.target.closest('.show-image-wrap')) {
    // 如果在非看图界面点击,则不执行任何操作
    }
    });
})();