UTILS_DOM Library

Eine nützliche Bibliothek für verschiedene Funktionen

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/528456/1545566/UTILS_DOM%20Library.js

  1. // ==UserScript==
  2. // @name UTILS_DOM Library
  3. // @namespace dannysaurus.epik
  4. // @version 1.0
  5. // @description dom manipulation library
  6. //
  7. // @license MIT
  8. // @grant unsafeWindow
  9. // ==/UserScript==
  10.  
  11. /* jslint esversion: 11 */
  12. /* global unsafeWindow */
  13. (() => {
  14. 'use strict';
  15. const selectorsConfig = (() => {
  16. const ids = {
  17. bcasts: 'bcasts',
  18. ctabs: 'ctabs',
  19. ctext: 'ctext',
  20. chatInputWrapper: 'chat-input-wrapper',
  21. broadcastSlots: 'broadcastSlots',
  22. messagesLC: 'messagesLC',
  23. usersLC: 'usersLC',
  24. username: 'username',
  25. };
  26.  
  27. const classNames = {
  28. userSection: 'user-section',
  29. chatSection: 'chat-section',
  30. sidebarSection: 'sidebar-section',
  31. bcstPlayer: 'bcst-player',
  32. userItem: 'user-item',
  33. theIcons: 'the-icons',
  34. faUser: 'fa-user',
  35. userName: 'user-name',
  36. };
  37. const attributes = {
  38. };
  39.  
  40. const querySelectors = {
  41. bcastsContainer: `#${ids.bcasts}`,
  42. ctabsContainer: `#${ids.ctabs}`,
  43. ctextContainer: `#${ids.ctext}`,
  44. chatInputWrapper: `#${ids.chatInputWrapper}`,
  45. broadcastSlots: `#${ids.broadcastSlots}`,
  46. messagesLC: `#${ids.messagesLC}`,
  47. usersLC: `#${ids.usersLC}`,
  48. usernameContainer: `#${ids.username}`,
  49.  
  50. userSection: `.${classNames.userSection}`,
  51. chatSection: `.${classNames.chatSection}`,
  52. sidebarSection: `.${classNames.sidebarSection}`,
  53. bcstPlayer: `.${classNames.bcstPlayer}`,
  54. userItem: `.${classNames.userItem}`,
  55. userIcon: `.${classNames.theIcons} .${classNames.faUser}`,
  56. userName: `.${classNames.userName}`,
  57. };
  58.  
  59. return {
  60. ids,
  61. classNames,
  62. attributes,
  63. querySelectors,
  64. };
  65. })();
  66.  
  67. /**
  68. * Tries to select an element by repeatedly attempting to find it.
  69. *
  70. * @param {Object} options - The options for selecting the element.
  71. * @param {String} [options.selectors] - The function to select the element.
  72. * @param {number} [options.maxAttempts=6] - The number of attempts to make.
  73. * @param {number} [options.intervalMs=10000] - The interval between attempts in milliseconds.
  74. * @returns {Promise<Element|NodeList>} The selected element(s).
  75. *
  76. * @throws {Error} If the element(s) could not be found.
  77. */
  78. const trySelectElement = async ({ selectors, maxAttempts = 6, intervalMs = 10000, throwError = true, } = {}) => {
  79. const sleep = () => new Promise(resolve => setTimeout(resolve, intervalMs));
  80.  
  81. for (let attemptCount = 0; attemptCount < maxAttempts; attemptCount++) {
  82. const elements = document.querySelector(selectors);
  83. if (elements instanceof Element || (elements instanceof NodeList && elements.length)) {
  84. return elements;
  85. }
  86. await sleep();
  87. }
  88. throw new Error(`Element(s) not found with selectors: ${selectors}`);
  89. };
  90.  
  91. const getUsername = () => {
  92. return document.querySelector(selectorsConfig.querySelectors.usernameContainer)?.textContent || '';
  93. };
  94.  
  95. /**
  96. * Checks if the specified keys are pressed.
  97. *
  98. * @param {KeyboardEvent} event - The keyboard event.
  99. * @param {Object} keysToCheck - The keys to check.
  100. * @returns {boolean} True if all specified keys are pressed, false otherwise.
  101. */
  102. const areKeysPressed = (event, keysToCheck) => {
  103. const keys = Object.keys(keysToCheck || []);
  104. if (keys.length === 0) {
  105. return false;
  106. }
  107. return Object.keys(keysToCheck).every(key => event[key] === keysToCheck[key]);
  108. };
  109.  
  110. unsafeWindow.dannysaurus_epik ||= {};
  111. unsafeWindow.dannysaurus_epik.libraries ||= {};
  112. unsafeWindow.dannysaurus_epik.libraries.UTILS_DOM = {
  113. selectors: selectorsConfig,
  114. trySelectElement,
  115. getUsername,
  116. areKeysPressed,
  117. };
  118. })();