Untranslator

костыльная отмена автоперевода

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

Advertisement:

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.

(I already have a user style manager, let me install it!)

Advertisement:

// ==UserScript==
// @name         Untranslator
// @namespace    нету
// @version      0.1
// @description  костыльная отмена автоперевода
// @author       жди
// @match        *://lolz.live/*
// @match        *://zelenka.guru/*
// @match        *://lolz.guru/*
// @license      MIT
// @grant        GM_getValue
// @grant        GM_setValue
// @run-at       document-idle
// ==/UserScript==

(function () {
    'use strict';

    const STORAGE_KEY = 'translated';
    const SHOW_ORIGINAL = 'Показать исходный текст';
    const TRANSLATE_TO_RUSSIAN = 'Перевести на Русский';

    function getTranslated() {
        try {
            const translated = JSON.parse(GM_getValue(STORAGE_KEY, '[]'));
            return Array.isArray(translated) ? translated : [];
        } catch (e) {
            return [];
        }
    }

    function setTranslated(arr) {
        GM_setValue(STORAGE_KEY, JSON.stringify(arr));
    }

    function getOwnerBlock(button) {
        return button.closest('li.comment, li.message');
    }

    function getBlockId(block) {
        return block?.id || '';
    }

    function addTranslated(blockId) {
        if (!blockId) return;

        const translated = getTranslated();

        if (!translated.includes(blockId)) {
            translated.push(blockId);
            setTranslated(translated);
        }
    }

    function removeTranslated(blockId) {
        if (!blockId) return;

        const translated = getTranslated();

        if (translated.includes(blockId)) setTranslated(translated.filter(id => id !== blockId));
    }

    function init() {
        const messageList = document.getElementById('messageList');

        if (!messageList) return;

        function untranslatePosts() {
            const translated = getTranslated();

            const translateButtons = messageList.querySelectorAll(`.TranslateButton[data-cachedtitle="${SHOW_ORIGINAL}"]`);

            for (const translateButton of translateButtons) {
                const block = getOwnerBlock(translateButton);
                const blockId = getBlockId(block);

                if (!blockId || translated.includes(blockId)) continue;

                translateButton.click();
            }
        }

        messageList.addEventListener('click', function (event) {
            const translateButton = event.target.closest('.TranslateButton');

            if (!translateButton || !event.isTrusted) return;

            const block = getOwnerBlock(translateButton);
            const blockId = getBlockId(block);

            if (!blockId) return;

            if (translateButton.getAttribute('data-cachedtitle') === TRANSLATE_TO_RUSSIAN) addTranslated(blockId);
            else removeTranslated(blockId);

        }, true);

        const observer = new MutationObserver(function (mutations) {
            for (const mutation of mutations) {
                if (mutation.type === 'childList' || (mutation.type === 'attributes' && mutation.attributeName === 'data-cachedtitle')) {
                    untranslatePosts();
                    break;
                }
            }
        });

        observer.observe(messageList, {
            childList: true, subtree: true, attributes: true, attributeFilter: ['data-cachedtitle']
        });

        untranslatePosts();
    }

    if (document.readyState === 'loading') window.addEventListener('load', init, {once: true}); else init();
})();