UserScript Automated Element Text Notifier

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

Verzia zo dňa 17.02.2022. Pozri najnovšiu verziu.

Tento skript by nemal byť nainštalovaný priamo. Je to knižnica pre ďalšie skripty, ktorú by mali používať cez meta príkaz // @require https://update.greatest.deepsurf.us/scripts/438801/1019653/UserScript%20Automated%20Element%20Text%20Notifier.js

  1. // ==UserScript==
  2. // @namespace Xortrox/UserScripts/AutomatedElementTextNotifier
  3. // @name UserScript Automated Element Text Notifier
  4. // @version 0.7
  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. let notified = false;
  47.  
  48. for (let element of notificationElements) {
  49. if (notified) {
  50. break;
  51. }
  52.  
  53. const text = element.innerText;
  54.  
  55. const textLower = text.toLowerCase();
  56.  
  57. for (const config of this.scanConfiguration) {
  58. let includesText = false;
  59. let excludesText = false;
  60.  
  61. for (const includeText of config.includes) {
  62. if (textLower.includes(includeText)) {
  63. includesText = true;
  64. break;
  65. }
  66. }
  67.  
  68. if (config.excludes?.length > 0) {
  69. for (const excludeText of config.excludes) {
  70. if (!textLower.includes(excludeText)) {
  71. excludesText = true;
  72. break;
  73. }
  74. }
  75. }
  76.  
  77. let canShowNotification = includesText;
  78.  
  79. if (config.excludes?.length > 0) {
  80. canShowNotification = canShowNotification && excludesText;
  81. }
  82.  
  83. if (canShowNotification) {
  84. window.UserScript.Notifications.notify(this.title, config.notificationText, this.icon);
  85. notified = true;
  86. break;
  87. }
  88. }
  89. }
  90. }
  91. }
  92.  
  93. error(error) {
  94. alert(error);
  95. console.error(new Error(error));
  96. }
  97. }