Untranslator

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

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

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