Google整页翻译

类似谷歌浏览器自带翻译

As of 2019-08-28. See the latest version.

// ==UserScript==
// @name         Google整页翻译
// @namespace    https://greasyfork.org/zh-CN/users/
// @version      0.4
// @description  类似谷歌浏览器自带翻译
// @author       zs6
// @license      GPL-3.0-only
// @match        http://*/*
// @include      https://*/*
// @include      file://*/*
// @run-at document-end
// @noframes
// ==/UserScript==

(function () {
    'use strict';
    function addNewElement(innerhtml,node,src) {
        var element = document.createElement(node);
        if(src){
            element.src = innerhtml;
        }else{
            element.innerHTML = innerhtml;
        }
        document.getElementsByTagName('head')[0].appendChild(element);
    }
    if (!/zh/.test(document.documentElement.lang)&&!/[\u4E00-\u9FA5\uF900-\uFA2D]/.test(document.title)) {

        var google_translate_element = document.createElement('div');
        google_translate_element.id = 'google_translate_element';
        google_translate_element.style = 'position:fixed; bottom:5px; left:5px; cursor:pointer;Z-INDEX: 99999;opacity:0.8;';
        document.documentElement.appendChild(google_translate_element);

        var google_translate_close = document.createElement('div');
        google_translate_close.id = 'google_translate_close';
        google_translate_close.style = 'font-size: 16px;position:fixed; bottom:7px; left:116px; cursor:pointer;Z-INDEX: 99999;opacity:0.8;background-color: #fff;';
        google_translate_close.innerHTML = "还原"
        document.documentElement.appendChild(google_translate_close);

        var gtehtml="function googleTranslateElementInit() {" +
            "new google.translate.TranslateElement({" +
            "layout: google.translate.TranslateElement.InlineLayout.SIMPLE," +
            "multilanguagePage: true," +
            "pageLanguage: 'auto'," +
            "includedLanguages: 'zh-CN,zh-TW,en'" +
            "}, 'google_translate_element');}";
        addNewElement(gtehtml,'script',false);
        addNewElement('https://cdn.jsdelivr.net/gh/zs6/gugefanyijs@1.9/element.js','script',true);
        addNewElement('.goog-te-banner-frame.skiptranslate {visibility:hidden !important;} body {top: 0px !important; }','style',false);
        google_translate_close.onclick=function(){
             document.getElementById(':2.container').contentWindow.document.getElementsByClassName("goog-close-link")[0].click();
        };
    }
}());