🏠 Home 

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