// ==UserScript== // @name 划词翻译:多词典查询 // @namespace http://tampermonkey.net/ // @version 6.5 // @description 划词翻译调用“有道词典(有道翻译)、金山词霸、Bing 词典(必应词典)、剑桥高阶、沪江小D、谷歌翻译” // @author https://github.com/barrer // @match http://*/* // @include https://*/* // @include file:///* // @run-at document-start // @connect hjenglish.com // @grant GM_xmlhttpRequest // ==/UserScript== (function () { 'use strict'; // Your code here... /**联网权限*/ // @connect hjenglish.com 沪江小D /**样式*/ var style = document.createElement('style'); // >>>>> 可以自定义的变量 var fontSize = 14; // 字体大小 var iconWidth = 300; // 整个面板宽度 var iconHeight = 400; // 整个面板高度 // 可以自定义的变量 <<<<< (自定义变量修改后把 “@version” 版本号改为 “10000” 防止更新后消失) var trContentWidth = iconWidth - 16; // 整个面板宽度 - 边距间隔 = 翻译正文宽度 var trContentHeight = iconHeight - 35; // 整个面板高度 - 边距间隔 = 翻译正文高度 var zIndex = '2147483647'; // 渲染图层 style.textContent = ` /*组件样式*/ :host{all:unset!important} :host{all:initial!important} *{word-wrap:break-word!important;word-break:break-word!important} a{color:#00c;text-decoration:none;cursor:pointer} a:hover{text-decoration:none} a:active{text-decoration:underline} img{cursor:pointer;display:inline-block;width:20px;height:20px;border:1px solid #dfe1e5;border-radius:4px;background-color:rgba(255,255,255,1);padding:2px;margin:0;margin-right:5px;box-sizing:content-box;vertical-align:middle} img:last-of-type{margin-right:auto} img:hover{border:1px solid #f90} img[activate]{border:1px solid #f90} img[activate]:hover{border:1px solid #f90} table{font-size:inherit;color:inherit} tr-icon{display:none;position:absolute;padding:0;margin:0;cursor:move;box-sizing:content-box;font-size:${fontSize}px;text-align:left;border:0;border-radius:4px;color:black;z-index:${zIndex};background:transparent} tr-icon[activate]{background:#fff;-webkit-box-shadow:0 3px 8px 0 rgba(0,0,0,0.2),0 0 0 0 rgba(0,0,0,0.08);box-shadow:0 3px 8px 0 rgba(0,0,0,0.2),0 0 0 0 rgba(0,0,0,0.08)} tr-audio{display:block;margin-bottom:5px} tr-audio a{margin-right:1em;font-size:80%} tr-audio a:last-of-type{margin-right:auto} tr-content{display:none;width:${trContentWidth}px;height:auto;overflow-x:hidden;overflow-y:scroll;background:white;padding:2px 8px;margin-top:5px;box-sizing:content-box;font-family:"Helvetica Neue","Helvetica","Arial","sans-serif";font-size:${fontSize}px;font-weight:normal;line-height:normal;-webkit-font-smoothing:auto;font-smoothing:auto;text-rendering:auto} tr-engine~tr-engine{margin-top:1em} tr-engine .title{color:#00c;display:inline-block;font-weight:bold} tr-engine .title:hover{text-decoration:none} /*各引擎样式*/ .hjenglish dl,.hjenglish dt,.hjenglish dd,.hjenglish p,.hjenglish ul,.hjenglish li,.hjenglish h3{margin:0;padding:0;margin-block-start:0;margin-block-end:0;margin-inline-start:0;margin-inline-end:0} .hjenglish h3{font-size:1em;font-weight:normal} .hjenglish .detail-pron,.hjenglish .pronounces{color:#777} .hjenglish ul{margin-left:2em} .hjenglish .def-sentence-from,.hjenglish .def-sentence-to{display:none} .hjenglish .detail-groups dd h3:before{counter-increment:eq;content:counter(eq) ".";display:block;width:22px;float:left} .hjenglish .detail-groups dl{counter-reset:eq;margin-bottom:.5em;clear:both} .hjenglish ol,.hjenglish ul{list-style:none} .hjenglish dd{margin-left:1em} .hjenglish dd>p{margin-left:2.5em} .hjenglish .pronounces .word-audio {margin-left:2.5em} `; // iframe 工具库 var iframe = document.createElement('iframe'); 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'), //翻译内容结果集(HTML内容)列表 selected, // 当前选中文本 engineId, // 当前翻译引擎 engineTriggerTime, // 引擎触发时间(milliseconds) idsType, // 当前翻译面板内容列表数组 pageX, // 图标显示的 X 坐标 pageY; // 图标显示的 Y 坐标 // 初始化内容面板 content.appendChild(contentList); // 发音缓存 var audioCache = {}; // {'mp3 download url': data} // 翻译引擎结果集 var engineResult = {}; // id: DOM // 唯一 ID var ids = { HJENGLISH: 'hjenglish', }; // 唯一 ID 扩展 var idsExtension = { // ID 组 LIST_DICT: [ids.HJENGLISH,], LIST_DICT_LOWER_CASE: [ids.HJENGLISH,], // 去重比对(大小写翻译可能一样) lowerCaseMap: (function () { var obj = {}; return obj; })(), // 标题 names: (function () { var obj = {}; obj[ids.HJENGLISH] = '沪江小D'; return obj; })(), // 跳转到网站(“%q%”占位符或者 function text -> return URL) links: (function () { var obj = {}; obj[ids.HJENGLISH] = 'https://dict.hjenglish.com/jp/jc/%q%'; return obj; })(), // 翻译引擎 engines: (function () { var obj = {}; obj[ids.HJENGLISH] = function (text, time) { ajax('https://dict.hjenglish.com/jp/jc/' + encodeURIComponent(text), function (rst) { putEngineResult(ids.HJENGLISH, parseHjenglish(rst), time); showContent(); }, function (rst) { putEngineResult(ids.HJENGLISH, htmlToDom('error: 无法连接翻译服务'), time); showContent(); }, { headers: { 'Cookie': 'HJ_SID=' + uuid() + '; HJ_SSID_3=' + uuid() + '; HJ_CST=1; HJ_CSST_3=1; HJ_UID=' + uuid(), 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36' } }); }; return obj; })() }; // 绑定图标拖动事件 var iconDrag = new Drag(icon); // 图标数组 var iconArray = [{ name: '多词典查询', id: 'icon-dict', image: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAAAXNSR0IArs4c6QAAFtFJREFUeAHNXWuMXddV3ufOnbE9xtTP2DUo2I6dUNwWKWooVmniCFVUbcCFqpXyi0iIlqoBIX7wbrBUlX+AKhKqWkiJQFA1VWgjUEVLpDZpiqEuSaGE0jgPJzFOHMevjO2ZsWfO4fvW2t85++5z78y5dh13W+estdde7732Pvvce20XYcz2kW9XkxeeD7dA7N1FEXZVIWwrQtgKuKaqwppeCCuAT+DqYbwHGoA30AJxQjbh9TgGyAwZbxlD1gUflRcGTUDC0aLxY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idsExtension.LIST_DICT; if (text != text.toLowerCase()) { idsType = idsExtension.LIST_DICT_LOWER_CASE; // 改为大小写 ID 组(大小写各请求一次) } idsType.forEach(function (id) { idsExtension.engines[id](text, time); }); initContent(); // 初始化翻译面板 displayContent(); // 立马显示翻译面板 } }]; // 添加翻译引擎图标 iconArray.forEach(function (obj) { var img = document.createElement('img'); img.setAttribute('src', obj.image); img.setAttribute('alt', obj.name); img.setAttribute('title', obj.name); img.setAttribute('icon-id', obj.id); img.addEventListener('mouseup', function () { if (engineId == obj.id) { // 已经是当前翻译引擎,不做任何处理 } else { icon.setAttribute('activate', 'activate'); // 标注面板展开 engineId = obj.id; // 翻译引擎 ID engineTriggerTime = new Date().getTime(); // 引擎触发时间 engineActivateShow(); // 显示翻译引擎指示器 audioCache = {}; // 清空发音缓存 engineResult = {}; // 清空翻译引擎结果集 obj.trigger(selected, engineTriggerTime); // 启动翻译引擎 } }); icon.appendChild(img); }); // 添加内容面板(放图标后面) icon.appendChild(content); // 添加样式、翻译图标到 DOM var root = document.createElement('div'); document.documentElement.appendChild(root); var shadow = root.attachShadow({ mode: 'closed' }); // iframe 工具库加入 Shadow shadow.appendChild(iframe); iframeWin = iframe.contentWindow; iframeDoc = iframe.contentDocument; // 外部样式表 var link = document.createElement('link'); link.rel = 'stylesheet'; link.type = 'text/css'; link.href = createObjectURLWithTry(new Blob(['\ufeff', style.textContent], { type: 'text/css;charset=UTF-8' })); // 多种方式最大化兼容:Content Security Policy // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy shadow.appendChild(style); // 内部样式表 shadow.appendChild(link); // 外部样式表 // 翻译图标加入 Shadow shadow.appendChild(icon); // 鼠标事件:防止选中的文本消失 document.addEventListener('mousedown', function (e) { log('mousedown event:', e); if (e.target === icon || (e.target.parentNode && e.target.parentNode === icon)) { // 点击了翻译图标 e.preventDefault(); } }); // 鼠标事件:防止选中的文本消失;显示、隐藏翻译图标 document.addEventListener('mouseup', showIcon); // 选中变化事件 document.addEventListener('selectionchange', showIcon); document.addEventListener('touchend', showIcon); // 内容面板滚动事件 content.addEventListener('scroll', function (e) { if (content.scrollHeight - content.scrollTop === content.clientHeight) { log('scroll bottom', e); e.preventDefault(); e.stopPropagation(); } else if (content.scrollTop === 0) { log('scroll top', e); e.preventDefault(); e.stopPropagation(); } }); /**日志输出*/ function log() { var debug = false; if (!debug) { return; } if (arguments) { for (var i = 0; i < arguments.length; i++) { console.log(arguments[i]); } } } /**鼠标拖动*/ function Drag(element) { this.dragging = false; this.startDragTime = 0; this.stopDragTime = 0; this.mouseDownPositionX = 0; this.mouseDownPositionY = 0; this.elementOriginalLeft = parseInt(element.style.left); this.elementOriginalTop = parseInt(element.style.top); var ref = this; this.startDrag = function (e) { e.preventDefault(); ref.dragging = true; ref.startDragTime = new Date().getTime(); ref.mouseDownPositionX = e.clientX; ref.mouseDownPositionY = e.clientY; ref.elementOriginalLeft = parseInt(element.style.left); ref.elementOriginalTop = parseInt(element.style.top); // set mousemove event window.addEventListener('mousemove', ref.dragElement); log('startDrag'); }; this.unsetMouseMove = function () { // unset mousemove event window.removeEventListener('mousemove', ref.dragElement); }; this.stopDrag = function (e) { e.preventDefault(); ref.dragging = false; ref.stopDragTime = new Date().getTime(); ref.unsetMouseMove(); log('stopDrag'); }; this.dragElement = function (e) { log('dragging'); if (!ref.dragging) { return; } e.preventDefault(); // move element element.style.left = ref.elementOriginalLeft + (e.clientX - ref.mouseDownPositionX) + 'px'; element.style.top = ref.elementOriginalTop + (e.clientY - ref.mouseDownPositionY) + 'px'; log('dragElement'); }; element.onmousedown = this.startDrag; element.onmouseup = this.stopDrag; } /**是否拖动图标*/ function isDrag() { return iconDrag.elementOriginalLeft !== parseInt(icon.style.left) || iconDrag.elementOriginalTop !== parseInt(icon.style.top); } /**强制结束拖动*/ function forceStopDrag() { if (iconDrag) { // 强制设置鼠标拖动事件结束,防止由于网页本身的其它鼠标事件冲突而导致没有侦测到:mouseup iconDrag.dragging = false; iconDrag.unsetMouseMove(); } } /**是否包含汉字*/ function hasChineseByRange(str) { return /[\u4e00-\u9fa5]/ig.test(str); } /**uuid*/ function uuid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } /**对象转 xml*/ function objToXml(obj) { var xml = ''; for (var prop in obj) { if (obj[prop] instanceof iframeWin.Function) { continue; } xml += obj[prop] instanceof iframeWin.Array ? '' : '<' + prop + '>'; if (obj[prop] instanceof iframeWin.Array) { for (var array in obj[prop]) { if (obj[prop][array] instanceof iframeWin.Function) { continue; } xml += '<' + prop + '>'; xml += objToXml(new iframeWin.Object(obj[prop][array])); xml += ''; } } else if (obj[prop] instanceof iframeWin.Object) { xml += objToXml(new iframeWin.Object(obj[prop])); } else { xml += obj[prop]; } xml += obj[prop] instanceof iframeWin.Array ? '' : ''; } xml = xml.replace(/<\/?[0-9]{1,}>/g, ''); return xml } /**xml 转 html*/ function xmlToHtml(xml, tag) { return xml.replace(/<([^/]+?)>/g, '<' + tag + ' class="$1">') .replace(/<\/(.+?)>/g, ''); } // html 字符串转 DOM function htmlToDom(html) { var div = document.createElement('div'); div.innerHTML = html; return div; } /**清理 html*/ function cleanHtml(html) { html = html.replace(//ig, '') .replace(//ig, '') .replace(//ig, '') .replace(//ig, ''); html = cleanAttr(html, 'on[a-z]*'); return html; } /** * 清理指定属性(忽略大小写) * @param attr 支持正则表示(如“on[a-z]*”,表示清理“on”开头的属性:onclick、onmove等) */ function cleanAttr(html, attr) { var regex = ' ' + attr + '="([^"<>]*)"'; return html.replace(new RegExp(regex, 'ig'), ''); } /**带异常处理的 createObjectURL*/ function createObjectURLWithTry(blob) { try { return iframeWin.URL.createObjectURL(blob); } catch (error) { log(error); } return ''; } /**ajax 跨域访问公共方法*/ function ajax(url, success, error, obj) { if (!!!obj) { obj = {}; } if (!!!obj.method) { obj.method = 'GET'; } // >>>因为Tampermonkey跨域访问(a.com)时会自动携带对应域名(a.com)的对应cookie // 不会携带当前域名的cookie // 所以,GM_xmlhttpRequest【不存在】cookie跨域访问安全性问题 // 以下设置的cookie会添加到已有cookie的后面<<< if (!!!obj.headers) { obj.headers = { 'cookie': '' }; } GM_xmlhttpRequest({ method: obj.method, url: url, headers: obj.headers, responseType: obj.responseType, data: obj.data, onload: function (res) { success(res.responseText, res, obj); }, onerror: function (res) { error(res.responseText, res, obj); }, onabort: function (res) { error('the request was aborted', res, obj); }, ontimeout: function (res) { error('the request failed due to a timeout', res, obj); }, onreadystatechange: function () { log('ajax:', arguments); } }); } /**放入翻译引擎结果集*/ function putEngineResult(id, value, time) { if (time == engineTriggerTime) { // 是本次触发的异步ajax请求 engineResult[id] = value; } } /**初始化面板*/ function initContent() { contentList.innerHTML = ''; // 清空翻译内容列表 // 初始化翻译引擎结构(此时内容暂未填充) idsType.forEach(function (id) { if (id in idsExtension.names) { var engine = document.createElement('tr-engine'); engine.setAttribute('data-id', id); engine.style.display = 'none'; // 暂无内容默认隐藏 // 标题 if (idsExtension.names[id]) { var title = document.createElement('a'); title.innerHTML = idsExtension.names[id]; title.setAttribute('class', '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.setAttribute('title', '打开源网站'); engine.appendChild(title); } contentList.appendChild(engine); } }); } /**显示内容面板*/ function displayContent() { var panelWidth = iconWidth + 8; // icon 展开后总宽度(8:冗余距离) var panelHeight = iconHeight + 8; // icon 展开后总高度(8:冗余距离) // 计算位置 log('content position:', 'window.scrollY', window.scrollY, 'document.documentElement.scrollTop', document.documentElement.scrollTop, 'document.body.scrollTop', document.body.scrollTop, 'window.innerHeight', window.innerHeight, 'document.documentElement.clientHeight', document.documentElement.clientHeight, 'document.body.clientHeight', document.body.clientHeight, 'icon.style.top', icon.style.top, 'window.scrollX', window.scrollX, 'document.documentElement.scrollLeft', document.documentElement.scrollLeft, 'document.body.scrollLeft', document.body.scrollLeft, 'window.innerWidth', window.innerWidth, 'document.documentElement.clientWidth', document.documentElement.clientWidth, 'document.body.clientWidth', document.body.clientWidth, 'icon.style.left', icon.style.left ); var scrollTop = Math.max(parseInt(document.documentElement.scrollTop), parseInt(document.body.scrollTop)); var scrollLeft = Math.max(parseInt(document.documentElement.scrollLeft), parseInt(document.body.scrollLeft)); var clientHeight = [parseInt(document.documentElement.clientHeight), parseInt(document.body.clientHeight)].filter(function (x) { return x <= parseInt(window.innerHeight); }).sort(function (a, b) { return a > b ? -1 : (a == b ? 0 : 1); })[0]; // 找出最大值且小于等于 window 的高度 if (!clientHeight) { // 网页缩放导致可能数组为空([0] 为 undefined) clientHeight = parseInt(window.innerHeight); } var clientWidth = [parseInt(document.documentElement.clientWidth), parseInt(document.body.clientWidth)].filter(function (x) { return x <= parseInt(window.innerWidth); }).sort(function (a, b) { return a > b ? -1 : (a == b ? 0 : 1); })[0]; // 找出最大值且小于等于 window 的宽度 if (!clientWidth) { // 网页缩放导致可能数组为空([0] 为 undefined) clientWidth = parseInt(window.innerWidth); } // 设置新的位置 var iconNewTop = -1; if (parseInt(icon.style.top) < scrollTop) { // 面板在滚动条顶部可见部分之上(隐藏了部分或全部) log('Y adjust top'); iconNewTop = scrollTop; // 设置为滚动条顶部可见部分位置 } else if (parseInt(icon.style.top) + panelHeight > scrollTop + clientHeight) { // 面板在滚动条滚到最底部时之下(隐藏了部分或全部) log('Y adjust bottom'); iconNewTop = parseInt(scrollTop + clientHeight - panelHeight); // 设置面板底部不超过滚动条滚到最底部时可见部分位置 if (iconNewTop < scrollTop) { // 如果此时又出现:面板在滚动条顶部可见部分之上(隐藏了部分或全部) log('Y adjust bottom top'); iconNewTop = scrollTop; // 设置为滚动条顶部可见部分位置 } } if (iconNewTop != -1 && Math.abs(iconNewTop - parseInt(icon.style.top)) <= panelHeight) { log('Y set iconNewTop', iconNewTop); icon.style.top = iconNewTop + 'px'; } var iconNewLeft = -1; if (parseInt(icon.style.left) < scrollLeft) { log('X adjust left'); iconNewLeft = scrollLeft; } else if (parseInt(icon.style.left) + panelWidth > scrollLeft + clientWidth) { log('X adjust right'); iconNewLeft = parseInt(scrollLeft + clientWidth - panelWidth); if (iconNewLeft < scrollLeft) { log('X adjust right left'); iconNewLeft = scrollLeft; } } if (iconNewLeft != -1 && Math.abs(iconNewLeft - parseInt(icon.style.left)) <= panelWidth) { log('X set iconNewLeft', iconNewLeft); icon.style.left = iconNewLeft + 'px'; } content.scrollTop = 0; // 翻译面板滚动到顶端 content.scrollLeft = 0; // 翻译面板滚动到左端 content.style.display = 'block'; } /**内容面板填充数据*/ function showContent() { // 填充已有结果集引擎内容 idsType.forEach(function (id) { if (engineResult[id] && !(id in idsExtension.lowerCaseMap)) { // 跳过小写的内容填充 var engine = contentList.querySelector('tr-engine[data-id="' + id + '"]'); if (engine) { engine.appendChild(engineResult[id]); engine.removeAttribute('data-id'); engine.style.display = 'block'; } } }); // 比较大小写内容 for (var id in idsExtension.lowerCaseMap) { if (engineResult[id] && engineResult[idsExtension.lowerCaseMap[id]] && engineResult[id].innerHTML != engineResult[idsExtension.lowerCaseMap[id]].innerHTML && engineResult[id].innerHTML.toLowerCase() != engineResult[idsExtension.lowerCaseMap[id]].innerHTML.toLowerCase()) { var engine = contentList.querySelector('tr-engine[data-id="' + id + '"]'); if (engine) { engine.appendChild(engineResult[id]); engine.removeAttribute('data-id'); engine.style.display = 'block'; } } } } /**隐藏翻译引擎指示器*/ function engineActivateHide() { icon.querySelectorAll('img[activate]').forEach(function (ele) { ele.removeAttribute('activate'); }); } /**显示翻译引擎指示器*/ function engineActivateShow() { engineActivateHide(); icon.querySelector('img[icon-id="' + engineId + '"]').setAttribute('activate', 'activate'); } /**显示 icon*/ function showIcon(e) { log('showIcon event:', e); var offsetX = 4; // 横坐标翻译图标偏移 var offsetY = 8; // 纵坐标翻译图标偏移 // 更新翻译图标 X、Y 坐标 if (e.pageX && e.pageY) { // 鼠标 log('mouse pageX/Y'); pageX = e.pageX; pageY = e.pageY; } if (e.changedTouches) { // 触屏 if (e.changedTouches.length > 0) { // 多点触控选取第 1 个 log('touch pageX/Y'); pageX = e.changedTouches[0].pageX; pageY = e.changedTouches[0].pageY; // 触屏修改翻译图标偏移(Android、iOS 选中后的动作菜单一般在当前文字顶部,翻译图标则放到底部) offsetX = -26; // 单个翻译图标块宽度 offsetY = 16 * 3; // 一般字体高度的 3 倍,距离系统自带动作菜单、选择光标太近会导致无法点按 } } log('selected:' + selected + ', pageX:' + pageX + ', pageY:' + pageY) if (e.target == icon || (e.target.parentNode && e.target.parentNode == icon)) { // 点击了翻译图标 e.preventDefault(); return; } selected = window.getSelection().toString().trim(); // 当前选中文本 log('selected:' + selected + ', icon display:' + icon.style.display); if (selected && icon.style.display !== 'block' && /[\u30a1-\u30f6\u3041-\u3093\uFF00-\uFFFF\u4e00-\u9fa5]/.test(selected) && pageX && pageY) { // 显示翻译图标 log('show icon'); icon.style.top = pageY + offsetY + 'px'; icon.style.left = pageX + offsetX + 'px'; icon.style.display = 'block'; // 兼容部分 Content Security Policy icon.style.position = 'absolute'; icon.style.zIndex = zIndex; } else if (!selected) { // 隐藏翻译图标 log('hide icon'); hideIcon(); } } /**隐藏 icon*/ function hideIcon() { icon.style.display = 'none'; icon.removeAttribute('activate'); // 标注面板关闭 content.style.display = 'none'; engineId = ''; engineTriggerTime = 0; pageX = 0; pageY = 0; engineActivateHide(); audioCache = {}; engineResult = {}; forceStopDrag(); } /**发音*/ function play(obj) { if (obj.url in audioCache) { // 查找缓存 log('audio in cache', obj, audioCache); playArrayBuffer(audioCache[obj.url]); // 播放 } else { ajax(obj.url, function (rst, res) { audioCache[obj.url] = res.response; // 放入缓存 playArrayBuffer(audioCache[obj.url]); // 播放 }, function (rst) { log(rst); }, { responseType: 'arraybuffer' }); } } /**播放 ArrayBuffer 音频*/ function playArrayBuffer(arrayBuffer) { var context = new iframeWin.AudioContext() || new iframeWin.webkitAudioContext(); context.decodeAudioData(arrayBuffer.slice(0), function (audioBuffer) { // `slice(0)`克隆一份(`decodeAudioData`后原数组清空) var bufferSource = context.createBufferSource(); bufferSource.buffer = audioBuffer; bufferSource.connect(context.destination); bufferSource.start(); }); } /**得到发音按钮*/ function getPlayButton(obj) { var type = document.createElement('a'); type.innerHTML = obj.name; type.setAttribute('href', 'javascript:void(0)'); type.setAttribute('class', 'audio-button'); type.setAttribute('title', '点击发音'); type.addEventListener('mouseup', function () { play(obj); }); return type; } /**沪江小D排版*/ function parseHjenglish(rst) { var dom = document.createElement('div'); dom.setAttribute('class', ids.HJENGLISH); var parser = new DOMParser(), doc = parser.parseFromString(rst, 'text/html'), content = doc.getElementsByClassName('word-details')[0]; dom.appendChild(content); /*try { var doc = htmlToDom(cleanHtml(rst)); var label = doc.querySelector('.word-details-item-content header'); var entry = doc.querySelector('.word-text h2'); 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 (collins) { if (label) { var regex = /(《.*?》)/ig; var match = regex.exec(label.innerHTML); if (match && match[1]) { dom.appendChild(htmlToDom('
' + match[1] + '
')); } } dom.appendChild(collins); } } } catch (error) { log(error); dom.appendChild(htmlToDom(error)); }*/ return dom; } /** * 谷歌翻译 token 计算 * https://github.com/hujingshuang/MTrans * */ function token(a) { var k = ""; var b = 406644; var b1 = 3293161072; var jd = "."; var sb = "+-a^+6"; var Zb = "+-3^+b+-f"; for (var e = [], f = 0, g = 0; g < a.length; g++) { var m = a.charCodeAt(g); 128 > m ? e[f++] = m : (2048 > m ? e[f++] = m >> 6 | 192 : (55296 == (m & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (m = 65536 + ((m & 1023) << 10) + (a.charCodeAt(++g) & 1023), e[f++] = m >> 18 | 240, e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224, e[f++] = m >> 6 & 63 | 128), e[f++] = m & 63 | 128) } a = b; for (f = 0; f < e.length; f++) a += e[f], a = RL(a, sb); a = RL(a, Zb); a ^= b1 || 0; 0 > a && (a = (a & 2147483647) + 2147483648); a %= 1E6; return a.toString() + jd + (a ^ b) }; function RL(a, b) { var t = "a"; var Yb = "+"; for (var c = 0; c < b.length - 2; c += 3) { var d = b.charAt(c + 2), d = d >= t ? d.charCodeAt(0) - 87 : Number(d), d = b.charAt(c + 1) == Yb ? a >>> d : a << d; a = b.charAt(c) == Yb ? a + d & 4294967295 : a ^ d } return a } })();