Untranslator

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

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