Greasy Fork

Greasy Fork is available in English.

google-translate

add a Google Translate plug-in to the page

当前为 2024-05-08 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            google-translate
// @name:zh         谷歌翻译
// @namespace       https://github.com/pansong291/
// @version         0.3.1
// @description     add a Google Translate plug-in to the page
// @description:zh  向页面添加谷歌翻译插件
// @author          paso
// @license         Apache-2.0
// @match           *://*/*
// @icon            https://ssl.gstatic.com/translate/favicon.ico
// @grant           none
// @run-at          context-menu
// ==/UserScript==

;(function () {
  'use strict'
  if (window.google?.translate?.TranslateElement) {
    console.info('%cGoogle Translate\n%chas already been inited.', 'color: #fbbc05; font-size: 22px; font-weight: bold', 'font-size: 14px')
    return
  }
  const elementId = `google-translate-element-${Math.floor(Math.random() * 100_000_000)}`
  document.head.insertAdjacentHTML('beforeend', `
    <style>
        #${elementId} {
            position: fixed;
            left: 0;
            bottom: 0;
            z-index: 99999;
            transform: translateX(calc(6px - 100%));
            transition: transform .3s;
        }
        #${elementId}.hold, #${elementId}:hover {
            transform: translateX(0);
        }
        #${elementId} > :first-child {
            background: white;
            padding: 4px;
            border-radius: 0 4px 0 0;
            box-shadow: 0 0 4px rgba(0,0,0,50%);
        }
        #${elementId} span, #${elementId} a, #${elementId} img {
            display: inline;
        }
    </style>`)

  const divElm = createElement('div', { id: elementId, class: 'hold' })
  divElm.addEventListener('mouseleave', () => divElm.classList.remove('hold'), { once: true })
  document.body.append(divElm)

  window.googleTranslateElementInit = () => {
    new window.google.translate.TranslateElement(null, elementId)
    console.log('%cGoogle Translate\n%cinit finished.', 'color: #4286F3; font-size: 22px; font-weight: bold', 'font-size: 14px')
    clearInterval(window.googleTranslateElementInitLoopId)
    delete window.googleTranslateElementInit
  }

  const jsElm = createElement('script', { src: '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit' })
  jsElm.onload = detectGoogleTranslate
  jsElm.onerror = () => {
    divElm.append(createElement('a', {
      style: 'font-size: 16px;',
      href: 'http://greasyfork.icu/scripts/493434',
      target: '_blank'
    }, ['Google Translate load failed!']))
  }
  document.head.append(jsElm)

  function detectGoogleTranslate() {
    let count = 360
    window.googleTranslateElementInitLoopId = setInterval(() => {
      if (window.google?.translate?.TranslateElement) {
        window.googleTranslateElementInit()
      } else if (count <= 0) {
        console.warn('%cGoogle Translate\n%cinit failed!', 'color: #EB4537; font-size: 22px; font-weight: bold', 'font-size: 14px')
        clearInterval(window.googleTranslateElementInitLoopId)
      }
      count--
    }, 500)
  }

  function createElement(tag, attrs, children) {
    const el = document.createElement(tag)
    if (attrs) {
      Object.entries(attrs).forEach(([k, v]) => el.setAttribute(k, v))
    }
    if (typeof children === 'string') {
      el.innerHTML = children
    } else if (Array.isArray(children)) {
      el.append.apply(el, children)
    }
    return el
  }
})()