Guest User

Google-Translate

a guest
Jun 24th, 2017
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name Google-Translate
  3. // @author Lex1
  4. // @version 1.7.9
  5. // @mod proxyfix (june 2017) — webproxy.stealthy.co
  6. // @description Google translate. Use opera button with similar code: "javascript:ujs_google_translate('auto|ru')" or hotkey Alt+Shift+T.
  7. // ==/UserScript==
  8.  
  9.  
  10. if(window.location.hostname == 'webproxy.stealthy.co' || window.location.hostname == 'm.translate.ru')(function(){
  11.     var h = window.location.href;
  12.     if(h.indexOf('&ujs=gtt') == h.length-8){
  13.         if(window.opera){
  14.             window.opera.addEventListener('BeforeExternalScript', function(e){e.preventDefault()}, false);
  15.             window.opera.addEventListener('BeforeScript', function(e){e.preventDefault()}, false);
  16.             window.opera.addEventListener('BeforeEventListener.load', function(e){e.preventDefault()}, false);
  17.         };
  18.         var s = document.createElement('style');
  19.         s.setAttribute('type', 'text/css');
  20.         s.appendChild(document.createTextNode('img,object,embed{display:none !important}'));
  21.         document.getElementsByTagName('head')[0].appendChild(s);
  22.     }
  23. })();
  24.  
  25. document.addEventListener('mouseup', function(e){
  26.     if(e && e.button == 0){
  27.         var lc = window.navigator.lastClick || (window.navigator.lastClick = {});
  28.         lc.X = e.clientX;
  29.         lc.Y = e.clientY;
  30.         lc.element = e.target;
  31.     }
  32. }, false);
  33.  
  34. document.addEventListener('focus', function(e){
  35.     if(e){
  36.         var target = e.target, tag = target.nodeName.toLowerCase();
  37.         if(tag == 'textarea' || (tag == 'input' && (target.type == 'text' || target.type == 'search'))){
  38.             (window.navigator.lastClick || (window.navigator.lastClick = {})).textArea = target;
  39.         }
  40.     }
  41. }, true);
  42.  
  43. window.navigator.ujs_createWindow = function(text, status, title, id, pos, size){
  44.     var win = window, doc = win.document, wId = 'ujs_window'+(id || ''), w = doc.getElementById(wId);
  45.     var keyDown = function(e){if(!e.shiftKey && !e.ctrlKey && !e.altKey && e.keyCode == 27)doc.getElementById(wId).closeWin()};
  46.     if(w)w.closeWin();
  47.     w = doc.createElement('div');
  48.     w.setAttribute('style', 'position:fixed;display:block;visibility:hidden;left:0;top:0;width:auto;height:auto;border:1px solid gray;padding:3px;margin:0;z-index:99999;overflow:hidden;cursor:move;'+(typeof w.style.borderRadius === 'string' ? 'background-color:#f3f5f7;padding-top:4px;border-radius:4px;box-shadow:0 0 12px rgba(0,0,0,.4);' : 'background:-o-skin("Window Skin");'));
  49.     w.id = wId;
  50.     w.closeWin = function(){
  51.         doc.removeEventListener('keydown', keyDown, false);
  52.         this.parentNode.removeChild(this);
  53.     };
  54.     w.addEle = function(str, style){
  55.         var ele = doc.createElement('div');
  56.         ele.setAttribute('style', style);
  57.         if(str){
  58.             ele.innerHTML = str;
  59.             for(var el, all = ele.getElementsByTagName('*'), i = all.length; i--;){
  60.                 el = all[i];
  61.                 if(/^(script|frame|iframe|applet|embed|object)$/i.test(el.nodeName)){
  62.                     el.parentNode.removeChild(el);
  63.                 }
  64.                 else{
  65.                     for(var att = el.attributes, j = att.length; j--;){
  66.                         if(/^on[a-z]+$/i.test(att[j].name))att[j].value = '';
  67.                     }
  68.                 }
  69.             }
  70.         };
  71.         return this.appendChild(ele);
  72.     };
  73.     var img = doc.createElement('div');
  74.     img.setAttribute('style', 'display:block;float:right;width:18px;height:18px;padding:0;margin:0;border:none;cursor:pointer;background-image:url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAQAAAD8x0bcAAAAZElEQVR42mNgGNyAGQixsVGU/AdCXjCbHczmxKaMFywlAlWiw6DAwIZNmQhYGqJEDWoqFiAGVmKKTwk73CRJ/Ep0GIyhbsPpO4hbIG4TwKaMk0EZ7hYBBk1cvmNDEmbDrmSwAADE8h10+qICXwAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAAASUVORK5CYII=");background:-o-skin("Caption Close Button Skin");');
  75.     img.title = (win.navigator.language.indexOf('ru') == 0) ? '\u0417\u0430\u043A\u0440\u044B\u0442\u044C' : 'Close';
  76.     img.addEventListener('click', function(){this.parentNode.closeWin()}, false);
  77.     w.appendChild(img);
  78.     w.addEle(title, 'display:table;color:#000;font:16px Times New Roman;width:auto;height:auto;padding:0;margin:0 2px;cursor:text;');
  79.     var cnt = w.addEle(text, 'display:block;border:1px solid #aaa;margin:2px 0 1px 0;padding:4px;background-color:#fafcfe;color:#000;font:14px Times New Roman;width:240px;height:120px;overflow:auto;cursor:text;');
  80.     w.addEle(status, 'display:table;color:#555;font:10px Times New Roman;width:auto;height:auto;padding:0;margin:0 2px;cursor:text;');
  81.     w.addEventListener('mousedown', function(e){
  82.         if(e.target == w){
  83.             e.preventDefault();
  84.             var grabX = e.clientX, grabY = e.clientY, origX = parseInt(w.style.left), origY = parseInt(w.style.top);
  85.             var mouseMove = function(ev){
  86.                 w.style.left = origX+ev.clientX-grabX+'px';
  87.                 w.style.top = origY+ev.clientY-grabY+'px';
  88.             };
  89.             doc.addEventListener('mousemove', mouseMove, false);
  90.             doc.addEventListener('mouseup', function(){doc.removeEventListener('mousemove', mouseMove, false)}, false);
  91.         }
  92.     }, false);
  93.     doc.documentElement.appendChild(w);
  94.  
  95.     if(size){
  96.         cnt.style.height = size.height;
  97.         cnt.style.width = size.width;
  98.     }
  99.     else{
  100.         for(var i = 3; i < 10; i++){
  101.             if(cnt.scrollHeight > cnt.offsetHeight || cnt.scrollWidth > cnt.offsetWidth){
  102.                 cnt.style.height = 50*i+'px';
  103.                 cnt.style.width = 100*i+'px';
  104.             }
  105.             else break;
  106.         }
  107.     };
  108.     var docEle = (doc.compatMode == 'CSS1Compat' && win.postMessage) ? doc.documentElement : doc.body;
  109.     var mX = docEle.clientWidth-w.offsetWidth, mY = docEle.clientHeight-w.offsetHeight;
  110.     if(mX < 0){cnt.style.width = parseInt(cnt.style.width)+mX+'px'; mX = 0};
  111.     if(mY < 0){cnt.style.height = parseInt(cnt.style.height)+mY+'px'; mY =0};
  112.     var hW = parseInt(w.offsetWidth/2);
  113.     w.style.left = (pos && pos.X < mX+hW ? (pos.X > hW ? pos.X-hW : 0) : mX)+'px';
  114.     w.style.top = (pos && pos.Y+10 < mY ? pos.Y+10 : mY)+'px';
  115.     w.style.visibility = 'visible';
  116.     doc.addEventListener('keydown', keyDown, false);
  117.     return w;
  118. };
  119.  
  120. window.addEventListener('message', function(e){
  121.     if(e.data == 'google-translate'){
  122.         var result = '', status = '';
  123.         if(window.location.hostname == 'webproxy.stealthy.co'){
  124.             var r = document.getElementById('result_box'), s = document.getElementById('source'), d = document.getElementById('gt-res-dict');
  125.             if(r){
  126.                 var p = r.getElementsByTagName('span');
  127.                 for(var i = 0, n; n = p[i]; i++){
  128.                     n.setAttribute('style', 'background-color:inherit;color:inherit;font-size:inherit;font-family:serif;');
  129.                 };
  130.                 result = r.innerHTML;
  131.             };
  132.             if(s && d && d.getElementsByTagName('ol').length){
  133.                 var a = d.getElementsByTagName('*');
  134.                 for(var i = a.length; i--;){
  135.                     var n = a[i];
  136.                     switch(n.nodeName.toLowerCase()){
  137.                         case 'ol': n.setAttribute('style', 'margin:1px 5px;padding:0;'); break;
  138.                         case 'li': n.setAttribute('style', 'margin:0;padding:1px 5px;font:12px Arial;list-style-type:none;float:left;font-weight:bold;'); break;
  139.                         case 'div': n.setAttribute('style', 'margin:0;padding:0 2px;font:12px Arial;white-space:nowrap;'); break;
  140.                         case 'a':
  141.                         case 'h3': n.parentNode.removeChild(n); break;
  142.                     }
  143.                 };
  144.                 result = '<b><q>'+s.value+'</q></b>'+d.innerHTML;
  145.             };
  146.             var sl = document.getElementById('nc_sl'), dl = document.getElementById('nc_dl'), tl = document.getElementById('nc_tl');
  147.             if(sl && dl && tl){
  148.                 status = ((dl.value || sl.value)+' -\u203A '+tl.value).toUpperCase();
  149.             };
  150.         };
  151.         if(window.location.hostname == 'm.translate.ru'){
  152.             var v = document.getElementsByTagName('div');
  153.             for(var i = 0, n; n = v[i]; i++){
  154.                 if(n.className == 'tblue')status = n.nextSibling.nodeValue.slice(0, -1)+' (PROMT)';
  155.                 if(n.className == 'tres')result = n.innerHTML;
  156.             }
  157.         };
  158.         if(result || status){
  159.             e.source.postMessage('google_translate'+encodeURI(result)+'|'+encodeURI(status)+'|'+encodeURI(window.location.href), '*');
  160.         }
  161.         else{
  162.             alert('Error!\n\n'+(document.body.textContent || document.body.innerText));
  163.         };
  164.     };
  165.     if(e.data && e.data.indexOf('google_translate') == 0){
  166.         var domain = e.origin ? e.origin.replace(/^https?:\/\//i, '') : e.domain;
  167.         if(domain == 'webproxy.stealthy.co' || domain == 'm.translate.ru'){
  168.             var msg = e.data.slice(16).split('|');
  169.             window.navigator.ujs_createWindow(decodeURI(msg[0]), decodeURI(msg[1]), '<a href="'+decodeURI(msg[2]).replace(/&/g, '&amp;')+'" target="_blank" style="display:inline;padding:0;margin:0;text-decoration:none;border:none;color:#009;font:16px Times New Roman;">Google Translate</a>', '_gt', window.navigator.lastClick);
  170.             var f = document.getElementById('ujs_googletranslateframe');
  171.             if(f)f.parentNode.removeChild(f);
  172.         }
  173.     }
  174. }, false);
  175.  
  176. function ujs_google_translate(dir){
  177.     var selText = function(w){var t; return w ? w.document.getSelection().toString() || (t = w.navigator.lastClick && w.navigator.lastClick.textArea) && t.value.substring(t.selectionStart, t.selectionEnd) : ''};
  178.     var selWin = function(w){if(selText(w))return w; for(var i = 0, f, r; f = w.frames[i]; i++){try{if(r = arguments.callee(f))return r}catch(e){}}};
  179.     var winWait = function(w, lng){w.navigator.ujs_createWindow('', (lng == 'ru' ? '\u041F\u0435\u0440\u0435\u0432\u043E\u0434\u0438\u043C' : 'Translating')+'\u2026', 'Google Translate', '_gt', w.navigator.lastClick)};
  180.     var createFrame = function(w, src){
  181.         var fId = 'ujs_googletranslateframe';
  182.         var f = w.document.getElementById(fId);
  183.         if(f)f.parentNode.removeChild(f);
  184.         f = w.document.createElement('iframe');
  185.         f.width = 0;
  186.         f.height = 0;
  187.         f.frameBorder = 'no';
  188.         f.scrolling = 'no';
  189.         f.id = fId;
  190.         f.name = fId;
  191.         if(src)f.src = src;
  192.         f.setAttribute('style', 'position:fixed;left:0;top:0;visibility:hidden;width:0;height:0;');
  193.         f.addEventListener('load', function(){(window.postMessage ? f.contentWindow : f.contentDocument).postMessage('google-translate', '*')}, false);
  194.         w.document.documentElement.appendChild(f);
  195.     };
  196.     var createForm = function(w, action, name, value){
  197.         var f = w.document.createElement('form');
  198.         f.action = action;
  199.         f.method = 'POST';
  200.         f.acceptCharset = 'UTF-8';
  201.         f.target = 'ujs_googletranslateframe';
  202.         f.style.display = 'none';
  203.         var t = w.document.createElement('textarea');
  204.         t.name = name;
  205.         t.value = value;
  206.         f.appendChild(t);
  207.         w.document.documentElement.appendChild(f);
  208.         f.submit();
  209.         f.parentNode.removeChild(f);
  210.     };
  211.     var w = selWin(window);
  212.     var txt = selText(w);
  213.     var encTxt = encodeURIComponent(txt);
  214.     var lng = window.navigator.language.slice(0, 2);
  215.     var url = escape(window.location.href);
  216.     if(dir.indexOf('|') != -1){
  217.         if(w && w.location.hostname != 'translate.google.com'){
  218.             winWait(w, lng);
  219.             if(encTxt.length < 1800){
  220.                 createFrame(w, 'http://webproxy.stealthy.co/browse.php?u='+encodeURIComponent('https://translate.google.com/translate_t?text='+encTxt+'&hl='+lng+'&langpair='+dir+'&eotf=0&tbb=1')+'&ujs=gtt&b=52');
  221.             }
  222.             else{
  223.                 var l = dir.split('|');
  224.                 createFrame(w, '');
  225.                 createForm(w, 'http://webproxy.stealthy.co/browse.php?u='+encodeURIComponent('https://translate.google.com/?sl='+l[0]+'&tl='+l[1]+'&hl='+lng+'&eotf=0')+'&ujs=gtt&b=52', 'text', txt);
  226.             }
  227.         }
  228.         else{
  229.             window.open('http://translate.google.com/translate?u='+url+'&hl='+lng+'&langpair='+dir+'&tbb=1'+(document.charset ? '&ie='+document.charset : ''));
  230.         }
  231.     }
  232.     else{
  233.         if(w && w.location.hostname != 'm.translate.ru'){
  234.             winWait(w, lng);
  235.             createFrame(w, 'http://m.translate.ru/translator/result/?usev2=1&text='+encodeURIComponent(txt.slice(0, 600))+'&dirCode='+dir+'&ujs=gtt');
  236.         }
  237.         else{
  238.             window.open('http://www.translate.ru/url/tran_url.asp?direction='+dir+'&template=General&autotranslate=true&url='+url);
  239.         }
  240.     }
  241. };
  242.  
  243.  
  244. // Hotkey Alt+Shift+T. Replace 'auto|ru' on 'er' (english to russian) to use translate.ru
  245. document.addEventListener('keydown', function(e){
  246.     if(e.shiftKey && !e.ctrlKey && e.altKey && e.keyCode == 84)ujs_google_translate('auto|ru');
  247. }, false);
Add Comment
Please, Sign In to add comment