UserScript Automated Element Text Notifier

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

Stan na 19-01-2022. Zobacz najnowsza wersja.

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/438801/1010229/UserScript%20Automated%20Element%20Text%20Notifier.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @namespace    Xortrox/UserScripts/AutomatedElementTextNotifier
// @name         UserScript Automated Element Text Notifier
// @version      0.5
// @description  This script is intended to work with @require only. Provides class AutomatedElementTextNotifier
// @author       Xortrox,
// @match        *
// @esversion:   6
// @license MIT
// ==/UserScript==

class AutomatedElementTextNotifier {
    constructor(options) {
        this.icon = options.icon || '';
        this.title = options.title || 'No Title Specified';

        this.selector = options.selector;

        /** How frequently to scan for changes on the website (in milliseconds) */
        this.notificationInterval = options.interval;


        /**
         * Every includes/excludes does a check for selector matched element's "innerText" value
         * Each entry will be checked in priority from top to bottom and only the first match will send its notification
         * All configs must have an includes entry, while exclusion is optional.
         * */
        this.scanConfiguration = options.config;

        if (!this.selector) {
            this.error('Error: No selector specified for AutomatedElementTextNotifier');
        }
    }

    async init() {
        await window.UserScript.Notifications.askPermission();

        this.interval = setInterval(this.scan, this.notificationInterval);
    }

    scan = () => {
        const notificationElements = document.querySelectorAll(this.selector);

        /** We send notification only once if any adventures are claimable */
        if (notificationElements && notificationElements.length > 0) {
            for (let element of notificationElements) {
                const text = element.innerText;

                const textLower = text.toLowerCase();

                for (const config of this.scanConfiguration) {
                    let includesText = false;
                    let excludesText = false;

                    for (const includeText of config.includes) {
                        if (textLower.includes(includeText)) {
                            includesText = true;
                            break;
                        }
                    }

                    if (config.excludes?.length > 0) {
                        for (const excludeText of config.includes) {
                            if (!textLower.includes(excludeText)) {
                                excludesText = true;
                                break;
                            }
                        }
                    }

                    let canShowNotification = includesText;

                    if (config.excludes?.length > 0) {
                        canShowNotification = canShowNotification && excludesText;
                    }

                    if (canShowNotification) {
                        window.UserScript.Notifications.notify(this.title, config.notificationText, this.icon);
                        break;
                    }
                }
            }
        }
    }

    error(error) {
        alert(error);
        console.error(new Error(error));
    }
}