Replace BatChest with AYAYA

Replaces BatChest twitch chat emotes with AYAYA

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Replace BatChest with AYAYA
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Replaces BatChest twitch chat emotes with AYAYA
// @author       gami
//
// @include     http://twitch.tv/*
// @include     https://twitch.tv/*
// @include     http://*.twitch.tv/*
// @include     https://*.twitch.tv/*
//
// @exclude     http://api.twitch.tv/*
// @exclude     https://api.twitch.tv/*
//
// @grant       none
// @run-at      document-end
// ==/UserScript==

// Fork of Corrodias' Larger Twitch Emotes script (https://greasyfork.org/en/scripts/419584-larger-twitch-emotes)

(function() {
    'use strict';

    var scriptName = 'Replace BatChest with AYAYA';
    var ayayaSrc = "https://cdn.betterttv.net/frankerfacez_emote/162146/";
    var batChest = "BatChest";

    console.log(`[${scriptName}] Loaded.`);
    onReady(document, function(event) {
        var chat = document.querySelector('.chat-scrollable-area__message-container');
        if (chat) {
            initialize(chat);
            return;
        }

        var callback = function(mutationsList, observer) {
            for (var mutation of mutationsList) {
                mutation.addedNodes.forEach((node) => {
                    var chat = (node.classList && node.classList.contains('chat-scrollable-area__message-container')) ? node : node.querySelector ? node.querySelector('.chat-scrollable-area__message-container') : null;
                    if (chat) {
                        initialize(chat);
                        // Disconnecting seemed to cause some problems, though I don't remember exactly when. It may have been on host/raid channel changes.
                        //observer.disconnect();
                        //console.log(`[${scriptName}] Chat panel observer removed.`);
                        return;
                    }
                });
            }
        };
        var observer = new MutationObserver(callback);
        observer.observe(document, { childList: true, subtree: true });
        console.log(`[${scriptName}] Chat panel observer added.`);
    });

    function initialize(chat) {
        var callback = function(mutationsList, observer) {
            for (var mutation of mutationsList) {
                mutation.addedNodes.forEach((node) => {
                    if (node.classList && node.classList.contains('chat-line__message--emote')) {
                        if(node.getAttribute("alt") == batChest) {
                           runReplace(node);
                        }
                    }
                    var emotes = node.querySelectorAll('.chat-line__message--emote');
                    emotes.forEach((emote) => {
                        if(emote.getAttribute("alt") == batChest) {
                            runReplace(emote);
                        }
                    });
                });
            };
        };
        var observer = new MutationObserver(callback);
        observer.observe(chat, { childList: true, subtree: true });
        console.log(`[${scriptName}] Chat line observer added.`);
    }

    function runReplace(emote) {
        emote.src = ayayaSrc + "1";
        emote.srcset = ayayaSrc + "2 2x, " + ayayaSrc + "4 4x";
    }

    function onReady(element, callback) {
        if (element.readyState!='loading') callback();
        else element.addEventListener('DOMContentLoaded', callback);
    }
})();