// ==UserScript== // @name translate // @namespace https://lufei.so // @grant GM_xmlhttpRequest // @description 划词翻译 // @version 1.4.0 // @downloadURL none // ==/UserScript== var sel, panel, audio, query audio = document.createElement('audio') audio.autoplay = true function setStyle(el, o) { var x, arr = [] function toLineThrough(s) { return s.replace(/[A-Z]/g, function(c) { return '-' + c.toLowerCase() }) } for (x in o) { arr.push(toLineThrough(x) + ': ' + o[x]) } el.setAttribute('style', arr.join(';')) } function play(type) { audio.src = 'https://dict.youdao.com/dictvoice?audio=' + window.encodeURIComponent(query) + '&type=' + type } function htmlEntities(s) { var o = { '<': '<', '>': '>', '&': '&', '"': '"', "'": ''' } return s.replace(/[<>&"]/g, function(c) { return o[c] }) } function decode(o) { // variable var us, uk, x // element var header, explains, web, translation if (o.basic) { us = o.basic['us-phonetic'] || '' uk = o.basic['uk-phonetic'] || '' query = o.query || '' header = document.createElement('div') setStyle(header, { borderBottom : '1px dashed #aaa', paddingBottom : '.5rem' }) header.innerHTML = '' + htmlEntities(query) + '' + 'uk:[' + uk + ']' + 'us:[' + us + ']' panel.appendChild(header) header.onclick = function(e) { if (e.target.dataset.type) play(e.target.dataset.type) } if (o.basic.explains) { explains = document.createElement('ul') setStyle(explains, { margin : 0, padding : 0, listStyle : 'none', lineHeight : '1.4rem', fontSize : '.8rem', marginTop : '.5rem' }) explains.innerHTML = '