From 8a4324cb152ba6cae02f6a79322f9ed6a67d3969 Mon Sep 17 00:00:00 2001 From: example Date: Tue, 8 Jan 2019 12:58:01 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=B7=E6=AD=8C=E7=BF=BB=E8=AF=91=E3=80=81?= =?UTF-8?q?=E6=9C=89=E9=81=93=E8=AF=8D=E5=85=B8=E8=A7=A3=E6=9E=90=E8=A7=84?= =?UTF-8?q?=E5=88=99=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- translate/translate-dictionary.js | 72 +++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 13 deletions(-) diff --git a/translate/translate-dictionary.js b/translate/translate-dictionary.js index 28983d9..8a86c0c 100644 --- a/translate/translate-dictionary.js +++ b/translate/translate-dictionary.js @@ -1,7 +1,7 @@ // ==UserScript== // @name 划词翻译:有道词典,金山词霸,谷歌翻译 // @namespace http://tampermonkey.net/ -// @version 1.1 +// @version 1.2 // @description 划词翻译调用“有道词典(有道翻译)、金山词霸、谷歌翻译” // @author https://github.com/barrer // @match http://*/* @@ -102,8 +102,6 @@ #google .src, #google .confidence, #google .ld_result, - #google .encodeHTML, - #google .replaceArray, #none { display: none; } @@ -136,6 +134,43 @@ #google .terms { margin-right: .2em; } + + #youdao .phone { + color: #777; + } + + #youdao .phone:before { + content: "["; + } + + #youdao .phone:after { + content: "]"; + } + + #youdao .phrs:before { + content: "[短语]"; + display: block; + } + + #youdao .trs>.tr>.exam:before { + content: "[例句]"; + display: block; + } + + #youdao .trs>.tr>.l:before { + content: "[释义]"; + display: block; + } + + #youdao [class="#text"] { + font-style: italic; + } + + #youdao .return-phrase, + #youdao [class="@action"], + #none { + display: none; + } `; var link = document.createElement('link'); link.rel = 'stylesheet'; @@ -355,26 +390,32 @@ function objToXml(obj) { var xml = ''; for (var prop in obj) { - xml += obj[prop] instanceof Array ? '' : "<" + prop + ">"; + if (typeof obj[prop] === 'function') { + continue; + } + xml += obj[prop] instanceof Array ? '' : '<' + prop + '>'; if (obj[prop] instanceof Array) { for (var array in obj[prop]) { - xml += "<" + prop + ">"; + if (typeof obj[prop][array] === 'function') { + continue; + } + xml += '<' + prop + '>'; xml += objToXml(new Object(obj[prop][array])); - xml += ""; + xml += ''; } - } else if (typeof obj[prop] == "object") { + } else if (typeof obj[prop] == 'object') { xml += objToXml(new Object(obj[prop])); } else { xml += obj[prop]; } - xml += obj[prop] instanceof Array ? '' : ""; + xml += obj[prop] instanceof Array ? '' : ''; } var xml = xml.replace(/<\/?[0-9]{1,}>/g, ''); return xml } /**xml 转 html*/ function xmlToHtml(xml, tag) { - return xml.replace(/<([^/].+?)>/g, '<' + tag + ' class="$1">') + return xml.replace(/<([^/]+?)>/g, '<' + tag + ' class="$1">') .replace(/<\/(.+?)>/g, ''); } /**ajax 跨域访问公共方法*/ @@ -487,10 +528,15 @@ } // 中英翻译 if (rstJson.ce_new && rstJson.ce_new.word) { - rstJson.ce_new.word.forEach(function (w) { - if (w.phone) - html += '[' + w.phone + ']
'; - }); + html += '
' + + '《新汉英大辞典》
' + xmlToHtml(objToXml(rstJson.ce_new.word), 'div') + + '
'; + } + // 中文翻译 + if (rstJson.hh && rstJson.hh.word) { + html += '
' + + '《现代汉语大词典》
' + xmlToHtml(objToXml(rstJson.hh.word), 'span') + + '
'; } // 长句翻译 if (rstJson.fanyi && rstJson.fanyi.tran) {