Greasy Fork is available in English.

网页翻译

给每个非中文的网页左下角添加一个google翻译图标,直接调用 Google 的翻译接口对非中文网页进行翻译

// ==UserScript==
// @name         网页翻译
// @author       Kaiter-Plus
// @namespace    https://gitee.com/Kaiter-Plus/TampermonkeyScript/tree/master/Translate
// @description  给每个非中文的网页左下角添加一个google翻译图标,直接调用 Google 的翻译接口对非中文网页进行翻译
// @include      *://*
// @exclude      /^[http|https].*[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/
// @exclude      /duyaoss\.com/
// @exclude      /lanzous\.com/
// @exclude      /w3school.*cn/
// @exclude      /iqiyi\.com/
// @exclude      /.*baidu.*/
// @exclude      /cnblogs\.com/
// @exclude      /csdn\.net/
// @exclude      /zhku\.edu\.cn/
// @exclude      /zhihuishu\.com/
// @exclude      /aliyuncs\.com/
// @exclude      /.*chaoxing\.com/
// @exclude      /youku\.com/
// @exclude      /examcoo\.com/
// @exclude      /.*mooc\.com/
// @exclude      /.*bilibili\.com/
// @exclude      /.*qq\.com/
// @exclude      /localhost/
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAXyElEQVR4Xu1de5gcVZU/p3rCS9F09cgKu1FkunoC8qErcWERVviWuCDIF/ATH/hCIUlXTeISH/hal6wPEFE0SVdnUBRfkF1ARR4uKCIurMKH4KqJSVdHEBCFTN+eEAl5TNfZryYBJ5hJuu6p6r7ddebf3N/vnvM79Us9+t5zEeRPFBAFplUARRtRQBSYXgExiFwdosAeFBCDyOUhCohB5BoQBfQUkDuInm6CyogCYpCMFFrS1FNADKKnm6AyooAYJCOFljT1FBCD6OkmqIwoIAbJSKElTT0FxCB6ugkqIwqIQTJSaElTTwExiJ5ugsqIAmKQjBRa0tRTQAyip5ugMqKAGCQjhZY09RQQg+jpJqiMKCAGyUihJU09BcQgeroJKiMKiEEyUmhJU08BMYieboLKiAJikIwUWtLUU0AMoqeboDKigBgkI4WWNPUUEIPo6SaojCggBslIoSVNPQXEIHq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GNKIYGYqIAYxMSqSEzGKCAGMaYUEoiJCohBTKyKxGSMAmIQY0ohgZiogBjExKpITMYo8P+dDE9BKT2zYgAAAABJRU5ErkJggg==
// @version      1.30
// @noframes
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @note         2020/03/26 网页整页翻译功能
// @note         2020/04/13 排除纯ip网址
// @note         2020/04/14 移除翻译后顶边栏
// @note         2020/05/01 排除百度、QQ、超星等中文网址
// @note         2020/05/04 修复去除上边栏网页先向下再向上跳的Bug
// @note         2020/05/05 尝试修复百度出现超粗顶栏的Bug
// @note         2020/05/12 添加恢复原网页的按钮(翻译按钮旁边),有点丑,不过希望可以先用着,有时间再看看能不能弄好看一点ヾ(≧▽≦*)o
// @note         2020/05/23 稍微修改了一下恢复原网页的按钮的样式(还是不好看)
// @note         2020/05/26 修改脚本为原生javascript,兼容暴力猴
// @note         2020/05/26 修改翻译栏样式,固定宽高,防止在一些页面上出现太宽或太高的现象
// @note         2020/06/06 修复火狐浏览器(firefox),内存溢出的bug,精简了一点代码
// @note         2020/06/08 排除一些代码块的翻译,如果还有其它的网站的代码块需要排除,可以反馈给我,我排除一下
// @note         2020/06/17 修改恢复原网页按钮的样式(使用@picasso250的样式),排除标签 tt
// @note         2020/06/18 适配Quora
// @note         2020/06/26 翻译和恢复按钮修改为在页面边缘附着的半透明半圆 -> 鼠标移入弹出翻译或恢复按钮
// @note         2020/07/02 按钮向上移动了30像素,经测试,点击弹出按钮的方式不太友好,故放弃
// @note         2020/08/23 使用了模板字符串代替原来的普通字符串,适配了移动端,移动端UI待改善
// @note         2020/08/24 把“恢复”按钮的文字修改为“原”,稍微修改了一下移动端的布局
// @note         2020/09/02 添加了一个网址的翻译排除
// @note         2020/09/13 最近没有时间更新其它的,先做个小更新:添加了通过 meta 信息 charset 来判断是否添加翻译按钮(感谢 @qinxs)
// @note         2020/10/03 放假了,更新了切换按钮的配置选项,点击浏览器的油猴或者暴力猴插件图标即可看见脚本的配置选项,点击即可切换按钮的位置
// @note         2020/10/03 刚刚更新按钮位置配置信息时,忘记调整移动端的布局,重新调整更新一下
// ==/UserScript==

