// ==UserScript== // @name MyTranslation // @namespace http://tampermonkey.net/ // @version 0.1 // @license MIT // @description choose text and translate // @author yu // @match *://*/* // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== // @grant unsafeWindow // @grant GM_addStyle // @grant GM_getResourceText // @grant GM_registerMenuCommand // @grant GM_unregisterMenuCommand // @grant GM_notification // @grant GM_getValue // @grant GM_setValue // @grant GM_xmlhttpRequest // @resource css http://at.alicdn.com/t/c/font_3753571_e8ofducbfmu.css // @downloadURL none // ==/UserScript== (function () { "use strict"; //翻译信息 let from = "auto"; let to = "zh"; let salt = "1435660288742342"; let locat = 10; //菜单 let id1=GM_registerMenuCommand ("设置appid", function(){ var newAppid = prompt("请输入appid", ""); if(newAppid != ""){ // appid = newAppid; GM_setValue("appid",newAppid); } }); let id2=GM_registerMenuCommand ("设置secret", function(){ var newSecret = prompt("请输入secret", ""); if(newSecret != ""){ // secret = newSecret; GM_setValue("secret",newSecret); } }); //引入图标css GM_addStyle(GM_getResourceText("css")) //翻译框 let show = document.createElement("div"); show.style.cssText = "position:absolute;background-color:black;color: white;display:none;padding:10px;font-size: 10px;z-index: 10"; show.className = "showplace"; show.id = "show"; document.body.appendChild(show); //翻译按钮 let button = document.createElement('i'); // button.style.fontFamily='iconfont' button.className ="iconfont icon-weibiaoti556"; button.style.cssText = "position:absolute;font-size: 30px;display:none;cursor: pointer;z-index: 10;"; document.body.appendChild(button); //选中文字 let text =''; let x; let y; let flag = 0; //选中事件 document.addEventListener("mouseup", function (e) { x = e.pageX; y = e.pageY+locat; let sel =window.getSelection().toString(); if ( sel!= "" && sel.charCodeAt() != 10) { // console.log(window.getSelection().toString().charCodeAt()); text = sel; button.style.display = "block"; button.style.top = y + "px"; button.style.left = x + "px"; // p.innerText = "当前坐标:"+ "x:"+x +"y:"+y; } }); //消失 document.addEventListener("mousedown", function (e) { show.style.display = "none"; if(flag != 1&&button.style.display != "none"){ button.style.display = "none"; } }); button.addEventListener("mouseover",function(e){ flag =1; }) button.addEventListener("mouseout",function(e){ flag =0; }) //翻译按钮按下事件 button.addEventListener('click',function(e){ button.style.display = "none"; show.style.top = y-locat + "px"; show.style.left = x + "px"; translate(); }) //翻译调用 function translate(){ let appid = GM_getValue("appid"); let secret = GM_getValue("secret"); let str = appid + text + salt + secret; let sign = MD5(str); let url = 'http://api.fanyi.baidu.com/api/trans/vip/translate?q='+text+'&appid='+appid+'&salt=1435660288742342&from=auto&to=zh&'+'sign='+sign // console.log(url); GM_xmlhttpRequest({ url:url , type: 'get', dataType: 'jsonp', onload: function (data) { // console.log(data); let resText = JSON.parse(data.responseText); // console.log(resText); if(resText.error_code == null){ let res = resText.trans_result[0].dst; show.innerText = res; show.style.display = "block"; let h = show.clientHeight; // console.log(h) show.style.borderRadius = h+'px' show.style.borderTopLeftRadius = 0 }else{ show.innerText = '无法翻译选中的文字,请重新选择'; show.style.display = "block"; let h = show.clientHeight; // console.log(h) show.style.borderRadius = h+'px' show.style.borderTopLeftRadius = 0 } }, }); } //MD% var MD5 = function (string) { function RotateLeft(lValue, iShiftBits) { return (lValue<>>(32-iShiftBits)); } function AddUnsigned(lX,lY) { var lX4,lY4,lX8,lY8,lResult; lX8 = (lX & 0x80000000); lY8 = (lY & 0x80000000); lX4 = (lX & 0x40000000); lY4 = (lY & 0x40000000); lResult = (lX & 0x3FFFFFFF)+(lY & 0x3FFFFFFF); if (lX4 & lY4) { return (lResult ^ 0x80000000 ^ lX8 ^ lY8); } if (lX4 | lY4) { if (lResult & 0x40000000) { return (lResult ^ 0xC0000000 ^ lX8 ^ lY8); } else { return (lResult ^ 0x40000000 ^ lX8 ^ lY8); } } else { return (lResult ^ lX8 ^ lY8); } } function F(x,y,z) { return (x & y) | ((~x) & z); } function G(x,y,z) { return (x & z) | (y & (~z)); } function H(x,y,z) { return (x ^ y ^ z); } function I(x,y,z) { return (y ^ (x | (~z))); } function FF(a,b,c,d,x,s,ac) { a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac)); return AddUnsigned(RotateLeft(a, s), b); }; function GG(a,b,c,d,x,s,ac) { a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac)); return AddUnsigned(RotateLeft(a, s), b); }; function HH(a,b,c,d,x,s,ac) { a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac)); return AddUnsigned(RotateLeft(a, s), b); }; function II(a,b,c,d,x,s,ac) { a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac)); return AddUnsigned(RotateLeft(a, s), b); }; function ConvertToWordArray(string) { var lWordCount; var lMessageLength = string.length; var lNumberOfWords_temp1=lMessageLength + 8; var lNumberOfWords_temp2=(lNumberOfWords_temp1-(lNumberOfWords_temp1 % 64))/64; var lNumberOfWords = (lNumberOfWords_temp2+1)*16; var lWordArray=Array(lNumberOfWords-1); var lBytePosition = 0; var lByteCount = 0; while ( lByteCount < lMessageLength ) { lWordCount = (lByteCount-(lByteCount % 4))/4; lBytePosition = (lByteCount % 4)*8; lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount)<>>29; return lWordArray; }; function WordToHex(lValue) { var WordToHexValue="",WordToHexValue_temp="",lByte,lCount; for (lCount = 0;lCount<=3;lCount++) { lByte = (lValue>>>(lCount*8)) & 255; WordToHexValue_temp = "0" + lByte.toString(16); WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length-2,2); } return WordToHexValue; }; function Utf8Encode(string) { string = string.replace(/\r\n/g,"\n"); var utftext = ""; for (var n = 0; n < string.length; n++) { var c = string.charCodeAt(n); if (c < 128) { utftext += String.fromCharCode(c); } else if((c > 127) && (c < 2048)) { utftext += String.fromCharCode((c >> 6) | 192); utftext += String.fromCharCode((c & 63) | 128); } else { utftext += String.fromCharCode((c >> 12) | 224); utftext += String.fromCharCode(((c >> 6) & 63) | 128); utftext += String.fromCharCode((c & 63) | 128); } } return utftext; }; var x=Array(); var k,AA,BB,CC,DD,a,b,c,d; var S11=7, S12=12, S13=17, S14=22; var S21=5, S22=9 , S23=14, S24=20; var S31=4, S32=11, S33=16, S34=23; var S41=6, S42=10, S43=15, S44=21; string = Utf8Encode(string); x = ConvertToWordArray(string); a = 0x67452301; b = 0xEFCDAB89; c = 0x98BADCFE; d = 0x10325476; for (k=0;k