Google Translator Detect Language

谷歌翻译自动跳转,根据查询的语言决定是英译中还是中译英。

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         Google Translator Detect Language
// @namespace    https://www.jeddd.com
// @version      0.3
// @description  谷歌翻译自动跳转,根据查询的语言决定是英译中还是中译英。
// @author       Jed-Z
// @match        https://translate.google.com/*
// @match        https://translate.google.cn/*
// @grant        none
// ==/UserScript==

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    query = decodeURI(query);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable) {
            return pair[1];
        }
    }
    return(false);
}

(function() {
    'use strict';
    var cn_threshold = 0.2;

    var text = getQueryVariable("text");
    var sl = getQueryVariable("sl");
    var tl = getQueryVariable("tl");
    if (!text || !sl || !tl) return;

    var total_count = text.length;
    var cn_count = text.match(/[\u4E00-\u9FA5]/g).length;

    if (cn_count / total_count > cn_threshold) {
        var new_sl = "zh-CN";
        var new_tl = "en";
    }
    if (new_sl != sl || new_tl != tl) {
        var url = "https://" + window.location.host + "/?sl=" + new_sl + "&tl=" + new_tl + "&text=" + text + "&op=translate"
        window.location.replace(url);
    }
})();