From a4cfd1279912c20483b291d05485038b10baba58 Mon Sep 17 00:00:00 2001 From: example Date: Fri, 4 Jan 2019 22:16:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B1=89=E8=AF=AD=E6=8B=BC=E9=9F=B3=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E8=A7=84=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 | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/translate/translate-dictionary.js b/translate/translate-dictionary.js index 9cddf0b..400f89f 100644 --- a/translate/translate-dictionary.js +++ b/translate/translate-dictionary.js @@ -1,7 +1,7 @@ // ==UserScript== // @name 划词翻译:有道词典,金山词霸 // @namespace http://tampermonkey.net/ -// @version 0.3 +// @version 0.4 // @description 划词翻译调用“有道词典(有道翻译)、金山词霸” // @author https://github.com/barrer // @match http://*/* @@ -249,7 +249,8 @@ function parseYoudao(rst) { try { var rstJson = JSON.parse(rst), - html = ''; + html = '', + phoneStyle = 'color:#777!important;'; if (rstJson.ec) { var word = rstJson.ec.word[0], tr = ''; @@ -257,7 +258,6 @@ ukphone = word.ukphone, usphone = word.usphone, phone = word.phone; - var phoneStyle = 'color:#777!important;'; if (ukphone && ukphone.length != 0) { html += '英[' + ukphone + '] '; } @@ -274,6 +274,14 @@ }); html += tr; } + // 中英翻译 + if (rstJson.ce_new && rstJson.ce_new.word) { + rstJson.ce_new.word.forEach(function (w) { + if (w.phone) + html += '[' + w.phone + ']
'; + }); + } + // 长句翻译 if (rstJson.fanyi && rstJson.fanyi.tran) { html += rstJson.fanyi.tran; } @@ -285,13 +293,20 @@ } /**金山词霸排版*/ function parseIciba(rst) { - rst = rst.replace(/\\"/g, '"') - .replace(//g, '') + rst = rst.replace(/class=\\"icIBahyI-prons\\"/g, '__mystyle__') // 音标 + .replace(/\\"/g, '"') // 引号 + // A标签 + .replace(/]*)?>详细释义<\/a([^>]*)?>/g, '') + .replace(/]*)?>/g, '') + .replace(/<\/a([^>]*)?>/g, '') + // 清理属性、标签、多余空格 .replace(/(?:class|id|style|xml:lang|lang)=\"([^"]*)\"/g, '') .replace(/(?:label>|strong>)/g, 'span>') .replace(/(?:)/g, 'div>') - .replace(/[ ]+/g, ' '); + .replace(/[ ]+/g, ' ') + // 音标 + .replace(/__mystyle__/g, ' style="color:#777!important;"'); var match = /dict.innerHTML='(.*)?';/g.exec(rst); return match[1]; }