MineFun Chat Translator (Vietnamese)

Script tự động dịch tất cả tin nhắn chat trong MineFun sang tiếng Việt

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         MineFun Chat Translator (Vietnamese)
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Script tự động dịch tất cả tin nhắn chat trong MineFun sang tiếng Việt
// @author       THIEN
// @match        https://minefun.io/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=minefun.io
// @run-at       document-start
// @license      ISC
// @grant        none
// ==/UserScript==

// Lấy data attribute cho message
let messageDataAttribute;
document.addEventListener('DOMContentLoaded', async () => {
    const messageDataInterval = setInterval(() => {
        try {
            let css = [...document.styleSheets].find(x => x?.href && x?.href.includes('assets/Game-'));
            if (!css) return;

            let match = [...css.cssRules].find(x => x?.selectorText && x?.selectorText.includes('.messages[data-v-'))?.selectorText;
            if (!match) return;

            messageDataAttribute = match.slice(10, -1);
            clearInterval(messageDataInterval);
        } catch {}
    }, 250);
});

// Hook setAttribute trên div để bắt message
const _createElement = document.createElement;
document.createElement = function createElement() {
    const el = _createElement.apply(this, arguments);
    if (el.tagName !== 'DIV') return el;

    const _setAttribute = el.setAttribute;
    el.setAttribute = function setAttribute() {
        if (messageDataAttribute && arguments[0] === messageDataAttribute) handleMessage(this);
        return _setAttribute.apply(this, arguments);
    }

    return el;
}

// Xử lý dịch message
function handleMessage(el) {
    if (!el || el?.children?.length !== 2) return;
    const userEl = el.children[0];
    const textEl = el.children[1];

    if (!textEl.innerText) return;
    console.log(`Đang dịch tin nhắn: "${textEl.innerText}"`);

    translateMessage(textEl.innerText).then(translated => {
        if (!textEl?.innerText) return;
        textEl.innerText = `[${translated[0]}]: ${translated[1]}`;
    }).catch(err => {
        console.warn('Lỗi dịch...', err);
    });
}

// Cache để tránh dịch trùng
const messageCache = {};

// Gọi Google Translate API để dịch sang tiếng Việt
async function translateMessage(text) {
    if (messageCache[text]) return messageCache[text];

    const res = await fetch("https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=vi&dt=t&q=" + encodeURIComponent(text));
    const data = await res.json();
    const result = [data[2], data[0].map(x => x[0]).join('')];
    messageCache[text] = result;

    return result;
}