Google谷歌翻译自动中英互译

自动切换目标语言为中/英文

当前为 2021-08-04 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Google Translate Auto Languages
// @name:zh-CN  Google谷歌翻译自动中英互译
// @description Auto switch Chinese/English
// @description:zh-CN 自动切换目标语言为中/英文
// @namespace   https://greasyfork.org/users/197529
// @version     1.5
// @author      kkocdko
// @license     Unlicense
// @match       *://translate.google.com/*
// @match       *://translate.google.cn/*
// @noframes
// ==/UserScript==
"use strict";

const firstLangRule = /English|英语/;
const firstLang = "en";
const secondLang = "zh-CN";

const inputBox = document.querySelector("textarea");
const detectTab = document.querySelector("[role=tab]");
let prevIsFirstLang = null;
const switchLang = () => {
  if (inputBox.value === "") return;
  const isFirstLang = firstLangRule.test(detectTab.textContent);
  if (isFirstLang === prevIsFirstLang) return;
  prevIsFirstLang = isFirstLang;
  const lang = isFirstLang ? secondLang : firstLang;
  const selector = `[data-popup-corner]~* [data-language-code=${lang}]`;
  const tab = document.querySelector(selector);
  if (tab.getAttribute("aria-selected") !== "true") tab.click();
};
const options = { characterData: true, subtree: true };
new MutationObserver(switchLang).observe(detectTab, options);
if (detectTab.getAttribute("aria-selected") !== "true") detectTab.click();