(function() {
  'use strict'

  // 获取 head
  const head = document.head
  // 获取body
  const body = document.body
  // 获取当前页面的语言
  const lang = document.documentElement.lang
  // 获取网页使用的主要语言
  const mainLang = document.characterSet.toLowerCase()
  // 获取按钮位置信息
  const currentPosition = GM_getValue('position')
  // 按钮位置:true 为左,false 为右, 默认为左
  let buttonPosition = currentPosition ? true : false

  // 判断是不是中文,如果是则直接return,否则执行
  if (lang.substr(0, 2) === 'zh' || mainLang === 'gbk') {
    return
  } else {

    // 创建网页元素方法
    function createElement(html, nodeText, attr, parent) {
      const element = document.createElement(nodeText)
      if (attr) {
        element[attr] = html
      } else {
        element.innerHTML = html
      }
      parent.appendChild(element)
    }

    // 自定义样式,隐藏顶部栏
    GM_addStyle(`
      html,body{
        top: 0!important
      }
      #google_translate_element {
        position: fixed;
        bottom: 30px;
        height: 22px;
        border-radius: 11px;
        z-index: 10000000;
        overflow: hidden;
        box-shadow: 1px 1px 3px 0 #888;
        opacity: .5;
        transition: all .3s;
      }
      .recoverPage {
        width: 4em;
        background-color: #fff;
        position: fixed;
        z-index: 10000000;
        bottom: 60px;
        user-select: none;
        text-align: center;
        font-size: small;
        line-height: 2em;
        border-radius: 1em;
        box-shadow: 1px 1px 3px 0 #888;
        opacity: .5;
        transition: all .3s;
      }
      #google_translate_element:hover, .recoverPage:hover {
        opacity: 1;
        transform: translateX(0);
      }
      .recoverPage:active {
        box-shadow: 1px 1px 3px 0 #888 inset;
      }
      #google_translate_element .goog-te-gadget-simple {
        width: 100%;
      }
      .goog-te-banner-frame.skiptranslate {
        display: none
      }
      @media handheld, only screen and (max-width: 768px) {
        #google_translate_element {
          width: 104px;
        }
        #google_translate_element .goog-te-combo {
          margin: 0;
          padding-top: 2px;
          border: none;
        }
        .recoverPage {
          width: 2em;
          line-height: 22px;
        }
      }
    `)

    setButtonPosition()

    // 创建容器
    createElement('google_translate_element', 'div', 'id', body)


    // 初始化
    createElement(`
      function googleTranslateElementInit() {
        let google_translate_element = document.getElementById('google_translate_element');
        let timer = setInterval(function() {
          google_translate_element = document.getElementById('google_translate_element');
          if (google_translate_element) {
             clearInterval(timer);
             new google.translate.TranslateElement({
               pageLanguage: 'auto',
               //包括的语言,中文简体,中文繁体,英语,日语,俄语
               includedLanguages: 'zh-CN,zh-TW,en,ja,ru',
               /*0,原生select,并且谷歌logo显示在按钮下方。
                 1,原生select,并且谷歌logo显示在右侧。
                 2,完全展开语言列表,适合pc。
               */
               layout: /mobile/i.test(navigator.userAgent) ? 0 : 2,
             }, 'google_translate_element');
             // 清除图片的请求,加快访问速度
            img = [].slice.call(document.querySelectorAll('#goog-gt-tt img,#google_translate_element img'));
            img.forEach(function(v, i) {
              v.src = '';
            });
            const recoverPage = document.createElement('div')
            recoverPage.setAttribute('class', 'notranslate recoverPage')
            recoverPage.innerText = '原'
            document.body.appendChild(recoverPage)
            // 点击恢复原网页
            recoverPage.onclick = (() => {
              const phoneRecoverIframe = document.getElementById(':1.container') // 移动端
              const PCRecoverIframe = document.getElementById(':2.container') // PC端
              if (phoneRecoverIframe) {
                const recoverDocument = phoneRecoverIframe.contentWindow.document
                recoverDocument.getElementById(':1.restore').click()
              } else if (PCRecoverIframe) {
                const recoverDocument = PCRecoverIframe.contentWindow.document
                recoverDocument.getElementById(':2.restore').click()
              }
            })
          }
        }, 300);
      }`,  'script', '', head)

    // 导入翻译接口
    if (/quora/i.test(location.href)) {
      // 这里主要是适配quora
      createElement('//translate.google.com/translate_a/element.js?&cb=googleTranslateElementInit', 'script', 'src',
        head)
    } else {
      createElement('//translate.google.cn/translate_a/element.js?&cb=googleTranslateElementInit', 'script', 'src',
        head)
      // createElement('//cdn.jsdelivr.net/gh/lindongbin/gt/element.js','script','src', head)
    }

    // 排除一些代码的翻译
    const noTranslateArray = [
      '.bbCodeCode',
      'tt',
      'pre[translate="no"]'
    ]
    noTranslateArray.forEach(selectorName => {
      [...document.querySelectorAll(selectorName)].forEach(node => {
        if (node.className.indexOf('notranslate') === -1) {
          node.classList.add('notranslate')
        }
      })
    })
  }

  // 设置按钮位置
  function setButtonPosition() {
    if (buttonPosition) {
      GM_addStyle(`
        #google_translate_element {
          left: 0px;
          transform: translateX(-85%);
        }
        .recoverPage {
          left: 0px;
          transform: translateX(-73%);
        }
        @media handheld, only screen and (max-width: 768px) {
          .recoverPage {
            transform: translateX(0);
          }
        }
      `)
    } else {
      GM_addStyle(`
        #google_translate_element {
          right: 0px;
          transform: translateX(85%);
        }
        .recoverPage {
          right: 0px;
          transform: translateX(73%);
        }
        @media handheld, only screen and (max-width: 768px) {
          .recoverPage {
            transform: translateX(0);
          }
        }
      `)
    }
  }

  function switchButtonPosition() {
    location.reload()
    GM_setValue('position', !buttonPosition)
    setButtonPosition()
  }

  // 注册菜单快捷键
  GM_registerMenuCommand("点击切换按钮位置", switchButtonPosition);
}());