translate

划词翻译

Fra 01.07.2016. Se den seneste versjonen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

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