UserScript Automated Element Text Notifier

This script is intended to work with @require only. Provides class AutomatedElementTextNotifier

Version vom 19.01.2022. Aktuellste Version

Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greatest.deepsurf.us/scripts/438801/1010232/UserScript%20Automated%20Element%20Text%20Notifier.js

  1. // ==UserScript==
  2. // @namespace Xortrox/UserScripts/AutomatedElementTextNotifier
  3. // @name UserScript Automated Element Text Notifier
  4. // @version 0.6
  5. // @description This script is intended to work with @require only. Provides class AutomatedElementTextNotifier
  6. // @author Xortrox,
  7. // @match *
  8. // @esversion: 6
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. class AutomatedElementTextNotifier {
  13. constructor(options) {
  14. this.icon = options.icon || '';
  15. this.title = options.title || 'No Title Specified';
  16.  
  17. this.selector = options.selector;
  18.  
  19. /** How frequently to scan for changes on the website (in milliseconds) */
  20. this.notificationInterval = options.interval;
  21.  
  22.  
  23. /**
  24. * Every includes/excludes does a check for selector matched element's "innerText" value
  25. * Each entry will be checked in priority from top to bottom and only the first match will send its notification
  26. * All configs must have an includes entry, while exclusion is optional.
  27. * */
  28. this.scanConfiguration = options.config;
  29.  
  30. if (!this.selector) {
  31. this.error('Error: No selector specified for AutomatedElementTextNotifier');
  32. }
  33. }
  34.  
  35. async init() {
  36. await window.UserScript.Notifications.askPermission();
  37.  
  38. this.interval = setInterval(this.scan, this.notificationInterval);
  39. }
  40.  
  41. scan = () => {
  42. const notificationElements = document.querySelectorAll(this.selector);
  43.  
  44. /** We send notification only once if any adventures are claimable */
  45. if (notificationElements && notificationElements.length > 0) {
  46. for (let element of notificationElements) {
  47. const text = element.innerText;
  48.  
  49. const textLower = text.toLowerCase();
  50.  
  51. for (const config of this.scanConfiguration) {
  52. let includesText = false;
  53. let excludesText = false;
  54.  
  55. for (const includeText of config.includes) {
  56. if (textLower.includes(includeText)) {
  57. includesText = true;
  58. break;
  59. }
  60. }
  61.  
  62. if (config.excludes?.length > 0) {
  63. for (const excludeText of config.excludes) {
  64. if (!textLower.includes(excludeText)) {
  65. excludesText = true;
  66. break;
  67. }
  68. }
  69. }
  70.  
  71. let canShowNotification = includesText;
  72.  
  73. if (config.excludes?.length > 0) {
  74. canShowNotification = canShowNotification && excludesText;
  75. }
  76.  
  77. if (canShowNotification) {
  78. window.UserScript.Notifications.notify(this.title, config.notificationText, this.icon);
  79. break;
  80. }
  81. }
  82. }
  83. }
  84. }
  85.  
  86. error(error) {
  87. alert(error);
  88. console.error(new Error(error));
  89. }
  90. }