Google翻译自动中英互译

自动切换输出语言

目前為 2019-08-04 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Google翻译自动中英互译
  3. // @description 自动切换输出语言
  4. // @namespace https://gf.qytechs.cn/users/197529
  5. // @author kkocdko
  6. // @license Unlicense
  7. // @version 0.5
  8. // @include *://translate.google.cn/*
  9. // ==/UserScript==
  10. 'use strict'
  11.  
  12. const sourceTextEl = document.querySelector('#source')
  13. const sourceLangEl = document.querySelector('.sl-selector a')
  14. if (window.location.hash === '') {
  15. window.location.hash = '#'
  16. }
  17. if (/source=osdd/.test(window.location.search)) {
  18. window.location.hash = '#text=' + sourceTextEl.value
  19. }
  20. replaceUrlHash(/sl=en|sl=zh-CN/, 'sl=auto')
  21. autoSwitchTargetLang()
  22. sourceLangEl.addEventListener('DOMSubtreeModified', autoSwitchTargetLang)
  23.  
  24. function autoSwitchTargetLang () {
  25. if (sourceTextEl.value !== '') {
  26. replaceUrlHash(/tl=en|tl=zh-CN/, sourceLangEl.textContent === '检测到英语' ? 'tl=zh-CN' : 'tl=en')
  27. }
  28. }
  29.  
  30. function replaceUrlHash (regex, replaceValue) {
  31. if (regex.test(window.location.hash)) {
  32. window.location.hash = window.location.hash.replace(regex, replaceValue)
  33. } else {
  34. window.location.hash += '&' + replaceValue
  35. }
  36. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址