From c3cba21bc3d89e0b61fa24be23b05eb5a91e7ab3 Mon Sep 17 00:00:00 2001 From: example Date: Sun, 17 Feb 2019 09:52:33 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BF=BB=E8=AF=91=E5=BC=95=E6=93=8E=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E5=BA=8F=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- translate/translate-dictionary.js | 88 +++++++++++++++++-------------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/translate/translate-dictionary.js b/translate/translate-dictionary.js index b1f8a21..303cb91 100644 --- a/translate/translate-dictionary.js +++ b/translate/translate-dictionary.js @@ -1,7 +1,7 @@ // ==UserScript== // @name 划词翻译:多词典查询 // @namespace http://tampermonkey.net/ -// @version 2.4 +// @version 2.5 // @description 划词翻译调用“有道词典(有道翻译)、金山词霸、Bing 词典(必应词典)、剑桥高阶、沪江小D、谷歌翻译” // @author https://github.com/barrer // @match http://*/* @@ -450,13 +450,13 @@ var iframeWin = null; var iframeDoc = null; iframe.style.display = 'none'; - // 翻译图标、内容面板、翻译内容列表、当前选中文本、当前翻译引擎、当前翻译面板内容列表数组 - var icon = document.createElement('tr-icon'), - content = document.createElement('tr-content'), - contentList = document.createElement('div'), - selected, - engineId, - idsType; + var icon = document.createElement('tr-icon'), //翻译图标 + content = document.createElement('tr-content'), // 内容面板 + contentList = document.createElement('div'), //翻译内容结果集(HTML内容)列表 + selected, // 当前选中文本 + engineId, // 当前翻译引擎 + engineTriggerTime, // 引擎触发时间(milliseconds) + idsType; // 当前翻译面板内容列表数组 // 初始化内容面板 var largeHeight = document.createElement('div'); // 防止滚动条闪来闪去 largeHeight.style.height = '10000px'; @@ -525,66 +525,66 @@ })(), engines: (function () { var obj = {}; - obj[ids.ICIBA] = function (text) { + obj[ids.ICIBA] = function (text, time) { ajax('http://open.iciba.com/huaci_v3/dict.php?word=' + encodeURIComponent(text), function (rst) { - engineResult[ids.ICIBA] = parseIciba(rst); + putEngineResult(ids.ICIBA, parseIciba(rst), time); showContent(); }, function (rst) { - engineResult[ids.ICIBA] = htmlToDom('error: 无法连接翻译服务'); + putEngineResult(ids.ICIBA, htmlToDom('error: 无法连接翻译服务'), time); showContent(); }); }; - obj[ids.ICIBA_LOWER_CASE] = function (text) { + obj[ids.ICIBA_LOWER_CASE] = function (text, time) { ajax('http://open.iciba.com/huaci_v3/dict.php?word=' + encodeURIComponent(text.toLowerCase()), function (rst) { - engineResult[ids.ICIBA_LOWER_CASE] = parseIciba(rst); + putEngineResult(ids.ICIBA_LOWER_CASE, parseIciba(rst), time); showContent(); }, function (rst) { - engineResult[ids.ICIBA_LOWER_CASE] = htmlToDom('error: 无法连接翻译服务'); + putEngineResult(ids.ICIBA_LOWER_CASE, htmlToDom('error: 无法连接翻译服务'), time); showContent(); }); }; - obj[ids.YOUDAO] = function (text) { + obj[ids.YOUDAO] = function (text, time) { ajax('http://dict.youdao.com/jsonapi?xmlVersion=5.1&jsonversion=2&q=' + encodeURIComponent(text), function (rst) { - engineResult[ids.YOUDAO] = parseYoudao(rst); + putEngineResult(ids.YOUDAO, parseYoudao(rst), time) showContent(); }, function (rst) { - engineResult[ids.YOUDAO] = htmlToDom('error: 无法连接翻译服务'); + putEngineResult(ids.YOUDAO, htmlToDom('error: 无法连接翻译服务'), time); showContent(); }); }; - obj[ids.YOUDAO_LOWER_CASE] = function (text) { + obj[ids.YOUDAO_LOWER_CASE] = function (text, time) { ajax('http://dict.youdao.com/jsonapi?xmlVersion=5.1&jsonversion=2&q=' + encodeURIComponent(text.toLowerCase()), function (rst) { - engineResult[ids.YOUDAO_LOWER_CASE] = parseYoudao(rst); + putEngineResult(ids.YOUDAO_LOWER_CASE, parseYoudao(rst), time); showContent(); }, function (rst) { - engineResult[ids.YOUDAO_LOWER_CASE] = htmlToDom('error: 无法连接翻译服务'); + putEngineResult(ids.YOUDAO_LOWER_CASE, htmlToDom('error: 无法连接翻译服务'), time) showContent(); }); }; - obj[ids.BING] = function (text) { + obj[ids.BING] = function (text, time) { ajax('https://www4.bing.com/dict/search?q=' + encodeURIComponent(text), function (rst) { - engineResult[ids.BING] = parseBing(rst); + putEngineResult(ids.BING, parseBing(rst), time); showContent(); }, function (rst) { - engineResult[ids.BING] = htmlToDom('error: 无法连接翻译服务'); + putEngineResult(ids.BING, htmlToDom('error: 无法连接翻译服务'), time); showContent(); }); }; - obj[ids.BING_LOWER_CASE] = function (text) { + obj[ids.BING_LOWER_CASE] = function (text, time) { ajax('https://www4.bing.com/dict/search?q=' + encodeURIComponent(text.toLowerCase()), function (rst) { - engineResult[ids.BING_LOWER_CASE] = parseBing(rst); + putEngineResult(ids.BING_LOWER_CASE, parseBing(rst), time); showContent(); }, function (rst) { - engineResult[ids.BING_LOWER_CASE] = htmlToDom('error: 无法连接翻译服务'); + putEngineResult(ids.BING_LOWER_CASE, htmlToDom('error: 无法连接翻译服务'), time); showContent(); }); }; - obj[ids.HJENGLISH] = function (text) { + obj[ids.HJENGLISH] = function (text, time) { ajax('https://dict.hjenglish.com/w/' + encodeURIComponent(text), function (rst) { - engineResult[ids.HJENGLISH] = parseHjenglish(rst); + putEngineResult(ids.HJENGLISH, parseHjenglish(rst), time); showContent(); }, function (rst) { - engineResult[ids.HJENGLISH] = htmlToDom('error: 无法连接翻译服务'); + putEngineResult(ids.HJENGLISH, htmlToDom('error: 无法连接翻译服务'), time); showContent(); }, { headers: { @@ -592,7 +592,7 @@ } }); }; - obj[ids.GOOGLE] = function (text) { + obj[ids.GOOGLE] = function (text, time) { var url = 'https://translate.google.cn/translate_a/single?client=gtx&dt=t&dt=bd&dj=1&source=input&hl=zh-CN&sl=auto'; url += '&tk=' + token(text); if (hasChineseByRange(text)) { @@ -601,20 +601,20 @@ url += '&tl=zh-CN&q=' + encodeURIComponent(text); } ajax(url, function (rst) { - engineResult[ids.GOOGLE] = parseGoogle(rst); + putEngineResult(ids.GOOGLE, parseGoogle(rst), time); showContent(); }, function (rst) { - engineResult[ids.GOOGLE] = htmlToDom('error: 无法连接翻译服务'); + putEngineResult(ids.GOOGLE, htmlToDom('error: 无法连接翻译服务'), time); showContent(); }); }; - obj[ids.CAMBRIDGE] = function (text) { + obj[ids.CAMBRIDGE] = function (text, time) { var url = 'https://dictionary.cambridge.org/dictionary/english-chinese-simplified/' + encodeURIComponent(text); ajax(url, function (rst) { - engineResult[ids.CAMBRIDGE] = parseCambridge(rst); + putEngineResult(ids.CAMBRIDGE, parseCambridge(rst), time); showContent(); }, function (rst) { - engineResult[ids.CAMBRIDGE] = htmlToDom('error: 无法连接翻译服务'); + putEngineResult(ids.CAMBRIDGE, htmlToDom('error: 无法连接翻译服务'), time); showContent(); }); }; @@ -628,23 +628,23 @@ name: '多词典查询', id: 'icon-dict', image: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAAAwFBMVEX////9+/zc4eSwusOZprF1h5VpfIu5w8rk6Or3+PljeIjy8/X////M09h+kJ2ns7zr7vCMm6dccoNWbX7S2N3GztTAyc/X3OD+9fb95ub6xMX5t7j2qqv0iInydXbxcHH8bW2adH72m5vzfn/xbW7xa2zNcXXibnHxbm/xaGnwZGXwYGHwX2D70dLwXV75vr7z+/7o9/3S7/vL7frc8/yw5fqg3vaL1vRry/HL8f1bxfBVwu9Twu9MwO9Gvu5BvO1OQbHPAAAAAXRSTlMAQObYZgAABjtJREFUeAHsltWChDAMAK9O00DbBPn/Lz0J524bXph1zdBZ4erk5OTk5OTk5DcY63yInxN8Gi41P7sA3yGkfJH5xcM3ib5cYr6L8F1i+v/5OAb4PtP4/wtQ4SfUfxdoHX7C/38K7ARvIb7j3QbzfwvMAd7AwS3rUiPBG8KiIuCLMWboRwkQJIOIxhEdI8DBGrxli0cJ+IJty9j6MQJSYKxNGhwhwJNFdNOGOIcjBKRA9pTuGjDpC0iBNrHP0kBfgKcBcY5ysQU+QGDfdI6LwdaJtAUI1n0w1TsRUBeQ78A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fvtZY6wFBwM8I0BJ0xDaEW+QwFf4vIB1HPI/ydkZ/Y4xTws0EAk3XdlCETJCzwpAAeQBuk/MLoj7tACwpcS3ZFpeELDWJ4BpH39xAUxIFYdXkkAAOtDvOj/6VhHINz/P4/NN6OQAHahmY9YQ6Lap74HmbAmgAgIEmHF6eYGmoV2V/DoOzwsYaIGeW7uwQPfzTkiLBIXHYSigGQiIpQXyLd6WdZElyLfgW3NewGmGFhbYb9AmSdO0bX34OC18AS4IjMsJhKchiLR56dd/QWDxEtAiy/aw/DbdZ3lRvgXxj5sQBBC3i+0C2PtvlOCyLrewCbo/CN4F4H3EgkH47DasvgVpCKG/IKTBeBdStt8G0eznwCSfGkRZUORPdsfgIon4p4D+dRY9N4rbcnc7uIp9BoImGJUz+mGBOL9DoE6j7wL+MPCz0D2egSgpbo5f5vE3AaiBYRt/HlpnHxaI2mp74/rzJAoEjJMjAgOurP2lAB/1nQJRkhd1ia9UH4bzfhMFAlCEWUwIGIVU8AOtlBTC2PsEQCHNqytkbRJ5jq/l3gA4MC4E52xCh+BaviihgM+BHHv0lUHOf6GA7wMtxuHDoR9G6cxfKAC4WUs+HqZpOoyMSy+1nsC5h+ezs0oqO8/wiTFrChykNsdo/XMHHj1DovniAoMwd6DGxQUQ1OBmtByiFVKg/s4EAJO80UArHq3CwJXWV6NrJcdoJfqRC6kuIwUbon8gL168ePHixYs/AQllYQQ4I2UrAAAAAElFTkSuQmCC', - trigger: function (text) { + trigger: function (text, time) { idsType = idsExtension.LIST_DICT; if (text != text.toLowerCase()) { idsType = idsExtension.LIST_DICT_LOWER_CASE; // 大小写各请求一次 } idsType.forEach(function (id) { - idsExtension.engines[id](text); + idsExtension.engines[id](text, time); }); } }, { name: '谷歌翻译', id: 'icon-google', image: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAAAwFBMVEX////x+Pu83vl9vvRQq/Q0nvQsm/STyfXY6/pktfYjmfUhlvOr1Pb2+/0+pPT6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nn30FNj4J1MGHv1udn34ILLdgM36vP8ODsm8Dr7TqdYPYWmPgl8q+AEydOnDhx4sR/q8tILrvoB2AAAAAASUVORK5CYII=', - trigger: function (text) { + trigger: function (text, time) { idsType = idsExtension.LIST_GOOGLE; idsType.forEach(function (id) { - idsExtension.engines[id](text); + idsExtension.engines[id](text, time); }); } }]; @@ -665,10 +665,11 @@ content.scrollTop = 0; // 翻译面板滚动到顶端 content.scrollLeft = 0; // 翻译面板滚动到左端 engineId = obj.id; // 翻译引擎 ID + engineTriggerTime = new Date().getTime(); // 引擎触发时间 engineActivateShow(); // 显示翻译引擎指示器 audioEngines = []; // 清空发音引擎 engineResult = {}; // 清空翻译引擎结果集 - obj.trigger(selected); // 启动翻译引擎 + obj.trigger(selected, engineTriggerTime); // 启动翻译引擎 } }); icon.appendChild(img); @@ -933,6 +934,12 @@ } }); } + /**放入翻译引擎结果集*/ + function putEngineResult(id, value, time) { + if (time == engineTriggerTime) { // 是本次触发的异步ajax请求 + engineResult[id] = value; + } + } /**翻译引擎结果集状态判断*/ function isAllDone() { var rst = true; @@ -1033,6 +1040,7 @@ icon.style.display = 'none'; content.style.display = 'none'; engineId = ''; + engineTriggerTime = 0; audioEngines = []; engineResult = {}; engineActivateHide();