UserScript Automated Button Text Notifier

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

Tính đến 19-01-2022. Xem phiên bản mới nhất.

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://update.greatest.deepsurf.us/scripts/438801/1010219/UserScript%20Automated%20Button%20Text%20Notifier.js

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