// ==UserScript==
// @name 智能划词翻译
// @namespace https://greasyfork.org/zh-CN/users/150560
// @version 1.5.1
// @description 划词翻译,自动切换谷歌翻译和有道词典
// @author 田雨菲 xinggsf
// @include *
// @connect dict.youdao.com
// @connect translate.google.cn
// @grant GM_xmlhttpRequest
// @downloadURL none
// ==/UserScript==
'use strict';
var youdaoUrl = 'http://dict.youdao.com/jsonapi?xmlVersion=5.1&jsonversion=2&q=';
var googleUrl = 'https://translate.google.cn/translate_a/single?client=gtx&dt=t&dt=bd&dj=1&source=input&hl=zh-CN&sl=auto&tl=';
var icon = document.createElement('span');
icon.innerHTML = ``;
icon.setAttribute('style', `
width:32px!important;
height:32px!important;
display:none!important;
background:#fff!important;
border-radius:50%!important;
box-shadow:4px 4px 8px #888!important;
position:absolute!important;
z-index:2147483647!important;
`);
var outTimer, overTimer;
// 添加翻译图标到 DOM
document.documentElement.appendChild(icon);
// 鼠标事件:防止选中的文本消失
icon.addEventListener('mousedown', e => e.preventDefault(), true);
// 鼠标事件:防止选中的文本消失
icon.addEventListener('mouseup', e => e.preventDefault(), true);
document.addEventListener('mouseup', function (e) {
if (translateTip.rendered.includes(e.target)) // 点击了翻译内容面板
return; // 不再创建翻译图标
//console.log('mouseup!');
var text = window.getSelection().toString().trim();
if (text && icon.style.display != '') {
icon.style.top = e.pageY + 12 + 'px';
icon.style.left = e.pageX + 'px';
icon.style.display = '';
outTimer = setTimeout(() => {
icon.style.display = 'none';
outTimer = null;
}, 1900);
} else if (!text) {
icon.style.display = 'none';
translateTip.containerDestroy(); // 销毁翻译内容面板
}
});
// 翻译图标点击事件
var clickIcon = function (e) {
var text = window.getSelection().toString().trim();
if (text) {
icon.style.display = 'none';
translateTip.containerDestroy(); // 销毁翻译内容面板
// 新建翻译内容面板
var container = translateTip.container();
container.style.top = e.pageY + 'px';
if (e.pageX + 350 <= document.body.clientWidth) // container 面板css最大宽度为250px
container.style.left = e.pageX + 'px';
else
container.style.left = document.body.clientWidth - 350 + 'px';
document.body.appendChild(container);
translateTip.rendered.push(container);
if (isChina(text)) {
ajax(googleUrl + 'en&q=', text, 1, container);
} else {
if (countOfWord(text) == 1) {
ajax(youdaoUrl, text, 0, container);
} else {
ajax(googleUrl + 'zh-CN&q=', text, 1, container);
}
}
}
};
icon.addEventListener('click', clickIcon, true);
icon.addEventListener('mouseover', function (e) {
if (outTimer) {
clearTimeout(outTimer);
outTimer = null;
}
overTimer = overTimer || setTimeout(clickIcon, 360, e);
}, true);
icon.addEventListener('mouseout', function (e) {
if (overTimer) {
clearTimeout(overTimer);
overTimer = null;
}
outTimer = outTimer || setTimeout(() => {
icon.style.display = 'none';
outTimer = null;
}, 360);
}, true);
function countOfWord(str) {
var value = String(str).trim()
.replace(/\s{2,}/g, " ");
if (!value) return 0;
var m = value.match(/\s/g);
return m ? m.length + 1 : 1;
}
function isChina(str) {
return /^[\u4E00-\u9FA5\uFF00-\uFF20\u3000-\u301C]/.test(str);
}
// ajax 跨域访问公共方法
function ajax(url, text, target, element, method, data, headers) {
method = method || 'GET';
// >>>因为Tampermonkey跨域访问(a.com)时会自动携带对应域名(a.com)的对应cookie
// 不会携带当前域名的cookie
// 所以,GM_xmlhttpRequest【不存在】cookie跨域访问安全性问题
// 以下设置默认headers不起作用<<<
url += encodeURIComponent(text);
headers = headers || {'cookie': ''};
GM_xmlhttpRequest({
method: method,
url: url,
headers: headers,
data: data,
onload: function (res) {
if (target == 0) {
youdao(res.responseText, text, element);
} else {
google(res.responseText, element);
}
},
onerror: function (res) {
displaycontainer("连接失败\nURL: "+url, element);
}
});
}
// 有道翻译 引擎
function youdao(rst, text, element) {
var ec = JSON.parse(rst).ec;
if (!!ec) {
var word = JSON.parse(rst).ec.word[0],
html = '',
tr = '';
const { trs, ukphone, usphone, phone } = word;
var phoneStyle = 'color:#9E9E9E!important;';
if (!!ukphone && ukphone.length > 0) {
html += '英[' + ukphone + '] ';
}
if (!!usphone && usphone.length > 0) {
html += '美[' + usphone + '] ';
}
if (html.length > 0) {
html += '
';
} else if (!!phone && phone.length > 0) {
html += '[' + phone + ']
';
}
trs.forEach(element => {
tr += element.tr[0].l.i[0] + '
';
});
html += tr;
displaycontainer(html, element);
} else {
ajax(googleUrl + 'zh-CN&q=', text, 1, element);
}
}
// 谷歌翻译 引擎
function google(rst, element) {
let k, html = '',
json = JSON.parse(rst);
for (k of json.sentences) html += k.trans;
displaycontainer(html, element);
}
function displaycontainer(text, element) {
element.innerHTML = text;
element.style.display = ''; // 显示结果
}
// 翻译结果面板
var translateTip = {
// 存放已经生成的翻译内容面板(销毁的时候用)
rendered: [],
// 销毁已经生成的翻译内容面板
containerDestroy: function () {
for (let k of this.rendered) k.remove();
},
// 生成翻译结果面板 DOM (此时还未添加到页面)
container: function () {
var div = document.createElement('div');
div.setAttribute('style',
`display:none!important;
position:absolute!important;
font-size:13px!important;
overflow:auto!important;
background:#fff!important;
font-family:sans-serif,Arial!important;
font-weight:normal!important;
text-align:left!important;
color:#000!important;
padding:0.5em 1em!important;
line-height:1.5em!important;
border-radius:5px!important;
border:1px solid #ccc!important;
box-shadow:4px 4px 8px #888!important;
max-width:350px!important;
max-height:216px!important;
z-index:2147483647!important;`
);
return div;
}
};