Block Twitch Emotes - IOS Block button.

Twitch emoji blocking via a channel with a management interface and context menu (via Tampermonkey)

目前为 2024-12-26 提交的版本。查看 最新版本

// ==UserScript==
// @name         Block Twitch Emotes - IOS Block button.
// @namespace    http://tampermonkey.net/
// @version      1.7.2
// @description  Twitch emoji blocking via a channel with a management interface and context menu (via Tampermonkey)
// @author
// @license      MIT
// @match        https://www.twitch.tv/*
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// @icon         https://cdn-icons-png.flaticon.com/512/10531/10531738.png
// ==/UserScript==

(function () {
    'use strict';

    // Загружаем список заблокированных каналов и состояние панели из хранилища
    let blockedChannels = GM_getValue('blockedChannels', []); // Массив заблокированных каналов
    let isPanelVisible = GM_getValue('isPanelVisible', false); // Флаг, указывающий, видна ли панель

//----------------------------------- Панель управления -----------------------------------------//
    const controlPanel = document.createElement('div');
    controlPanel.style.position = 'fixed'; // Фиксируем панель на экране
    controlPanel.style.bottom = '124px'; // Располагаем панель на 124px от нижней границы экрана
    controlPanel.style.right = '310px'; // Располагаем панель на 310px от правой границы экрана
    controlPanel.style.width = '360px'; // Ширина панели
    controlPanel.style.height = '370px'; // Высота панели
    controlPanel.style.backgroundColor = '#5c5065'; // Цвет фона панели
    controlPanel.style.border = '1px solid #ccc'; // Цвет и стиль границы панели
    controlPanel.style.borderRadius = '8px'; // Скругляем углы панели
    controlPanel.style.padding = '10px'; // Отступы внутри панели
    controlPanel.style.boxShadow = '0 4px 6px rgba(0, 0, 0, 0.1)'; // Добавляем тень панели
    controlPanel.style.zIndex = 10000; // Устанавливаем высокий z-index, чтобы панель была поверх других элементов
    controlPanel.style.fontFamily = 'Arial, sans-serif'; // Шрифт текста на панели
    controlPanel.style.transition = 'height 0.3s ease'; // Плавное изменение высоты при изменении
    controlPanel.style.overflow = 'hidden'; // Скрытие содержимого, если оно выходит за пределы панели


//--------------- Название листа список ------------------------//
    const title = document.createElement('h4');
    title.innerText = 'list of channels';
    title.style.margin = '0 0 10px 0';
    title.style.color = '#fff';
    controlPanel.appendChild(title);


//--------------- Список заблокированных каналов ------------------//
const list = document.createElement('ul');
list.id = 'blockedChannelsList';
list.style.listStyle = 'none';
list.style.padding = '0';
list.style.margin = '0 0 10px 0';
list.style.maxHeight = '230px';
list.style.height = '550px';
list.style.overflowY = 'auto';
list.style.border = '1px solid #ddd';
list.style.borderRadius = '4px';
list.style.maxHeight = '230px'; // Установите максимальную высоту для списка
list.style.overflowY = 'auto'; // Включите вертикальную прокрутку


//---------------- Применяем новый градиентный фон ------------------------//
list.style.background = 'linear-gradient(45deg, hsla(292, 44%, 16%, 1) 0%, hsla(173, 29%, 48%, 1) 100%)';
list.style.background = '-moz-linear-gradient(45deg, hsla(292, 44%, 16%, 1) 0%, hsla(173, 29%, 48%, 1) 100%)'; // Для Firefox
list.style.background = '-webkit-linear-gradient(45deg, hsla(292, 44%, 16%, 1) 0%, hsla(173, 29%, 48%, 1) 100%)'; // Для Safari и Chrome
list.style.filter = 'progid: DXImageTransform.Microsoft.gradient(startColorstr="#36173b", endColorstr="#589F97", GradientType=1)'; // Для старых версий IE
list.style.color = '#fff';

const buttonColor = '#907cad'; // Общий цвет для кнопок //
const buttonShadow = '0 4px 8px rgba(0, 0, 0, 0.6)'; // Тень для кнопок (60% прозрачности) //


//====== Обновляет список заблокированных каналов, перерисовывая его в DOM ================//
//====== Функция очищает текущий список и заново добавляет элементы для каждого канала в массиве blockedChannels =======//
function updateChannelList() {
    // Очищаем текущий список, чтобы обновить его с новыми данными //
    list.innerHTML = '';

    // Проходим по всем заблокированным каналам //
    blockedChannels.forEach(channel => {
        //---------------- Создаем элемент списка для каждого канала ----------------//
        const item = document.createElement('li');
        item.style.display = 'flex'; // Располагаем элементы в строку
        item.style.justifyContent = 'space-between'; // Разделяем элементы по краям //
        item.style.padding = '5px'; // Добавляем отступы //
        item.style.borderBottom = '1px solid #eee'; // Легкая граница между элементами списка //

        // Создаем текстовый элемент для имени канала
        const channelName = document.createElement('span');
        channelName.innerText = channel; // Устанавливаем название канала //
        item.appendChild(channelName); // Добавляем его в элемент списка //

        // Создаем кнопку для Удаления канала
        const removeButton = document.createElement('button');
        removeButton.innerText = 'Delete'; // Текст кнопки //
        removeButton.style.background = '#ff4d4d'; // Цвет фона кнопки (красный) //
        removeButton.style.color = '#fff'; // Цвет текста кнопки (белый) //
        removeButton.style.border = 'none'; // Без рамки //
        removeButton.style.borderRadius = '4px'; // Округляем углы кнопки //
        removeButton.style.padding = '2px 6px'; // Отступы внутри кнопки //
        removeButton.style.cursor = 'pointer'; // Курсор при наведении //
        removeButton.style.boxShadow = buttonShadow; // Тень для кнопки (предположительно переменная) //

        // При нажатии на кнопку удаляем канал из списка блокированных
        removeButton.onclick = function () {
            // Обновляем массив blockedChannels, исключив выбранный канал
            blockedChannels = blockedChannels.filter(c => c !== channel);

            // Сохраняем обновленный список в хранилище
            GM_setValue("blockedChannels", blockedChannels);

            // Обновляем отображение списка каналов и счетчик
            updateChannelList();
            updateCounter();
        };

        // Добавляем кнопку удаления в элемент списка
        item.appendChild(removeButton);

        // Добавляем элемент списка в основной список
        list.appendChild(item);
    });
}

//------------------ Функционал для добавления нового канала в список заблокированных -----------//
const inputContainer = document.createElement('div');
inputContainer.style.display = 'flex';
inputContainer.style.gap = '5px';

const input = document.createElement('input');
input.type = 'text';
input.placeholder = 'type channel name';
input.style.flex = '1';
input.style.padding = '5px';
input.style.border = '1px solid #ccc';
input.style.borderRadius = '4px';

//----------------- Add it Button ----------------------//
const addButton = document.createElement('button');
addButton.innerText = 'Add it';
addButton.style.background = buttonColor;
addButton.style.color = '#fff';
addButton.style.border = 'none';
addButton.style.borderRadius = '4px';
addButton.style.padding = '5px 10px';
addButton.style.cursor = 'pointer';
addButton.style.boxShadow = buttonShadow; // Тень для кнопки "Add it"
addButton.onclick = (event) => {
    event.preventDefault();
    const channel = input.value.trim();
    if (channel && !blockedChannels.includes(channel)) {
        blockedChannels.push(channel);
        GM_setValue("blockedChannels", blockedChannels);
        updateChannelList();
        updateCounter();
        input.value = '';
    }
};


//----------------Единый контейнер для кнопок -------------------------//
const buttonContainer = document.createElement('div');
buttonContainer.style.display = 'flex'; // Используем flexbox для расположения кнопок в строку
buttonContainer.style.gap = '6px'; // Задаем промежуток между кнопками
buttonContainer.style.marginTop = '10px'; // Отступ сверху для контейнера кнопок

//-------------- Кнопка "Delete all" ------------------------//
const clearAllButton = document.createElement('button');
clearAllButton.innerText = 'Delete all'; // Текст на кнопке
clearAllButton.style.background = buttonColor; // Цвет фона кнопки
clearAllButton.style.color = '#fff'; // Цвет текста кнопки
clearAllButton.style.border = 'none'; // Убираем бордер у кнопки
clearAllButton.style.borderRadius = '4px'; // Скругленные углы кнопки
clearAllButton.style.padding = '5px 10px'; // Отступы внутри кнопки
clearAllButton.style.cursor = 'pointer'; // Курсор в виде руки при наведении
clearAllButton.style.boxShadow = buttonShadow; // Тень для кнопки "Delete all"

buttonContainer.appendChild(clearAllButton); // Добавляем кнопку в контейнер

// Обработчик события для кнопки "Delete all"
clearAllButton.onclick = () => {
    blockedChannels = []; // Очищаем массив заблокированных каналов
    GM_setValue("blockedChannels", blockedChannels); // Сохраняем обновленный массив в хранилище
    updateChannelList(); // Обновляем список каналов
    updateCounter(); // Обновляем счетчик
};


//----------------- export Button --------------------//
const exportButton = document.createElement('button');
exportButton.innerText = 'Export';
exportButton.style.background = buttonColor;
exportButton.style.color = '#fff';
exportButton.style.border = 'none';
exportButton.style.borderRadius = '4px';
exportButton.style.padding = '5px 10px';
exportButton.style.cursor = 'pointer';
exportButton.style.boxShadow = buttonShadow; // Тень для кнопки "Export"
buttonContainer.appendChild(exportButton);
exportButton.onclick = () => {
    const blob = new Blob([JSON.stringify(blockedChannels, null, 2)], { type: 'application/json' });
    const url = URL.createObjectURL(blob);
    const link = document.createElement('a');
    link.href = url;
    link.download = 'blocked_channels.json';
    link.click();
    URL.revokeObjectURL(url);
};

//----------------- importButton --------------------//
const importButton = document.createElement('button');
importButton.innerText = 'Import';
importButton.style.background = buttonColor;
importButton.style.color = '#fff';
importButton.style.border = 'none';
importButton.style.borderRadius = '4px';
importButton.style.padding = '5px 10px';
importButton.style.cursor = 'pointer';
importButton.style.boxShadow = buttonShadow; // Тень для кнопки "Import"
buttonContainer.appendChild(importButton);
importButton.onclick = () => {
    const fileInput = document.createElement('input');
    fileInput.type = 'file';
    fileInput.accept = 'application/json';
    fileInput.onchange = (event) => {
        const file = event.target.files[0];
        const reader = new FileReader();
        reader.onload = (e) => {
            try {
                const importedChannels = JSON.parse(e.target.result);
                if (Array.isArray(importedChannels)) {
                    blockedChannels = Array.from(new Set([...blockedChannels, ...importedChannels]));
                    GM_setValue("blockedChannels", blockedChannels);
                    updateChannelList();
                    updateCounter();
                } else {
                    alert('Invalid file format!');
                }
            } catch {
                alert('Error reading file!');
            }
        };
        reader.readAsText(file);
    };
    fileInput.click();
};

//----------------- Счётчик ---------------------//
const counter = document.createElement('div');
counter.style.display = 'inline-block';
counter.style.backgroundColor = '#fff';
counter.style.color = '#4c2a5e';
counter.style.border = '1px solid #ccc';
counter.style.borderRadius = '8px';
counter.style.padding = '5px 10px';
counter.style.marginLeft = '6px';
counter.style.fontWeight = 'bold';
buttonContainer.appendChild(counter);

function updateCounter() {
    counter.innerText = `Count: ${blockedChannels.length}`;
}

inputContainer.appendChild(input);
inputContainer.appendChild(addButton);
controlPanel.appendChild(inputContainer);
controlPanel.appendChild(buttonContainer);

document.body.appendChild(controlPanel);



// Создаем кнопку "Open Blocker Emote"
const openPanelButton = document.createElement('button');
openPanelButton.innerText = 'Open Blocker Emote';
openPanelButton.style.fontWeight = 'bold';
openPanelButton.style.top = '23px';
openPanelButton.style.right = '1344px';
openPanelButton.style.position = 'fixed'; // Фиксированное положение
openPanelButton.style.width = '200px'; // Фиксированная ширина кнопки
openPanelButton.style.height = '41px'; // Фиксированная высота кнопки
openPanelButton.style.background = '#4c2a5e'; // Цвет кнопки в выключенном состоянии
openPanelButton.style.color = '#fff';
openPanelButton.style.border = 'none'; // Без границ
openPanelButton.style.borderRadius = '20px'; // Закругленные углы
openPanelButton.style.padding = '10px';
openPanelButton.style.cursor = 'pointer';
openPanelButton.style.zIndex = 10000; // Высокий z-index
openPanelButton.style.transition = 'background 0.3s ease'; // Плавное изменение фона
openPanelButton.style.display = 'flex';
openPanelButton.style.alignItems = 'center';
openPanelButton.style.justifyContent = 'space-between'; // Чтобы текст и переключатель были по разным краям

// Создаем контейнер для переключателя (темная рамка)
const switchContainer = document.createElement('div');
switchContainer.style.width = '44px'; // Увеличиваем ширину контейнера на 6px
switchContainer.style.height = '27px'; // Увеличиваем высоту контейнера на 6px
switchContainer.style.borderRadius = '13px'; // Скругленные углы
switchContainer.style.backgroundColor = '#9078a7'; // Темно-зеленая рамка для кружка
switchContainer.style.position = 'relative'; // Для абсолютного позиционирования кружка
switchContainer.style.transition = 'background 0.3s ease'; // Плавное изменение фона контейнера
openPanelButton.appendChild(switchContainer);

// Создаем фиолетовый кружок (переключатель)
const switchCircle = document.createElement('div');
switchCircle.style.width = '19px'; // Увеличиваем ширину кружка на 3px
switchCircle.style.height = '19px'; // Увеличиваем высоту кружка на 3px
switchCircle.style.borderRadius = '50%'; // Кружок
switchCircle.style.backgroundColor = '#4c2a5e'; // фиолетовый цвет кружка
switchCircle.style.boxShadow = '0 2px 6px rgba(0, 0, 0, 0.8)'; // Тень для кружка
switchCircle.style.position = 'absolute'; // Абсолютное позиционирование внутри контейнера
switchCircle.style.top = '3px'; // Отступ сверху
switchCircle.style.left = '3px'; // Отступ слева
switchCircle.style.transition = 'transform 0.3s ease'; // Плавное движение
switchContainer.appendChild(switchCircle);



// Устанавливаем начальное состояние Панели //
let isPanelOpen = false;

// Обработчик клика для изменения состояния Панели//
openPanelButton.onclick = () => {
    isPanelOpen = !isPanelOpen;
    if (isPanelOpen) {
        openPanelButton.style.background = '#4c2a5e'; // Включено
        switchCircle.style.transform = 'translateX(20px)'; // Перемещаем кружок вправо
        switchContainer.style.backgroundColor = '#3c995d'; // Цвет контейнера в включенном состоянии
        controlPanel.style.display = 'block'; // Показать панель
    } else {
        openPanelButton.style.background = '#4c2a5e'; // Выключено
        switchCircle.style.transform = 'translateX(0)'; // Перемещаем кружок влево
        switchContainer.style.backgroundColor = '#907cad'; // Цвет контейнера в выключенном состоянии
        controlPanel.style.display = 'none'; // Скрыть панель
    }
    GM_setValue('isPanelOpen', isPanelOpen); // Сохранить состояние
};

//---------- Добавляем кнопку в DOM только после полной загрузки страницы -------//
window.addEventListener('load', () => {
    document.body.appendChild(openPanelButton);

    // Устанавливаем начальное положение кнопки
    const updateButtonPosition = () => {
        const windowWidth = window.innerWidth;
        const windowHeight = window.innerHeight;

        // Позиция кнопки (например, 5% от высоты и 10% от ширины)
        openPanelButton.style.top = `${windowHeight * 0.01}px`; // 1% от высоты окна
        openPanelButton.style.right = `${windowWidth * 0.2}px`; // 20% от ширины окна
    };

    // Устанавливаем положение кнопки сразу
    updateButtonPosition();

    // Обновляем положение кнопки при изменении размеров окна
    window.addEventListener('resize', updateButtonPosition);
});


//----------------------- Функции для скрытия смайлов ---------------//
function hideEmotesForChannel(node) {
    const emotes = node.querySelectorAll('.chat-line__message img');
    emotes.forEach(emote => {
        const emoteName = emote.getAttribute('alt');
        if (emoteName && isEmoteFromBlockedChannel(emoteName)) {
            emote.style.display = 'none';
        }
    });
}

function isEmoteFromBlockedChannel(emoteName) {
    return blockedChannels.some(channel => emoteName.includes(channel));
}

const observer = new MutationObserver(mutations => {
    mutations.forEach(mutation => {
        mutation.addedNodes.forEach(node => {
            if (node.nodeType === 1) hideEmotesForChannel(node);
        });
    });
});



//--------------- Функция ожидания появления контейнера чата для отслеживания изменений -----------//
function waitForChatContainerAndObserve() {
    const chatContainer = document.querySelector('.chat-scrollable-area__message-container');
    if (chatContainer) {
        // Запуск наблюдателя, если контейнер чата найден
        observer.observe(chatContainer, { childList: true, subtree: true });
    } else {
        // Повторная попытка через 10 миллисекунд, если контейнер не найден
        setTimeout(waitForChatContainerAndObserve, 1);
    }
}

waitForChatContainerAndObserve();

// --------------- Периодическая проверка скрыта ли эмодзи, которые могли быть перерисованы ----------------//
setInterval(() => {
    const chatContainer = document.querySelector('.chat-scrollable-area__message-container');
    if (chatContainer) {
        const nodes = chatContainer.querySelectorAll('.chat-line__message');
        nodes.forEach(node => hideEmotesForChannel(node));
    }
}, 1); // Проверка каждую секунду //



//----------------- Анимация сворачивания панели-------------------------//
function openPanel() {
    isPanelVisible = true;
    GM_setValue('isPanelVisible', isPanelVisible);
    controlPanel.style.display = 'block'; // Делаем панель видимой
    setTimeout(() => {
        controlPanel.style.height = '370px'; // Плавно увеличиваем высоту //
    }, 0); // Устанавливаем высоту с задержкой для работы анимации //
}

//-------------------------------- Управление высотой панели ---------------//
function closePanel() {
    isPanelVisible = false;
    GM_setValue('isPanelVisible', isPanelVisible);
    controlPanel.style.height = '0px'; // Плавно уменьшаем высоту
    setTimeout(() => {
        if (!isPanelVisible) controlPanel.style.display = 'none'; // Полностью скрываем после завершения анимации
    }, 150); // Таймер соответствует времени анимации //
}

openPanelButton.onclick = () => {
    if (isPanelVisible) {
        closePanel();
    } else {
        openPanel();
    }
};


if (isPanelVisible) {
    controlPanel.style.display = 'block';
    controlPanel.style.height = '370px'; // Высота открытой панели //
} else {
    controlPanel.style.display = 'block'; // Оставляем display: block для анимации //
    controlPanel.style.height = '0px'; // Высота скрытой панели //
}


    if (isPanelVisible) controlPanel.style.display = 'block';
    else controlPanel.style.display = 'none';

    updateChannelList();
    updateCounter();


const ContextMenuManager = {
    menu: null,


//--------- Создать контекстное меню ------------//
    createMenu(event, emotePrefix) {
        this.removeMenu(); // Удалить существующее меню, если есть

        // Создание нового контекстного меню
        const menu = document.createElement('div');
        menu.className = 'custom-context-menu';
        menu.style.position = 'absolute';
        menu.style.top = `${event.pageY}px`;
        menu.style.left = `${event.pageX}px`;
        menu.style.background = '#4c2a5e';
        menu.style.border = '1px solid #ccc';
        menu.style.padding = '5px';
        menu.style.zIndex = 10001;
        menu.style.cursor = 'pointer';
        menu.innerText = `Block Emote (${emotePrefix})`;

        document.body.appendChild(menu);
        this.menu = menu;

        //------ Обработчик клика на меню -------------//
        menu.addEventListener('click', () => {
            this.blockEmote(emotePrefix);
        });

        //---------- Удалить меню при клике в любое другое место --------------------//
        document.addEventListener('click', () => this.removeMenu(), { once: true });
    },

//---------- Удалить контекстное меню ------------//
    removeMenu() {
        if (this.menu) {
            this.menu.remove();
            this.menu = null;
        }
    },

//--------------- Логика блокировки эмодзи ----------------//
    blockEmote(emotePrefix) {
        if (!blockedChannels.includes(emotePrefix)) {
            blockedChannels.push(emotePrefix);
            GM_setValue("blockedChannels", blockedChannels);
            updateChannelList();
            updateCounter();
        }
        this.removeMenu();
    }
};

//-------------- Основной обработчик контекстного меню ---------------------//
document.addEventListener('contextmenu', (event) => {
    const target = event.target;
    if (target.tagName === 'IMG' && target.closest('.chat-line__message')) {
        event.preventDefault();

        const emoteAlt = target.getAttribute('alt');
        if (emoteAlt) {
            const emotePrefix = emoteAlt.split(/[^a-zA-Z0-9]/)[0];
            ContextMenuManager.createMenu(event, emotePrefix);
        }
    }
});


     openPanelButton.onclick = () => {
    isPanelOpen = !isPanelOpen;

    // Переключаем фоновый цвет кнопки
    openPanelButton.style.background = isPanelOpen ? '#4c2a5e' : '#4c2a5e'; // Цвет кнопки для каждого состояния
    // Перемещаем переключатель
    switchCircle.style.transform = isPanelOpen ? 'translateX(20px)' : 'translateX(0)';
    // Меняем цвет фона контейнера
    switchContainer.style.backgroundColor = isPanelOpen ? '#3c995d' : '#907cad';

    // Переключаем состояние панели
    if (isPanelOpen) {
        openPanel(); // Разворачиваем панель
    } else {
        closePanel(); // Сворачиваем панель
    }

    // Сохраняем состояние панели в localStorage
    GM_setValue('isPanelOpen', isPanelOpen);
};

})();