Bilibili 宽屏助手

自动宽屏.(Forked from Bilibili Helper)

  1. // ==UserScript==
  2. // @name Bilibili WideScreen Helper
  3. // @name:zh-CN Bilibili 宽屏助手
  4. // @description Auto widescreen.(Forked from Bilibili Helper)
  5. // @description:zh-CN 自动宽屏.(Forked from Bilibili Helper)
  6. // @namespace bilibili-widescreen-helper
  7. // @version 1.1.1
  8. // @author everbrez
  9. // @author sabertaz
  10. // @license MIT License
  11. // @match *://www.bilibili.com/video/*
  12. // @match *://www.bilibili.com/bangumi/play/*
  13. // @match *://www.bilibili.com/blackboard/*
  14. // @match *://www.bilibili.com/watchlater/*
  15. // @match *://www.bilibili.com/list/*
  16. // @match *://player.bilibili.com/*
  17. // ==/UserScript==
  18.  
  19. "use strict";
  20.  
  21. (function () {
  22. /**
  23. * wait for an element to render
  24. * @usage
  25. * const targetElement = await waitForElement('.target')
  26. * // then do something
  27. * if the rootElement is not exist, waitForElement will throw an error
  28. *
  29. * @param {string} targetSelector the target element query selector
  30. * @param {string} [rootSelector='body'] default search root element: body
  31. * @param {number} [wait] how long to cancal watch this element to render, default: wait forever
  32. * @returns {Promise<Element>} return the target element dom object
  33. */
  34. function waitForElement(targetSelector, rootSelector = 'body', wait) {
  35. const rootElement = document.querySelector(rootSelector);
  36. if (!rootElement) {
  37. console.log('root element is not exist');
  38. return Promise.reject('root element is not exist');
  39. }
  40. // check if the element is already rendered
  41. const targetElement = rootElement.querySelector(targetSelector);
  42. if (targetElement) {
  43. return Promise.resolve(targetElement);
  44. }
  45. return new Promise((resolve, reject) => {
  46. const callback = function (matationList, observer) {
  47. const targetElement = rootElement.querySelector(targetSelector);
  48. if (targetElement) {
  49. // found
  50. resolve(targetElement);
  51. // then cancel to watch the element
  52. observer.disconnect();
  53. }
  54. };
  55. const observer = new MutationObserver(callback);
  56. observer.observe(rootElement, {
  57. subtree: true,
  58. childList: true
  59. });
  60. if (wait !== undefined) {
  61. // if wait is set, then cancel to watch the element to render after wait times
  62. setTimeout(() => {
  63. observer.disconnect();
  64. }, wait);
  65. }
  66. });
  67. }
  68.  
  69. async function autoClickElement(targetSelector, rootSelector, now = false) {
  70. if (now) {
  71. const parent = rootSelector ? document.querySelector(rootSelector) : document;
  72. if (parent) {
  73. const target = parent.querySelector(targetSelector);
  74. if (target) {
  75. target.click();
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. const target = await waitForElement(targetSelector, rootSelector);
  82. target.click();
  83. }
  84.  
  85. function main() {
  86. const selectorList = ['[role="button"][aria-label="宽屏"]'];
  87. selectorList.forEach(selector => autoClickElement(selector));
  88. }
  89.  
  90. main();
  91. })();

QingJ © 2025

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