Untranslator

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

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

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

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

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

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

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

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

Advertisement:

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

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

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

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

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

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

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

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