Greasy Fork is available in English.

Translate

划词翻译调用“必应翻译(必应词典)、谷歌翻译、有道词典(有道翻译)、必应在线翻译、百度翻译、Oxford Advanced Learner's Dictionary”网页翻译

Versão de: 02/01/2018. Veja: a última versão.

// ==UserScript==
// @name         Translate
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  划词翻译调用“必应翻译(必应词典)、谷歌翻译、有道词典(有道翻译)、必应在线翻译、百度翻译、Oxford Advanced Learner's Dictionary”网页翻译
// @author       barrer
// @match        http://*/*
// @include      https://*/*
// @include      file:///*
// @run-at document-end
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function () {
    'use strict';

    // Your code here...
    var baiduValue = GM_getValue('barrer-Translate-baidu'),
        bingValue = GM_getValue('barrer-Translate-bing'),
        bingTranslatorValue = GM_getValue('barrer-Translate-bingTranslator'),
        googleValue = GM_getValue('barrer-Translate-google'),
        oaldValue = GM_getValue('barrer-Translate-oald'),
        youdaoValue = GM_getValue('barrer-Translate-youdao');
    if (baiduValue && window.location.href.startsWith('https://fanyi.baidu.com')) {
        GM_setValue('barrer-Translate-baidu', '');
        document.querySelector('#baidu_translate_input').value = baiduValue;
        document.querySelector('#translate-button').click();
    }
    if (bingValue && window.location.href.startsWith('https://cn.bing.com/dict')) {
        GM_setValue('barrer-Translate-bing', '');
    }
    if (bingTranslatorValue && window.location.href.startsWith('https://www.bing.com/translator')) {
        GM_setValue('barrer-Translate-bingTranslator', '');
        document.querySelector('#srcText').value = bingTranslatorValue;
        document.querySelector('#TranslateButton').click();
    }
    if (googleValue && window.location.href.startsWith('https://translate.google.com')) {
        GM_setValue('barrer-Translate-google', '');
        document.querySelector('#source').value = googleValue;
        document.querySelector('#gt-submit').click();
    }
    if (oaldValue && window.location.href.startsWith('https://www.oxfordlearnersdictionaries.com')) {
        GM_setValue('barrer-Translate-oald', '');
        document.querySelector('.searchfield_input').value = oaldValue;
        document.querySelector('#search-btn input').click();
    }
    if (youdaoValue && window.location.href.startsWith('https://dict.youdao.com')) {
        GM_setValue('barrer-Translate-youdao', '');
    }
    if (window.location.href.startsWith('https://fanyi.baidu.com') ||
        window.location.href.startsWith('https://cn.bing.com/dict') ||
        window.location.href.startsWith('https://www.bing.com/translator') ||
        window.location.href.startsWith('https://translate.google.com') ||
        window.location.href.startsWith('https://www.oxfordlearnersdictionaries.com') ||
        window.location.href.startsWith('https://dict.youdao.com')
    )
        document.onkeydown = escExit;
    /**Esc键关闭窗口*/
    function escExit(e) {
        e = e || window.event;
        var isEscape = false;
        if ("key" in e)
            isEscape = (e.key == "Escape" || e.key == "Esc");
        else
            isEscape = (e.keyCode == 27);
        if (isEscape)
            window.close();
    }

    /**弹出居中窗口*/
    function popupCenter(url, title, w, h) {
        var x = screen.width / 2 - w / 2;
        var y = screen.height / 2 - h / 2;
        var win = window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + y + ', left=' + x);
        if (window.focus) {
            win.focus();
        }
        return win;
    }

    /**日志输出*/
    function log() {
        var debug = false;
        if (!debug)
            return;
        if (arguments) {
            for (var i = 0; i < arguments.length; i++) {
                console.log(arguments[i]);
            }
        }
    }

    log('url:' + window.location.href);
    // 翻译图标
    var icon = document.createElement('div'), style = '' +
        'font-family:Arial,sans-serif!important;' +
        'font-weight:normal!important;' +
        'background:#f60!important;' +
        'color:#fff!important;' +
        'border-radius:3px!important;' +
        'font-size:13px!important;' +
        'line-height:100%!important;' +
        'padding:2px 4px!important;' +
        'margin:0 4px!important;' +
        'display:inline-block!important;' +
        'text-decoration:none!important;' +
        '';
    icon.innerHTML = '' +
        '<a href="javascript:void(0)" style="' + style + '"  type="bing">必应</a>' +
        '<a href="javascript:void(0)" style="' + style + '"  type="google">谷歌</a>' +
        '<a href="javascript:void(0)" style="' + style + '" type="youdao">有道</a>' +
        '<a href="javascript:void(0)" style="' + style + '"  type="bingTranslator">必译</a>' +
        '<a href="javascript:void(0)" style="' + style + '"  type="baidu">百度</a>' +
        '<a href="javascript:void(0)" style="' + style + '"  type="oald">OALD</a>' +
        '';
    icon.setAttribute('style', '' +
        'display:none!important;' +
        'position:absolute!important;' +
        'font-size:13px!important;' +
        'text-align:left!important;' +
        'z-index:2147483647!important;' +
        '');
    // 添加翻译图标到 DOM
    document.documentElement.appendChild(icon);
    // 鼠标事件:防止选中的文本消失
    document.addEventListener('mousedown', function (e) {
        if (e.target == icon || (e.target.parentNode && e.target.parentNode == icon)) {// 点击了翻译图标
            e.preventDefault();
        }
    });
    // 选中变化事件:当点击已经选中的文本的时候,隐藏翻译图标和翻译面板(此时浏览器动作是:选中的文本已经取消选中了)
    document.addEventListener("selectionchange", function () {
        log('selectionchange:' + window.getSelection().toString());
        if (!window.getSelection().toString().trim()) {
            icon.style.display = 'none';
        }
    });
    // 鼠标事件:防止选中的文本消失;显示、隐藏翻译图标
    document.addEventListener('mouseup', function (e) {
        if (e.target == icon || (e.target.parentNode && e.target.parentNode == icon)) {// 点击了翻译图标
            e.preventDefault();
            return;
        }
        var text = window.getSelection().toString().trim();
        log('click text:' + text);
        if (text && icon.style.display == 'none') {
            log('show icon');
            log(text + '|' + e.pageX + '|' + e.pageY);
            icon.style.top = e.pageY + 10 + 'px';
            icon.style.left = e.pageX + 10 + 'px';
            icon.style.display = 'block';
        } else if (!text) {
            log('hide icon');
            icon.style.display = 'none';
        }
    });
    // 翻译图标点击事件
    icon.addEventListener('click', function (e) {
        var text = window.getSelection().toString().trim();
        if (text) {
            log('text:' + text);
            var engine = e.target.hasAttribute('type') ? e.target.getAttribute('type') : '';
            log('engine:' + engine);
            if (server[engine])
                server[engine](text);
        }
    });
    // 翻译server
    var server = {
        baidu: function (text) {
            GM_setValue('barrer-Translate-baidu', text);
            popupCenter('https://fanyi.baidu.com', null, 1024, 600);
        },
        bing: function (text) {
            GM_setValue('barrer-Translate-bing', text);
            popupCenter('https://cn.bing.com/dict/search?q=' + encodeURIComponent(text), null, 800, 600);
        },
        bingTranslator: function (text) {
            GM_setValue('barrer-Translate-bingTranslator', text);
            popupCenter('https://www.bing.com/translator', null, 800, 600);
        },
        google: function (text) {
            GM_setValue('barrer-Translate-google', text);
            popupCenter('https://translate.google.com', null, 800, 600);
        },
        oald: function (text) {
            GM_setValue('barrer-Translate-oald', text);
            popupCenter('https://www.oxfordlearnersdictionaries.com/definition/english', null, 800, 600);
        },
        youdao: function (text) {
            GM_setValue('barrer-Translate-youdao', text);
            popupCenter('https://dict.youdao.com/w/eng/' + encodeURIComponent(text), null, 800, 600);
        }
    };// 翻译server结束
})();