diff --git a/README.md b/README.md index 84381d2..090ea13 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ ![translate-dictionary.jpg](translate/translate-dictionary.jpg) 特色: -* 划词翻译调用“有道词典(有道翻译)、金山词霸、Bing 词典(必应词典)、沪江小D、谷歌翻译” +* 划词翻译调用“有道词典(有道翻译)、金山词霸、Bing 词典(必应词典)、剑桥高阶、沪江小D、谷歌翻译” * 支持发音 * 支持显示当前翻译引擎 * 支持面板拖动 @@ -55,4 +55,4 @@ Tampermonkey 是一款免费的浏览器扩展和最为流行的用户脚本管 脚本名 | 用途 -|- translate.js|划词翻译调用“金山词霸、有道词典(有道翻译)、Google Translate(谷歌翻译)、沪江小D、搜狗翻译、必应词典(必应翻译)、Microsoft Translator(必应在线翻译)、海词词典、百度翻译、Oxford Learner's Dictionaries、Oxford Dictionaries、Merriam-Webster、汉典、PDF 划词翻译”网页翻译 -translate-dictionary.js|划词翻译调用“有道词典(有道翻译)、金山词霸、Bing 词典(必应词典)、沪江小D、谷歌翻译” +translate-dictionary.js|划词翻译调用“有道词典(有道翻译)、金山词霸、Bing 词典(必应词典)、剑桥高阶、沪江小D、谷歌翻译” diff --git a/translate/translate-dictionary.js b/translate/translate-dictionary.js index 1110246..1da75c5 100644 --- a/translate/translate-dictionary.js +++ b/translate/translate-dictionary.js @@ -1,20 +1,20 @@ // ==UserScript== // @name 划词翻译:多词典查询 // @namespace http://tampermonkey.net/ -// @version 2.1 -// @description 划词翻译调用“有道词典(有道翻译)、金山词霸、Bing 词典(必应词典)、沪江小D、谷歌翻译” +// @version 2.2 +// @description 划词翻译调用“有道词典(有道翻译)、金山词霸、Bing 词典(必应词典)、剑桥高阶、沪江小D、谷歌翻译” // @author https://github.com/barrer // @match http://*/* // @include https://*/* // @include file:///* // @run-at document-end -// @connect dict.youdao.com -// @connect open.iciba.com +// @connect youdao.com +// @connect iciba.com // @connect translate.google.cn // @connect hjenglish.com -// @connect hjapi.com -// @connect hjfile.cn // @connect bing.com +// @connect chinacloudapi.cn +// @connect cambridge.org // @grant GM_xmlhttpRequest // ==/UserScript== @@ -124,7 +124,7 @@ line-height: 18px; } - a.audio-button{ + a.audio-button { color: #36f; text-decoration: none; cursor: pointer; @@ -138,7 +138,7 @@ a.audio-button:hover { text-decoration: underline; } - + .br { border-top: 1px dashed #777; margin: .5em auto .3em auto; @@ -150,6 +150,7 @@ .list-title { color: #00c; + display: inline-block; } .google .sentences, @@ -267,7 +268,6 @@ margin-left: 2em; } - /*例句*/ .hjenglish .def-sentence-from, .hjenglish .def-sentence-to { display: none; @@ -291,7 +291,7 @@ .hjenglish ul { list-style: none; } - + .bing h1, .bing strong { font-size: 1em; @@ -299,29 +299,33 @@ margin: 0; padding: 0; } - + .bing .concise ul { list-style: none; margin: 0; padding: 0; } - + + .bing .hd_tf { + margin-right: 1em; + } + .bing .concise .pos { margin-right: .2em; } - + .bing .concise .web { margin-right: auto; } - + .bing .concise .web:after { content: ":" } - + .bing .oald { margin-top: .4em; } - + .bing .hd_tf_lh div { display: inline; color: #777; @@ -342,13 +346,12 @@ padding-right: .25em; } - /*例句*/ .bing .li_exs { display: none; } .bing .li_id { - border: 1px solid #ebebeb; + border: 0; padding: .2em; } @@ -359,7 +362,7 @@ .bing .gra { padding-right: .25em; } - + .bing .infor, .bing .label { padding-left: .25em; @@ -372,6 +375,63 @@ .bing .de_co div { display: inline; } + + .cambridge .entry~.entry { + margin-top: 1em; + } + + .cambridge p, + .cambridge h2, + .cambridge h3 { + padding: 0; + margin: 0; + } + + .cambridge h2, + .cambridge h3 { + font-size: 1em; + font-weight: normal; + } + + .cambridge .headword .hw { + display: block; + } + + .cambridge .pron { + color: #777; + } + + .cambridge b.def { + font-weight: normal; + } + + .cambridge .epp-xref { + border: 1px solid #777; + border-radius: .5em; + padding: 0 2px; + } + + .cambridge .examp, + .cambridge .extraexamps, + .cambridge .cols, + .cambridge .xref, + .cambridge .fcdo { + display: none; + } + + .cambridge .entry-body__el+.entry-body__el { + margin-top: 1em; + } + + .cambridge .pos::before { + content: "["; + padding-right: 3px; + } + + .cambridge .pos::after { + content: "]"; + padding-left: 3px; + } `; // iframe 工具库 var iframe = document.createElement('iframe'); @@ -403,11 +463,12 @@ BING: 'bing', BING_LOWER_CASE: 'bingLowerCase', HJENGLISH: 'hjenglish', - GOOGLE: 'google' + GOOGLE: 'google', + CAMBRIDGE: 'cambridge' }; var idsExtension = { - LIST_DICT: [ids.ICIBA, ids.YOUDAO, ids.BING, ids.HJENGLISH], - LIST_DICT_LOWER_CASE: [ids.ICIBA, ids.ICIBA_LOWER_CASE, ids.YOUDAO, ids.YOUDAO_LOWER_CASE, ids.BING, ids.BING_LOWER_CASE, ids.HJENGLISH], + LIST_DICT: [ids.BING, ids.CAMBRIDGE, ids.HJENGLISH, ids.ICIBA, ids.YOUDAO], + LIST_DICT_LOWER_CASE: [ids.BING, ids.BING_LOWER_CASE, ids.CAMBRIDGE, ids.HJENGLISH, ids.ICIBA, ids.ICIBA_LOWER_CASE, ids.YOUDAO, ids.YOUDAO_LOWER_CASE], LIST_GOOGLE: [ids.GOOGLE], lowerCaseMap: (function () { var obj = {}; @@ -426,6 +487,125 @@ obj[ids.BING_LOWER_CASE] = ''; obj[ids.HJENGLISH] = '《沪江小D》'; obj[ids.GOOGLE] = '《谷歌翻译》'; + obj[ids.CAMBRIDGE] = '《剑桥高阶》'; + return obj; + })(), + links: (function () { + var obj = {}; + obj[ids.ICIBA] = 'http://www.iciba.com/%q%'; + obj[ids.ICIBA_LOWER_CASE] = ''; + obj[ids.YOUDAO] = 'https://dict.youdao.com/w/eng/%q%'; + obj[ids.YOUDAO_LOWER_CASE] = ''; + obj[ids.BING] = 'https://cn.bing.com/dict/search?q=%q%'; + obj[ids.BING_LOWER_CASE] = ''; + obj[ids.HJENGLISH] = 'https://dict.hjenglish.com/w/%q%'; + obj[ids.GOOGLE] = function (text) { + var rst = ''; + if (hasChineseByRange(text)) { + rst = 'https://translate.google.cn/#view=home&op=translate&sl=auto&tl=en&text=' + encodeURIComponent(text); + } else { + rst = 'https://translate.google.cn/#view=home&op=translate&sl=auto&tl=zh-CN&text=' + encodeURIComponent(text); + } + return rst; + }; + obj[ids.CAMBRIDGE] = 'https://dictionary.cambridge.org/dictionary/english-chinese-simplified/%q%'; + return obj; + })(), + engines: (function () { + var obj = {}; + obj[ids.ICIBA] = function (text) { + ajax('http://open.iciba.com/huaci_v3/dict.php?word=' + encodeURIComponent(text), function (rst) { + engineResult[ids.ICIBA] = parseIciba(rst); + showContent(); + }, function (rst) { + engineResult[ids.ICIBA] = htmlToDom('error: 无法连接翻译服务'); + showContent(); + }); + }; + obj[ids.ICIBA_LOWER_CASE] = function (text) { + ajax('http://open.iciba.com/huaci_v3/dict.php?word=' + encodeURIComponent(text.toLowerCase()), function (rst) { + engineResult[ids.ICIBA_LOWER_CASE] = parseIciba(rst); + showContent(); + }, function (rst) { + engineResult[ids.ICIBA_LOWER_CASE] = htmlToDom('error: 无法连接翻译服务'); + showContent(); + }); + }; + obj[ids.YOUDAO] = function (text) { + ajax('http://dict.youdao.com/jsonapi?xmlVersion=5.1&jsonversion=2&q=' + encodeURIComponent(text), function (rst) { + engineResult[ids.YOUDAO] = parseYoudao(rst); + showContent(); + }, function (rst) { + engineResult[ids.YOUDAO] = htmlToDom('error: 无法连接翻译服务'); + showContent(); + }); + }; + obj[ids.YOUDAO_LOWER_CASE] = function (text) { + ajax('http://dict.youdao.com/jsonapi?xmlVersion=5.1&jsonversion=2&q=' + encodeURIComponent(text.toLowerCase()), function (rst) { + engineResult[ids.YOUDAO_LOWER_CASE] = parseYoudao(rst); + showContent(); + }, function (rst) { + engineResult[ids.YOUDAO_LOWER_CASE] = htmlToDom('error: 无法连接翻译服务'); + showContent(); + }); + }; + obj[ids.BING] = function (text) { + ajax('https://www4.bing.com/dict/search?q=' + encodeURIComponent(text), function (rst) { + engineResult[ids.BING] = parseBing(rst); + showContent(); + }, function (rst) { + engineResult[ids.BING] = htmlToDom('error: 无法连接翻译服务'); + showContent(); + }); + }; + obj[ids.BING_LOWER_CASE] = function (text) { + ajax('https://www4.bing.com/dict/search?q=' + encodeURIComponent(text.toLowerCase()), function (rst) { + engineResult[ids.BING_LOWER_CASE] = parseBing(rst); + showContent(); + }, function (rst) { + engineResult[ids.BING_LOWER_CASE] = htmlToDom('error: 无法连接翻译服务'); + showContent(); + }); + }; + obj[ids.HJENGLISH] = function (text) { + ajax('https://dict.hjenglish.com/w/' + encodeURIComponent(text), function (rst) { + engineResult[ids.HJENGLISH] = parseHjenglish(rst); + showContent(); + }, function (rst) { + engineResult[ids.HJENGLISH] = htmlToDom('error: 无法连接翻译服务'); + showContent(); + }, { + headers: { + 'Cookie': 'HJ_SID=' + uuid() + '; HJ_SSID_3=' + uuid() + '; HJ_CST=1; HJ_CSST_3=1; HJ_UID=' + uuid() + } + }); + }; + obj[ids.GOOGLE] = function (text) { + 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)) { + url += '&tl=en&q=' + encodeURIComponent(text); + } else { + url += '&tl=zh-CN&q=' + encodeURIComponent(text); + } + ajax(url, function (rst) { + engineResult[ids.GOOGLE] = parseGoogle(rst); + showContent(); + }, function (rst) { + engineResult[ids.GOOGLE] = htmlToDom('error: 无法连接翻译服务'); + showContent(); + }); + }; + obj[ids.CAMBRIDGE] = function (text) { + var url = 'https://dictionary.cambridge.org/dictionary/english-chinese-simplified/' + encodeURIComponent(text); + ajax(url, function (rst) { + engineResult[ids.CAMBRIDGE] = parseCambridge(rst); + showContent(); + }, function (rst) { + engineResult[ids.CAMBRIDGE] = htmlToDom('error: 无法连接翻译服务'); + showContent(); + }); + }; return obj; })() } @@ -439,63 +619,11 @@ trigger: function (text) { idsType = idsExtension.LIST_DICT; if (text != text.toLowerCase()) { - idsType = idsExtension.LIST_DICT_LOWER_CASE; + idsType = idsExtension.LIST_DICT_LOWER_CASE; // 大小写各请求一次 } - ajax('http://open.iciba.com/huaci_v3/dict.php?word=' + encodeURIComponent(text), function (rst) { - engineResult[ids.ICIBA] = parseIciba(rst); - showContent(); - }, function (rst) { - engineResult[ids.ICIBA] = htmlToDom('error: 无法连接翻译服务'); - showContent(); + idsType.forEach(function (id) { + idsExtension.engines[id](text); }); - ajax('http://dict.youdao.com/jsonapi?xmlVersion=5.1&jsonversion=2&q=' + encodeURIComponent(text), function (rst) { - engineResult[ids.YOUDAO] = parseYoudao(rst); - showContent(); - }, function (rst) { - engineResult[ids.YOUDAO] = htmlToDom('error: 无法连接翻译服务'); - showContent(); - }); - ajax('https://www4.bing.com/dict/search?q=' + encodeURIComponent(text), function (rst) { - engineResult[ids.BING] = parseBing(rst); - showContent(); - }, function (rst) { - engineResult[ids.BING] = htmlToDom('error: 无法连接翻译服务'); - showContent(); - }); - ajax('https://dict.hjenglish.com/w/' + encodeURIComponent(text), function (rst) { - engineResult[ids.HJENGLISH] = parseHjenglish(rst); - showContent(); - }, function (rst) { - engineResult[ids.HJENGLISH] = htmlToDom('error: 无法连接翻译服务'); - showContent(); - }, { - headers: { - 'Cookie': 'HJ_SID=' + uuid() + '; HJ_SSID_3=' + uuid() + '; HJ_CST=1; HJ_CSST_3=1; HJ_UID=' + uuid() - } - }); - if (text != text.toLowerCase()) { // 小写再请求一次 - ajax('http://open.iciba.com/huaci_v3/dict.php?word=' + encodeURIComponent(text.toLowerCase()), function (rst) { - engineResult[ids.ICIBA_LOWER_CASE] = parseIciba(rst); - showContent(); - }, function (rst) { - engineResult[ids.ICIBA_LOWER_CASE] = htmlToDom('error: 无法连接翻译服务'); - showContent(); - }); - ajax('http://dict.youdao.com/jsonapi?xmlVersion=5.1&jsonversion=2&q=' + encodeURIComponent(text.toLowerCase()), function (rst) { - engineResult[ids.YOUDAO_LOWER_CASE] = parseYoudao(rst); - showContent(); - }, function (rst) { - engineResult[ids.YOUDAO_LOWER_CASE] = htmlToDom('error: 无法连接翻译服务'); - showContent(); - }); - ajax('https://www4.bing.com/dict/search?q=' + encodeURIComponent(text.toLowerCase()), function (rst) { - engineResult[ids.BING_LOWER_CASE] = parseBing(rst); - showContent(); - }, function (rst) { - engineResult[ids.BING_LOWER_CASE] = htmlToDom('error: 无法连接翻译服务'); - showContent(); - }); - } } }, { name: '谷歌翻译', @@ -503,18 +631,8 @@ image: 'data:image/png;base64,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', trigger: function (text) { idsType = idsExtension.LIST_GOOGLE; - 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)) - url += '&tl=en&q=' + encodeURIComponent(text); - else - url += '&tl=zh-CN&q=' + encodeURIComponent(text); - ajax(url, function (rst) { - engineResult[ids.GOOGLE] = parseGoogle(rst); - showContent(); - }, function (rst) { - engineResult[ids.GOOGLE] = htmlToDom('error: 无法连接翻译服务'); - showContent(); + idsType.forEach(function (id) { + idsExtension.engines[id](text); }); } }]; @@ -527,8 +645,9 @@ img.setAttribute('icon-id', obj.id); img.addEventListener('mouseup', function () { if (!isDrag()) { // 没有拖动鼠标抬起的时候触发点击事件 - if (engineId == obj.id) + if (engineId == obj.id) { return; // 已经是当前翻译引擎 + } contentList.innerHTML = ''; // 清空翻译内容列表 content.style.display = 'block'; // 立马显示翻译面板 content.scrollTop = 0; // 翻译面板滚动到顶端 @@ -621,8 +740,9 @@ /**日志输出*/ function log() { var debug = false; - if (!debug) + if (!debug) { return; + } if (arguments) { for (var i = 0; i < arguments.length; i++) { console.log(arguments[i]); @@ -664,8 +784,9 @@ }; this.dragElement = function (e) { log('dragging'); - if (!ref.dragging) + if (!ref.dragging) { return; + } e.preventDefault(); // move element element.style.left = ref.elementOriginalLeft + (e.clientX - ref.mouseDownPositionX) + 'px'; @@ -762,18 +883,21 @@ } /**ajax 跨域访问公共方法*/ function ajax(url, success, error, obj) { - if (!!!obj) + if (!!!obj) { obj = {}; - if (!!!obj.method) + } + if (!!!obj.method) { obj.method = 'GET'; + } // >>>因为Tampermonkey跨域访问(a.com)时会自动携带对应域名(a.com)的对应cookie // 不会携带当前域名的cookie // 所以,GM_xmlhttpRequest【不存在】cookie跨域访问安全性问题 // 以下设置的cookie会添加到已有cookie的后面<<< - if (!!!obj.headers) + if (!!!obj.headers) { obj.headers = { 'cookie': '' }; + } GM_xmlhttpRequest({ method: obj.method, url: url, @@ -799,14 +923,15 @@ } /**翻译引擎结果集状态判断*/ function isAllDone() { - var rst = false; - idsType.forEach(function (id, i) { - if (i == 0) - rst = true; - if (!(id in engineResult)) { - log('isAllDone(not found):' + id); - rst = false; - return rst; + var rst = true; + var count = 0; + idsType.forEach(function (id) { + if (count < 1) { // 判断指定数量的完成情况 + count++; + if (!(id in engineResult)) { + log('isAllDone(not found):' + id); + rst = false; + } } }); return rst; @@ -816,16 +941,18 @@ log('showContent:', idsType, engineResult); if (isAllDone()) { log('showContent allDone'); + } else { + return; } log('showContent rendering'); // 发音 audioEngines = []; // 清空防止多次渲染 audioEngines.push({ - name: '♪US', + name: '♪us', url: 'http://dict.youdao.com/dictvoice?audio=' + selected + '&type=2' }); audioEngines.push({ - name: '♪UK', + name: '♪uk', url: 'http://dict.youdao.com/dictvoice?audio=' + selected + '&type=1' }); var audio = document.createElement('tr-audio'); @@ -846,9 +973,29 @@ idsType.forEach(function (id) { if (engineResult[id] && !(id in needDel)) { if (idsExtension.names[id]) { - var title = document.createElement('div'); + var title = document.createElement('a'); title.innerHTML = idsExtension.names[id]; title.setAttribute('class', 'list-title'); + // 添加跳转 + var href = 'javascript:void(0)'; + if (idsExtension.links[id]) { + var link = idsExtension.links[id]; + if (typeof link == 'string') { + if (link.length > 0) { + href = link.replace(/%q%/ig, encodeURIComponent(selected)); + } + } else if (typeof link == 'function') { + var fnHref = link(selected); + if (fnHref.length > 0) { + href = fnHref; + } + } + } + title.setAttribute('rel', 'noreferrer noopener'); + title.setAttribute('target', '_blank'); + title.setAttribute('href', href); + // 事件 + title.addEventListener('click', linkEvent); contentList.appendChild(title); } contentList.appendChild(engineResult[id]); @@ -905,6 +1052,12 @@ }); return type; } + /**链接点击事件*/ + function linkEvent(e) { + if (isDrag()) { // 拖动时候不触发跳转 + e.preventDefault(); + } + } /**有道词典排版*/ function parseYoudao(rst) { var html = ''; @@ -983,8 +1136,9 @@ .replace(/__mystyle__/g, ' style="color:#777;"'); var match = /dict.innerHTML='(.*)?';/g.exec(rst); html += match[1]; - if (html.indexOf('去爱词霸官网翻译') != -1) + if (html.indexOf('去爱词霸官网翻译') != -1) { html = ''; + } } catch (error) { log(error); html += error; @@ -1001,30 +1155,12 @@ try { var doc = htmlToDom(cleanHtml(rst)); var entry = doc.querySelector('.word-text h2'); - var pronounces = doc.querySelector('.pronounces'); var collins = doc.querySelector('div[data-id="detail"] .word-details-item-content .detail-groups'); if (entry) { var entryDom = document.createElement('div'); entryDom.setAttribute('class', 'entry'); entryDom.innerHTML = entry.innerHTML; dom.appendChild(entryDom); - if (pronounces) { - var pronounceDom = document.createElement('div'); - pronounces.querySelectorAll('.pronounces [class="word-audio"]').forEach(function (ele) { - pronounceDom.appendChild(getPlayButton({ - name: '♪US', - url: ele.getAttribute('data-src') - })); - }); - pronounces.querySelectorAll('.pronounces [class="word-audio word-audio-en"]').forEach(function (ele) { - pronounceDom.appendChild(getPlayButton({ - name: '♪UK', - url: ele.getAttribute('data-src') - })); - }); - dom.appendChild(pronounceDom); - dom.appendChild(pronounces); - } if (collins) { dom.appendChild(htmlToDom('
《权威词典》
')); dom.appendChild(collins); @@ -1040,6 +1176,7 @@ function parseBing(rst) { var html = ''; try { + rst = rst.replace(/onmouseover/ig, 'data-sound'); rst = cleanHtml(rst).replace(/(?:a>)/ig, 'span>') .replace(/(?:'; - if (concise) + if (concise) { html += '
' + concise.outerHTML + '
'; - if (tense) + } + if (tense) { html += '
' + tense.outerHTML + '
'; - if (oald) + } + if (oald) { html += '
《牛津高阶英汉双解词典第八版》
' + oald.outerHTML + '
'; + } } // 计算机翻译 var machineTrans = doc.querySelector('.smt_hw'); @@ -1075,6 +1215,18 @@ var dom = document.createElement('div'); dom.setAttribute('class', ids.BING); dom.innerHTML = html; + // 发音 + dom.querySelectorAll('[data-sound]').forEach(function (ele) { + var str = ele.getAttribute('data-sound'); + var regex = /'(https:\/\/.*?)'/ig; + var match = regex.exec(str); + if (match && match.length >= 1) { + ele.appendChild(getPlayButton({ + name: '♫', + url: match[1] + })); + } + }); return dom; } /**谷歌翻译排版*/ @@ -1082,8 +1234,6 @@ var dom = document.createElement('div'); dom.setAttribute('class', ids.GOOGLE); try { - // 发音 - // 内容 dom.appendChild(htmlToDom(xmlToHtml(objToXml(iframeWin.JSON.parse(rst)), 'span'))); } catch (error) { log(error); @@ -1091,6 +1241,31 @@ } return dom; } + /**剑桥高阶排版*/ + function parseCambridge(rst) { + var dom = document.createElement('div'); + dom.setAttribute('class', ids.CAMBRIDGE); + try { + rst = cleanHtml(rst).replace(/(?:a>)/ig, 'span>') + .replace(/(?: