bilibili 自动网页全屏

自动网页全屏

  1. // ==UserScript==
  2. // @name bilibili 自动网页全屏
  3. // @author Linda6
  4. // @license Apache-2.0
  5. // @namespace Polar
  6. // @description 自动网页全屏
  7. // @version 1.0.4
  8. // @include *://www.bilibili.com/video/*
  9. // @include *://www.bilibili.com/bangumi/play/*
  10. // @run-at document-start
  11. // @icon https://www.bilibili.com/favicon.ico
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. // 可能的全屏按钮类名或 ID
  16. const FULLSCREEN_BUTTON_NAMES = [
  17. "bpx-player-ctrl-web-enter",
  18. "bilibili-player-iconfont-web-fullscreen-off",
  19. "player_pagefullscreen_player",
  20. "squirtle-pagefullscreen-inactive"
  21. ];
  22. // 不同浏览器的全屏状态变化事件
  23. const FULLSCREEN_EVENTS = [
  24. 'fullscreenchange',
  25. 'webkitfullscreenchange',
  26. 'mozfullscreenchange',
  27. 'MSFullscreenChange'
  28. ];
  29. // 查找元素的最大尝试次数
  30. const MAX_ATTEMPTS = 20;
  31. // 查找元素的检查间隔时间(毫秒)
  32. const CHECK_INTERVAL = 1000;
  33. // 用于保存找到的全屏按钮
  34. let foundFullscreenButton = null;
  35.  
  36. // 页面加载完成后执行的操作
  37. window.addEventListener('load', function () {
  38. attemptFullscreen();
  39. setupFullscreenListeners();
  40. });
  41.  
  42. // 尝试进入全屏模式
  43. function attemptFullscreen() {
  44. // 如果已经保存了全屏按钮,直接点击
  45. if (foundFullscreenButton) {
  46. try {
  47. foundFullscreenButton.click();
  48. } catch (error) {}
  49. return;
  50. }
  51. // 遍历可能的全屏按钮名称,查找并点击按钮
  52. for (let i = 0; i < FULLSCREEN_BUTTON_NAMES.length; i++) {
  53. const elementName = FULLSCREEN_BUTTON_NAMES[i];
  54. const element = waitElement(elementName);
  55. if (element) {
  56. foundFullscreenButton = element;
  57. break;
  58. }
  59. }
  60. }
  61.  
  62. // 设置全屏状态变化的监听器
  63. function setupFullscreenListeners() {
  64. FULLSCREEN_EVENTS.forEach((eventName) => {
  65. document.addEventListener(eventName, function () {
  66. if (!isFullscreen()) {
  67. attemptFullscreen();
  68. }
  69. });
  70. });
  71. }
  72.  
  73. // 检查页面是否处于全屏状态
  74. function isFullscreen() {
  75. return document.fullscreenElement || document.webkitIsFullScreen || document.mozFullScreen || document.msFullscreenElement;
  76. }
  77.  
  78. // 查找并点击指定名称的元素
  79. function waitElement(elementName) {
  80. let attempts = MAX_ATTEMPTS;
  81. let intervalId;
  82. const findAndClick = () => {
  83. if (attempts <= 0) {
  84. clearInterval(intervalId);
  85. return null;
  86. }
  87. attempts--;
  88. let element = document.getElementsByClassName(elementName)[0];
  89. if (!element) {
  90. element = document.getElementById(elementName);
  91. }
  92. if (element) {
  93. try {
  94. element.click();
  95. } catch (error) {}
  96. clearInterval(intervalId);
  97. return element;
  98. }
  99. };
  100. const initialElement = document.getElementsByClassName(elementName)[0] || document.getElementById(elementName);
  101. if (initialElement) {
  102. return findAndClick();
  103. } else {
  104. intervalId = setInterval(findAndClick, CHECK_INTERVAL);
  105. }
  106. return null;
  107. }
  108. })();

QingJ © 2025

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