Google Translate Auto Language Enchanced

Automatically set the target language to English based on detected source language changes

目前為 2024-12-12 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Google Translate Auto Language Enchanced
// @namespace   https://greasyfork.org/en/users/1030895-universedev
// @version     2.4
// @author      UniverseDev
// @license     GPL-3.0-or-later
// @description Automatically set the target language to English based on detected source language changes
// @match       *://translate.google.*/*
// @noframes
// ==/UserScript==
"use strict";

const targetLang = "en";

function setTargetLanguage() {
    const selector = `[data-language-code="${targetLang}"]`;
    const targetTab = document.querySelector(selector);
    if (targetTab && targetTab.getAttribute("aria-selected") !== "true") {
        targetTab.click();
    }
}

function observeSourceLanguage() {
    const sourceLangTab = document.querySelector("[role=tablist] [aria-selected='true']");
    if (sourceLangTab) {
        const observer = new MutationObserver(() => {
            setTargetLanguage();
        });
        observer.observe(sourceLangTab, { characterData: true, subtree: true });
    }
}

function init() {
    setTargetLanguage();
    observeSourceLanguage();
}

window.addEventListener('load', init);
window.addEventListener('popstate', init);