translate

划词翻译

Verze ze dne 01. 07. 2016. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name translate
// @namespace https://lufei.so
// @grant GM_xmlhttpRequest
// @description 划词翻译
// @version 1.2
// ==/UserScript==

var panel = document.createElement('div');
panel.style.position = 'fixed';
panel.style.zIndex = 999;
panel.style.display = 'none';
panel.style.maxWidth = '400px';
panel.style.wordBreak = 'break-all';
panel.style.lineHeight = '1.2rem';
panel.style.border = '1px solid #eaeaea';
panel.style.backgroundColor = '#fff';
panel.style.borderRadius = '4px';
panel.style.padding = '.5rem 1rem';
panel.style.fontFamily = 'monospace, consolas';
panel.style.fontSize = '14px';
panel.style.color = '#555';
panel.style.textAlign = 'left';
document.body.appendChild(panel);
var sel, text, show = true;

document.addEventListener('mousedown', function(e) {
	if (e.buttons != 2) {
		if (sel) sel.removeAllRanges();
	}
});

document.addEventListener('mouseup', function(e) {
	sel = window.getSelection();
	text = sel.toString();
	if (e.target === panel || panel.contains(e.target)) return;
    panel.style.display = 'none';
    panel.innerHTML = '';
	if (text != '' && !/^\s+$/.test(text)) {
		GM_xmlhttpRequest({
            method: 'GET',
            url: 'https://fanyi.youdao.com/openapi.do?relatedUrl=http%3A%2F%2Ffanyi.youdao.com%2Fopenapi%3Fpath%3Dweb-mode&keyfrom=test&key=null&type=data&doctype=json&version=1.1&q=' + window.encodeURIComponent(text),
            onload: function(event) {
                var data = JSON.parse(event.responseText),
                    s = '';
                if (data.errorCode === 0) {
					if (data.basic) {
						s += '<div>' + data.translation[0] + '<span>[' + data.basic.phonetic + ']</span>' + '</div>';
                        data.basic.explains.forEach(function(e) {
                            s += '<div>' + e + '</div>';
                        });
                    } else {
						s += '<div>' + data.translation[0] + '</div>';
					}
                    panel.innerHTML = s;
					panel.style.top = e.clientY + 10 + 'px';
        			panel.style.left = e.clientX + 'px';
        			panel.style.display = 'block';
                }
            }
        });
	};
});