// ==UserScript==
// @name 智能划词翻译
// @namespace translate.xinggsf
// @version 1.6.6
// @description 划词翻译,自动切换谷歌翻译和有道词典
// @author xinggsf 田雨菲
// @include http*
// @include file://*
// @exclude https://www.nnyy6*.html
// @exclude https://www.dandanzan1*.html
// @run-at document-body
// @connect fanyi.youdao.com
// @connect translate.google.cn
// @grant GM_xmlhttpRequest
// @downloadURL none
// ==/UserScript==
'use strict';
// const youdaoUrl = 'http://dict.youdao.com/jsonapi?jsonversion=2&client=mobile&dicts=%7B%22count%22%3A99%2C%22dicts%22%3A%5B%5B%22ec%22%2C%22ce%22%2C%22newcj%22%2C%22newjc%22%2C%22kc%22%2C%22ck%22%2C%22fc%22%2C%22cf%22%2C%22multle%22%2C%22jtj%22%2C%22pic_dict%22%2C%22tc%22%2C%22ct%22%2C%22typos%22%2C%22special%22%2C%22tcb%22%2C%22baike%22%2C%22lang%22%2C%22simple%22%2C%22wordform%22%2C%22exam_dict%22%2C%22ctc%22%2C%22web_search%22%2C%22auth_sents_part%22%2C%22ec21%22%2C%22phrs%22%2C%22input%22%2C%22wikipedia_digest%22%2C%22ee%22%2C%22collins%22%2C%22ugc%22%2C%22media_sents_part%22%2C%22syno%22%2C%22rel_word%22%2C%22longman%22%2C%22ce_new%22%2C%22le%22%2C%22newcj_sents%22%2C%22blng_sents_part%22%2C%22hh%22%5D%2C%5B%22ugc%22%5D%2C%5B%22longman%22%5D%2C%5B%22newjc%22%5D%2C%5B%22newcj%22%5D%2C%5B%22web_trans%22%5D%2C%5B%22fanyi%22%5D%5D%7D&keyfrom=mdict.7.2.0.android&model=honor&mid=5.6.1&imei=659135764921685&vendor=wandoujia&screen=1080x1800&ssid=superman&network=wifi&abtest=2&xmlVersion=5.1&q='
const youdaoUrl = 'http://fanyi.youdao.com/translate?&doctype=json&type=AUTO&i=';
const googleUrl = 'https://translate.google.cn/translate_a/single?client=gtx&dt=t&dt=bd&dj=1&source=input&hl=zh-CN&sl=auto&tl=';
// const googleUrl = 'http://translate.google.com/translate_a/single?client=gtx&dt=t&dj=1&ie=UTF-8&sl=auto&tl=';
const reHZ = /^[\u4E00-\u9FA5\uFF00-\uFF20\u3000-\u301C]/;
const countOfWord = s => s ? s.split(/\s+/).length : 0;
const isChina = s => reHZ.test(s);
const xfetch = (url, type = 'json') => new Promise((success, fail) => {
GM_xmlhttpRequest({
method: 'GET',
url: url,
responseType: type,
onload: success,
onerror: fail,
ontimeout: fail
});
});
// 翻译结果面板
class TranslateTip {
constructor() {
const div = document.createElement('div');
div.hidden = true;
div.setAttribute('style',
`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;`
);
document.body.appendChild(div);
//点击了翻译内容面板,不再创建翻译图标
div.addEventListener('mouseup', e => e.stopPropagation());
this._tip = div;
}
showText(text) { //显示翻译文本
if (!this._tip.innerHTML) this._tip.innerHTML = text;
else this._tip.innerHTML += '
${content}
`; }, '有道翻译结果: