Google Translator Detect Language

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

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==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);
    }
})();