Chatlings Auto-Powerup

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

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==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)
    }
})();