网页翻译

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

目前為 2020-03-26 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         网页翻译
// @author       Kaiter
// @namespace    https://gitee.com/Kaiter-Plus
// @description  给每个非中文的网页左下角添加一个google翻译图标,直接调用 Google 的翻译接口对非中文网页进行翻译
// @include      *://*
// @run-at       document-end
// @version      1.0
// ==/UserScript==

(function () {
    // 获取当前页面的语言
    let lang = document.documentElement.lang;

    // 判断是不是中文,如果不是则执行
    if (lang.substr(0, 2) != "zh" && lang != "") {
        // 导入谷歌翻译接口
        let googletrans = document.createElement("script");
        googletrans.src = "//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit";
        document.head.appendChild(googletrans);

        // 初始化
        let script = document.createElement("script");
        let text = "";
        text += [
            "function googleTranslateElementInit() {",
            "    var google_translate_element = document.getElementById('google_translate_element');",
            "    var timer = setInterval(function() {",
            "        google_translate_element = document.getElementById('google_translate_element');",
            "        if (google_translate_element) {",
            "            clearInterval(timer);",
            "            //var langName = navigator.language ? navigator.language.split('-')[0].toLowerCase() : 'en';",
            "            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 = '';",
            "            });",
            "        }",
            "    }, 300);",
            "}"
        ].join("\n");;
        script.appendChild(document.createTextNode(text));
        document.head.appendChild(script);

        // 在页面的左下角显示一个翻译图标,防止和其它插件位置冲突
        var google_translate_element = document.createElement('div');
        google_translate_element.id = 'google_translate_element';
        google_translate_element.style = 'position: fixed;left: 10px;bottom: 10px;border-radius: 5px;overflow: hidden;box-shadow: 1px 2px 6px 2px #888;';
        document.body.appendChild(google_translate_element);


    }
})();