google-translate

add a Google Translate plug-in to the page

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

  1. // ==UserScript==
  2. // @name google-translate
  3. // @name:zh 谷歌翻译
  4. // @namespace https://github.com/pansong291/
  5. // @version 0.3.1
  6. // @description add a Google Translate plug-in to the page
  7. // @description:zh 向页面添加谷歌翻译插件
  8. // @author paso
  9. // @license Apache-2.0
  10. // @match *://*/*
  11. // @icon https://ssl.gstatic.com/translate/favicon.ico
  12. // @grant none
  13. // @run-at context-menu
  14. // ==/UserScript==
  15.  
  16. ;(function () {
  17. 'use strict'
  18. if (window.google?.translate?.TranslateElement) {
  19. console.info('%cGoogle Translate\n%chas already been inited.', 'color: #fbbc05; font-size: 22px; font-weight: bold', 'font-size: 14px')
  20. return
  21. }
  22. const elementId = `google-translate-element-${Math.floor(Math.random() * 100_000_000)}`
  23. document.head.insertAdjacentHTML('beforeend', `
  24. <style>
  25. #${elementId} {
  26. position: fixed;
  27. left: 0;
  28. bottom: 0;
  29. z-index: 99999;
  30. transform: translateX(calc(6px - 100%));
  31. transition: transform .3s;
  32. }
  33. #${elementId}.hold, #${elementId}:hover {
  34. transform: translateX(0);
  35. }
  36. #${elementId} > :first-child {
  37. background: white;
  38. padding: 4px;
  39. border-radius: 0 4px 0 0;
  40. box-shadow: 0 0 4px rgba(0,0,0,50%);
  41. }
  42. #${elementId} span, #${elementId} a, #${elementId} img {
  43. display: inline;
  44. }
  45. </style>`)
  46.  
  47. const divElm = createElement('div', { id: elementId, class: 'hold' })
  48. divElm.addEventListener('mouseleave', () => divElm.classList.remove('hold'), { once: true })
  49. document.body.append(divElm)
  50.  
  51. window.googleTranslateElementInit = () => {
  52. new window.google.translate.TranslateElement(null, elementId)
  53. console.log('%cGoogle Translate\n%cinit finished.', 'color: #4286F3; font-size: 22px; font-weight: bold', 'font-size: 14px')
  54. clearInterval(window.googleTranslateElementInitLoopId)
  55. delete window.googleTranslateElementInit
  56. }
  57.  
  58. const jsElm = createElement('script', { src: '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit' })
  59. jsElm.onload = detectGoogleTranslate
  60. jsElm.onerror = () => {
  61. divElm.append(createElement('a', {
  62. style: 'font-size: 16px;',
  63. href: 'https://gf.qytechs.cn/scripts/493434',
  64. target: '_blank'
  65. }, ['Google Translate load failed!']))
  66. }
  67. document.head.append(jsElm)
  68.  
  69. function detectGoogleTranslate() {
  70. let count = 360
  71. window.googleTranslateElementInitLoopId = setInterval(() => {
  72. if (window.google?.translate?.TranslateElement) {
  73. window.googleTranslateElementInit()
  74. } else if (count <= 0) {
  75. console.warn('%cGoogle Translate\n%cinit failed!', 'color: #EB4537; font-size: 22px; font-weight: bold', 'font-size: 14px')
  76. clearInterval(window.googleTranslateElementInitLoopId)
  77. }
  78. count--
  79. }, 500)
  80. }
  81.  
  82. function createElement(tag, attrs, children) {
  83. const el = document.createElement(tag)
  84. if (attrs) {
  85. Object.entries(attrs).forEach(([k, v]) => el.setAttribute(k, v))
  86. }
  87. if (typeof children === 'string') {
  88. el.innerHTML = children
  89. } else if (Array.isArray(children)) {
  90. el.append.apply(el, children)
  91. }
  92. return el
  93. }
  94. })()

QingJ © 2025

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