Google谷歌翻译自动中英互译

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

旧版: v0.6 - 2019-08-05 - 1. 修复了各种奇怪的Bug 2. 精简代码
新版: v0.7 - 2019-08-09 - 匹配各国的Google Translate域名

  • --- /tmp/diffy20240513-429615-45bpgv 2024-05-13 22:55:13.081089019 +0000
  • +++ /tmp/diffy20240513-429615-97e62k 2024-05-13 22:55:13.081089019 +0000
  • @@ -1,11 +1,11 @@
  • // ==UserScript==
  • -// @name Google翻译自动中英互译
  • +// @name Google谷歌翻译自动中英互译
  • // @description 自动切换输出语言
  • // @namespace https://greasyfork.org/users/197529
  • // @author kkocdko
  • // @license Unlicense
  • -// @version 0.6
  • -// @include *://translate.google.cn/*
  • +// @version 0.7
  • +// @match *://translate.google.*/*
  • // ==/UserScript==
  • 'use strict'
  • @@ -20,10 +20,10 @@
  • return
  • }
  • const isEnglish = sourceLangEl.textContent === '检测到英语'
  • - const rightValue = isEnglish ? 'tl=zh-CN' : 'tl=en'
  • - const leftValue = isEnglish ? 'tl=en' : 'tl=zh-CN'
  • - if (window.location.hash.indexOf(rightValue)) {
  • - window.location.hash = window.location.hash.replace(rightValue, leftValue)
  • + const targetValue = isEnglish ? 'tl=zh-CN' : 'tl=en'
  • + const sourceValue = isEnglish ? 'tl=en' : 'tl=zh-CN'
  • + if (window.location.hash.indexOf(targetValue)) {
  • + window.location.hash = window.location.hash.replace(targetValue, sourceValue)
  • }
  • - window.location.hash = window.location.hash.replace(leftValue, rightValue)
  • + window.location.hash = window.location.hash.replace(sourceValue, targetValue)
  • }