自动打开哔哩哔哩字幕

自动打开哔哩哔哩字幕,只在有播放列表时开启,方便看课程,不需要每次都点击字幕

  1. // ==UserScript==
  2. // @name 自动打开哔哩哔哩字幕
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.9
  5. // @description 自动打开哔哩哔哩字幕,只在有播放列表时开启,方便看课程,不需要每次都点击字幕
  6. // @author lisisuidegithub
  7. // @match https://www.bilibili.com/video/*
  8. // @icon https://i0.hdslb.com/bfs/static/jinkela/long/images/favicon.ico
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. let interval; // 声明轮询变量
  17.  
  18. // 等待页面完全加载完毕后执行脚本
  19. window.onload = function() {
  20. let lastUrl = window.location.href; // 存储上一个 URL
  21.  
  22. // 初始检测播放列表和字幕
  23. checkAndOpenSubtitle();
  24.  
  25. // 监听 DOM 变化
  26. const observer = new MutationObserver(() => {
  27. const currentUrl = window.location.href;
  28.  
  29. // 检查 URL 是否发生变化
  30. if (currentUrl !== lastUrl) {
  31. console.log('URL发生变化');
  32. lastUrl = currentUrl; // 更新上一个 URL
  33.  
  34. clearInterval(interval); // 停止当前轮询
  35. checkAndOpenSubtitle(); // 重新检测播放列表和字幕
  36. }
  37. });
  38.  
  39. // 开始观察 DOM 变化,监听整个页面的变化
  40. observer.observe(document.body, { childList: true, subtree: true });
  41.  
  42. function checkAndOpenSubtitle() {
  43. // 检测是否存在 ul.list-box 元素(播放列表)
  44. const listBox = document.querySelector('ul.list-box');
  45. if (listBox) {
  46. console.log('存在播放列表');
  47. clearInterval(interval); // 确保清除旧的轮询
  48. waitForSubtitleButton();
  49. }
  50. }
  51.  
  52. // 轮询检测列表和字幕
  53. function waitForSubtitleButton() {
  54. const maxAttempts = 10; // 设置最大尝试次数
  55. let attempts = 0;
  56.  
  57. interval = setInterval(() => {
  58. attempts++;
  59. console.log(`第${attempts}次尝试`)
  60. if (attempts >= maxAttempts) {
  61. console.log('尝试次数过多,停止轮询');
  62. console.log('不存在字幕');
  63. clearInterval(interval);
  64. }
  65. const subtitleButton = document.querySelector('.bpx-player-ctrl-btn.bpx-player-ctrl-subtitle[aria-label="字幕"] .bpx-player-ctrl-btn-icon .bpx-common-svg-icon');
  66.  
  67. // 检测字幕filter是否存在,来判断字幕是否开启
  68. const subtitleFilter = document.querySelector('.bpx-player-ctrl-btn.bpx-player-ctrl-subtitle[aria-label="字幕"] .bpx-player-ctrl-btn-icon .bpx-common-svg-icon filter[id^="__lottie_element_"]');
  69. if (subtitleFilter) {
  70. console.log('字幕已开启!');
  71. clearInterval(interval);
  72. }
  73.  
  74. subtitleButton.click(); // 点击字幕按钮
  75.  
  76. }, 1000); // 每秒检测一次字幕按钮
  77. }
  78. };
  79. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址