// ==UserScript== // @name 智能划词翻译,可替换原文版 // @namespace https://greasyfork.org/zh-CN/scripts/382768 // @version 4.0.1 // @description 划词翻译,翻译后可以替换原来的文字,可朗读翻译结果,适应移动端 // @grant unsafeWindow // @require http://cdn.bootcss.com/jquery/3.3.1/jquery.min.js // @require https://cdn.bootcss.com/rangy/1.3.0/rangy-core.min.js // @author 辣条要甜点 // @match http://*/* // @include https://*/* // @run-at document-end // @connect dict.youdao.com // @connect translate.google.cn // @grant GM_xmlhttpRequest // @grant GM_setValue // @grant GM_getValue // @downloadURL https://update.greasyfork.icu/scripts/382768/%E6%99%BA%E8%83%BD%E5%88%92%E8%AF%8D%E7%BF%BB%E8%AF%91%EF%BC%8C%E5%8F%AF%E6%9B%BF%E6%8D%A2%E5%8E%9F%E6%96%87%E7%89%88.user.js // @updateURL https://update.greasyfork.icu/scripts/382768/%E6%99%BA%E8%83%BD%E5%88%92%E8%AF%8D%E7%BF%BB%E8%AF%91%EF%BC%8C%E5%8F%AF%E6%9B%BF%E6%8D%A2%E5%8E%9F%E6%96%87%E7%89%88.meta.js // ==/UserScript== (function () { 'use strict'; rangy.init(); var lang = $('html').attr('lang'); var isCnWeb = Boolean(lang) && lang.trim().toLowerCase() == 'zh-cn'; console.log(isCnWeb); var audioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.msAudioContext; var youdaoUrl = 'http://dict.youdao.com/jsonapi?xmlVersion=5.1&jsonversion=2&q='; var googleUrl = 'https://translate.google.cn/translate_a/single?client=gtx&dt=t&dt=bd&dj=1&source=input&hl=zh-CN&sl=auto&tl='; var iconSize = 24; var translationTestSize = 16; var icon = document.createElement('div'); var style = '' + 'width:24px!important;' + 'height:24px!important;' + 'margin:4px!important;' + 'position:absolute!important;'; var iconStyle = '' + 'width:32px!important;' + 'height:32px!important;' + 'display:none!important;' + 'background:#fff!important;' + 'border-radius:16px!important;' + 'box-shadow:4px 4px 8px #888!important;' + 'position:absolute!important;' + 'z-index:2147483647!important;' + ''; icon.innerHTML = '' + '' + '' + ''; icon.setAttribute('style', iconStyle); var container = document.createElement('div'); container.setAttribute('style', '' + 'display:none!important;' + 'position:absolute!important;' + 'font-size:13px!important;' + 'overflow:auto!important;' + 'background:#fff!important;' + 'font-family:sans-serif,Arial!important;' + 'font-weight:normal!important;' + 'text-align:left!important;' + 'color:#000!important;' + 'padding:0.5em 1em!important;' + 'line-height:1.5em!important;' + 'border-radius:5px!important;' + 'border:1px solid #ccc!important;' + 'box-shadow:4px 4px 8px #888!important;' + 'max-width:350px!important;' + 'min-width:134px!important;' + 'max-height:216px!important;' + 'z-index:2147483647!important;' + ''); var selectText,fyDom,range,isL,isR,_t,selectText2='',isCn,isPlay=false,wavs,_e; // 添加翻译图标和面板到 DOM document.documentElement.appendChild(icon); var containerBox = document.createElement('div'); containerBox.setAttribute('style','position:relative;'); containerBox.appendChild(container); document.documentElement.appendChild(containerBox); // 鼠标事件:防止选中的文本消失 icon.addEventListener('mousedown', function (e) { e.preventDefault(); }); // 鼠标事件and触摸:防止选中的文本消失;显示、隐藏翻译图标 icon.addEventListener('mouseup', function (e) { e.stopPropagation(); }); container.addEventListener('mouseup', function (e) { e.stopPropagation(); }); container.addEventListener('touchend', function (e) { e.stopPropagation(); }); document.addEventListener('touchend', function (e) { if (container.style.display != 'none') { container.style.display = 'none'; } }); $(document).keydown(function (event){ if (event.ctrlKey && event.shiftKey && event.keyCode == 81) { GM_setValue('isKey', !GM_getValue('isKey', false)); return; } if(event.ctrlKey && event.keyCode == 81 && GM_getValue('isKey', false)){ showContainer(_e); return; } }); document.addEventListener('mouseup', function (e) { if (container.style.display != 'none') { container.style.display = 'none'; } _e = e; range = getFirstRange(); selectText = range ? rangy.getSelection().toString().trim() : ''; // console.log(selectText,icon.style.display); if(selectText == '' || selectText == selectText2){ selectText2 = ''; icon.style.display = 'none'; container.style.display = 'none'; return; } if(GM_getValue('isKey', false))return; icon.style.top = (e.pageY + 12) + 'px'; icon.style.left = e.pageX + 'px'; icon.style.display = 'block'; selectText2 = selectText; }); // 选中变化事件:当点击已经选中的文本的时候,隐藏翻译图标(此时浏览器动作是:选中的文本已经取消选中了) document.addEventListener("selectionchange", function () { if (container.style.display != 'none') { icon.style.display = 'none'; } }); //如果是手机,点击复制触发翻译 if (isMobile()) { document.oncopy = function (e) { container.style.top = ($(e.target).offset().top + $(e.target).height()) + 'px'; container.style.left = 0; container.style.right = 0; container.style.margin = 'auto'; container.style.minWidth = (e.target.clientWidth - 8) + 'px'; container.style.maxWidth = (document.body.clientWidth - 8) + 'px'; container.style.width = (e.target.clientWidth - 8) + 'px'; range = getFirstRange(); selectText = range ? rangy.getSelection().toString() : ''; icon.click(); }; } //点击了朗读按钮 $(container).on('click', '#_fy_play', function (e) { playSound(selectText, 'en'); e.stopPropagation(); }); //点击了替换图标 $(container).on('click', '#_fy_th', function (e) { if (range) { //删除选中的节点 range.deleteContents(); var startNode, endNode, i=0; while (fyDom.childNodes.length !== 0) { i++; var _node = fyDom.childNodes[fyDom.childNodes.length - 1]; //插入翻译的节点 range.insertNode(_node); if (i === 1) endNode = _node; if (fyDom.childNodes.length === 0) startNode = _node; } rangy.getSelection().setSingleRange(range); //处理重复的node,必须在添加完所有node后处理,不然节点位置会乱 if (startNode) disposeRepeatDom(startNode, true); if (endNode) disposeRepeatDom(endNode, false); } icon.style.display = 'none'; container.style.display = 'none'; }); // 翻译图标点击事件 icon.addEventListener('click', function (e) { showContainer(e); }); function showContainer(e){ if (range) { wavs = []; var html = rangy.getSelection().toHtml(); console.log(); fyDom = document.createElement("div"); fyDom.innerHTML = html; //自定义翻译的文本格式,谷歌翻译不会把|翻译,所以找到所有文本节点的text,组成"选择的dom下标-选择的dom下标>>text||||" _t = ''; getDomText(fyDom, ''); isL = (html.substr(0, 1) == "<"); isR = (html.substr(-1) == ">"); icon.style.display = 'none'; container.style.display = 'none'; container.innerHTML = ''; if (!isMobile()) { container.style.bottom = (document.body.clientHeight-e.pageY) + 'px'; if (e.pageX + 350 <= document.body.clientWidth) {// container 面板css最大宽度为250px container.style.left = e.pageX + 'px'; } else { container.style.left = document.body.clientWidth - 350 + 'px'; } } console.log(isChina(selectText),isCnWeb); if(isChina(selectText) || isCnWeb) { ajax(googleUrl + 'en&q=', encodeURIComponent(selectText), 1, container); } else { if(countOfWord(selectText) == 1) { ajax(youdaoUrl, selectText, 0, container); }else { ajax(googleUrl + 'zh-CN&q=', encodeURIComponent(_t), 1, container); } } } } function countOfWord(str) { var value = String(str); value = value.replace(/^\s+|\s+$/gi, ""); // 前后空格不计算为单词数 value = value.replace(/\s+/gi, " ");// 多个空格替换成一个空格 var length = 0; // 更新计数 var match = value.match(/\s/g);//没有匹配到则返回null if (match) { length = match.length + 1; } else if (value) { length = 1; } return length; } function isChina(str){ var reg = /[\u4E00-\u9FA5]/g var cnStr = str.match(reg); return Boolean(cnStr) && cnStr.length >= (str.length-cnStr.length); } // ajax 跨域访问公共方法 function ajax(url, text, target, element, method, data, headers) { if (!!!method) method = 'GET'; // >>>因为Tampermonkey跨域访问(a.com)时会自动携带对应域名(a.com)的对应cookie // 不会携带当前域名的cookie // 所以,GM_xmlhttpRequest【不存在】cookie跨域访问安全性问题 // 以下设置默认headers不起作用<<< url += text; if (!!!headers) headers = { 'cookie': '' }; GM_xmlhttpRequest({ method: method, url: url, headers: headers, data: data, onload: function (res) { if(target == 0){ youdao(res.responseText, text, element); }else{ google(res.responseText, element); } }, onerror: function (res) { displaycontainer("连接失败",element); } }); } // 有道翻译 function youdao(rst, text, element) { var ec = JSON.parse(rst).ec; if (!!ec) { var word = JSON.parse(rst).ec.word[0], html = '', tr = ''; var trs = word.trs, ukphone = word.ukphone, usphone = word.usphone, phone = word.phone; var phoneStyle = '' + 'color:#9E9E9E!important;' + ''; if (!!ukphone && ukphone.length != 0) { html += '英[' + ukphone + '] '; } if (!!usphone && usphone.length != 0) { html += '美[' + usphone + '] '; } html+='' if (html.length != 0) { html += '
'; } else if (!!phone && phone.length != 0) { html += '[' + phone + ']
'; } trs.forEach(element => { tr += element.tr[0].l.i[0] + '
'; }); html += tr; displaycontainer(html, element); }else { ajax(googleUrl +'zh-CN&q=', text, 1, element); } } // 谷歌翻译 function google(rst, element) { var json = JSON.parse(rst), html = ''; for (var i = 0; i < json.sentences.length; i++) { html += json.sentences[i].trans.trim(); } if (!isCn) { //解析格式 var arr = html.split("\\"); if(html.charAt(html.length - 1) == "\\")arr.pop(); for (var i in arr){ if(!arr.hasOwnProperty(i))continue; var _arr = arr[i].split('>'); var j = 'fyDom'; var c = ''; if (_arr.length == 1) { j += '.childNodes["0"]'; c = TextEnCode(_arr[0]); } else { for (var x in _arr) { if(!_arr.hasOwnProperty(x))continue; _arr[x] = _arr[x].replace(/\{\/\}/g, '\\'); //第一个是dom数组的下标 if (x == 0) { var __arr = _arr[x].split('/'); for (var _x in __arr){ if(!__arr.hasOwnProperty(_x))continue; j += '.childNodes["'+parseInt(__arr[_x])+'"]'; } } else if (x == 1) { c += TextEnCode(_arr[x]); } else { c += '>'+TextEnCode(_arr[x]); } } } j += '.nodeValue="'+c+'"'; // console.log(j); //为了方便直接组成js代码,执行 eval(j); } html = '
' + htmlEncode($(fyDom).text()) + '
朗读
替换
'; } else { selectText = html; html = '
' + htmlEncode(html) + '
朗读
'; } displaycontainer(html, element); } function displaycontainer(html, element) { $(element).html(html).show(); } //获取range function getFirstRange() { var sel = rangy.getSelection(); return sel.rangeCount ? (sel.getRangeAt(0)).cloneRange() : null; } //处理重复的dom function disposeRepeatDom(node, direction) { var _node = node.cloneNode(true); _node.innerText = ''; var brotherNode = direction ? node.previousElementSibling : node.nextElementSibling; if (brotherNode) { if (brotherNode.innerText.trim() == '') { node.parentNode.removeChild(brotherNode); return; } var _brotherNode = brotherNode.cloneNode(true); _brotherNode.innerText = ''; if (_node.innerHTML == _brotherNode.innerHTML) { //找到textNode后设置翻译的文字+原dom文字 setTextNode(node, brotherNode, direction); node.childNodes.length > brotherNode.childNodes.length ? node.parentNode.removeChild(brotherNode) : node.parentNode.removeChild(node); } } } function setTextNode(dom, brotherDom, direction) { if (dom.hasChildNodes() && brotherDom.hasChildNodes()) { for (var i in dom.childNodes) { var _dom = dom.childNodes[direction?i:dom.childNodes.length-1-i]; var _brotherDom = brotherDom.childNodes[i]; if(_dom.nodeType == 3){ _dom.nodeValue = _brotherDom.nodeValue = (direction ? (_brotherDom.nodeValue + _dom.nodeValue) : (_dom.nodeValue + _brotherDom.nodeValue)); return true; }else{ if (setTextNode(_dom, _brotherDom, direction)) return true; } } } return false; } //获取所有文本节点的内容数据 function getDomText(dom, index) { if (dom.hasChildNodes()) { for (var i = 0, len = dom.childNodes.length; i < len; i++){ var node = dom.childNodes[i]; if (node.nodeType == 3) { if (node.nodeValue.replace(/\s*/g,'')) { _t += index+i+'>'+node.nodeValue.replace(/\\/g, "{/}")+'\\'; } } else { getDomText(node,index+i+'/'); } } } } //转实体字符 function TextEnCode(text) { var str = ""; text = text.trim(); for (var i = 0,len = text.length; i < len; i++) { var temp = text.charCodeAt(i).toString(16); str += "\\u" + new Array(5 - String(temp).length).join("0") + temp; } return str; } function htmlEncode(str) { var s = ""; if (str.length === 0) { return ""; } s = str.replace(/&/g, "&"); s = s.replace(//g, ">"); s = s.replace(/ /g, " "); s = s.replace(/\'/g, "'");//IE下不支持实体名称 s = s.replace(/\"/g, """); return s; } //朗读文字 function playSound(text, tl) { if (isPlay) return; var context = new audioContext(); if (wavs.length <= 0) { var texts = text.split(/\n|\r|\n\r|(\.^0-9)|\,/g); for (var i in texts) { if (!texts[i] || !texts[i].replace(/\s+/g, "")) continue; var url = 'https://translate.google.cn/translate_tts?ie=UTF-8&q=' + encodeURIComponent(texts[i]) + '&tl=' + tl + '&client=gtx&.wav'; wavs.push(new Promise(function (resolve, reject) { GM_xmlhttpRequest({ method: "GET", url: url, responseType: 'arraybuffer', onload: function (res) { try { resolve(res.response); } catch (e) { reject(e); } } }); })); } } Promise.all(wavs).then(play, e => console.log(e)); function play(bufferList) { isPlay = true; var reader = new FileReader(); var blob = new Blob(bufferList, {type: 'application/octet-binary'}); reader.addEventListener("loadend", function() { var buffer = reader.result; context.decodeAudioData(buffer, function (buffer) { var source = context.createBufferSource(); source.buffer = buffer; source.connect(context.destination); source.start(0); }) }); reader.readAsArrayBuffer(blob); isPlay = false; } } function isMobile() { var userAgentInfo = navigator.userAgent; var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"]; var flag = true; for (var v = 0; v < Agents.length; v++) { if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; } } return !flag; } function getElementToPageTop(el) { var h = 0, pT = -1; $(el).parents().map(function () { if (this.offsetTop == pT) return; h += this.offsetTop; pT = h; }); return h; } })();