Chatlings Auto-Powerup

Tired of missing powerups when you're active, but look away for a few seconds? Chatling Auto-Powerup is for you!

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

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

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==
// @name         Chatlings Auto-Powerup
// @namespace    http://tampermonkey.net/
// @version      1.02
// @description  Tired of missing powerups when you're active, but look away for a few seconds? Chatling Auto-Powerup is for you!
// @author       Grayson Powers
// @match        https://www.twitch.tv/j0ko
// @icon         https://www.google.com/s2/favicons?sz=64&domain=twitch.tv
// @grant        none
// @license MIT
// ==/UserScript==

(() => {
    const overlord = 'j0ko_bot';
    const messageTriggers = [
        { text: 'Type !boost within 15 seconds to get a temporary powerup!', response: '!boost' },
        { text: 'FEEDING FRENZY starting in 30 seconds! Type !frenzy to join!', response: '!frenzy' },
        { text: 'Boss spawning in 10 seconds! Type !riot for a temporary damage buff!', response: '!riot' }
    ];

    window.addEventListener('load', async () => {
        const chat = document.querySelector('.chat-scrollable-area__message-container');

        await startObservable(chat, (mutation) => {
            let message = getMessageFromElement(mutation.addedNodes[0]);

            messageTriggers.forEach(trigger => {
                if (message.user === overlord && message.text.indexOf(trigger.text) != -1) {
                    try {
                        setInputValue(trigger.response, true);
                    } catch (error) {
                        console.log(`Failed to ${trigger.response}`, error);
                    }
                }
            });
        });
    }, false);

    function startObservable(targetNode, action) {
        var observerConfig = {
            childList: true
        };

        return new Promise((resolve) => {
            var observer = new MutationObserver(function (mutations) {
                mutations.forEach(function (mutation) {
                    action(mutation);
                });

                resolve(mutations);
            });
            observer.observe(targetNode, observerConfig);
        });
    };

    function getMessageFromElement(element) {
        if (!element) return {}

        const usernameEle = element.querySelector('.chat-author__display-name');
        const textEle = element.querySelector('.text-fragment');

        if (!usernameEle || !textEle) return { user: 'unknown', text: '[ERROR: Failed to get elements]'}

        let message = {
            user: usernameEle.textContent,
            text: textEle.textContent
        };

        // console.log(message);

        return message;
    }

    function setInputValue(text) {
        const inputElement = document.querySelector('.chat-input__textarea');
        const reactEventHandlerName = Object.getOwnPropertyNames(inputElement)[1];
        const reactInputContext = inputElement[reactEventHandlerName].children[0];

        reactInputContext.props.setInputValue(text, true)
    }
})();