Advertisement
Guest User

Untitled

a guest
Aug 19th, 2022
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.55 KB | None | 0 0
  1. // ==UserScript==
  2. // @name 4chan Translation Links
  3. // @version 1.2
  4. // @description Translation Plugin for 4chan
  5. // @author some /int/ guy (borrowing from cyg + fsn here and there)
  6. // @include https://boards.4chan.org/*
  7. // @include https://boards.4channel.org/*
  8. // @exclude https://boards.4chan.org/*/catalog
  9. // @exclude https://boards.4channel.org/*/catalog
  10. // @grant window.focus
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. var PPBS = {};
  18.  
  19. PPBS.updatePostMenu = function() {
  20. var menu = $.id("post-menu"), repNode, rcNode, aNode;
  21. if (!menu) {
  22. return;
  23. }
  24. var pid = menu.firstChild.firstChild.getAttribute("data-id");
  25. var post = $.id(`p${pid}`);
  26.  
  27. //google translate option
  28. repNode = $.el("LI");
  29. aNode = $.el("a");
  30. aNode.setAttribute("data-cmd", "ppbs-translate-google");
  31. aNode.setAttribute("data-id", pid);
  32. aNode.textContent = "Translate with Google";
  33. repNode.appendChild(aNode);
  34. menu.firstChild.insertBefore(repNode, $.cls("dd-admin")[0]);
  35.  
  36. //bing translate option
  37. repNode = $.el("LI");
  38. aNode = $.el("a");
  39. aNode.setAttribute("data-cmd", "ppbs-translate-bing");
  40. aNode.setAttribute("data-id", pid);
  41. aNode.textContent = "Translate with Bing";
  42. repNode.appendChild(aNode);
  43. menu.firstChild.insertBefore(repNode, $.cls("dd-admin")[0]);
  44.  
  45. //yandex translate option
  46. repNode = $.el("LI");
  47. aNode = $.el("a");
  48. aNode.setAttribute("data-cmd", "ppbs-translate-yandex");
  49. aNode.setAttribute("data-id", pid);
  50. aNode.textContent = "Translate with Yandex";
  51. repNode.appendChild(aNode);
  52. menu.firstChild.insertBefore(repNode, $.cls("dd-admin")[0]);
  53.  
  54. //DeepL translate option
  55. repNode = $.el("LI");
  56. aNode = $.el("a");
  57. aNode.setAttribute("data-cmd", "ppbs-translate-deepl");
  58. aNode.setAttribute("data-id", pid);
  59. aNode.textContent = "Translate with DeepL";
  60. repNode.appendChild(aNode);
  61. menu.firstChild.insertBefore(repNode, $.cls("dd-admin")[0]);
  62.  
  63. menu.style.zIndex = "10001";
  64. };
  65.  
  66. PPBS.parseMessage = function(messageText) {
  67. messageText = messageText.replace(/<br>/gi, "\n"); // conserve linebreaks
  68. messageText = messageText.replace(/(<([^>]+)>)/gi, ""); // filter out rest of html
  69. messageText = messageText.replace(/&gt;/gi, ">"); // deal with < and >
  70. messageText = messageText.replace(/&lt;/gi, "<");
  71. messageText = encodeURIComponent(messageText); // encode other special characters etc
  72. return messageText;
  73. }
  74.  
  75. PPBS.translateGoogle = function(postID) {
  76. var message = $.id("m" + postID);
  77. var info = $.id("pi" + postID);
  78. if (!message) {
  79. return;
  80. }
  81. var messageText = PPBS.parseMessage(message.innerHTML);
  82. var tUrl = "https://translate.google.com/?hl=en&sl=auto&tl=en&op=translate&text=" + messageText;
  83. window.open(tUrl,"_blank");
  84. };
  85.  
  86. PPBS.translateBing = function(postID) {
  87. var message = $.id("m" + postID);
  88. var info = $.id("pi" + postID);
  89. if (!message) {
  90. return;
  91. }
  92. var messageText = PPBS.parseMessage(message.innerHTML);
  93. var tUrl = "https://www.bing.com/translator/?to=en&text=" + messageText;
  94. window.open(tUrl,"_blank");
  95. };
  96.  
  97. PPBS.translateYandex = function(postID) {
  98. var message = $.id("m" + postID);
  99. var info = $.id("pi" + postID);
  100. if (!message) {
  101. return;
  102. }
  103. var messageText = PPBS.parseMessage(message.innerHTML);
  104. var tUrl = "https://translate.yandex.com/?text=" + messageText;
  105. window.open(tUrl,"_blank");
  106. };
  107.  
  108. PPBS.translateDeepL = function(postID) {
  109. var message = $.id("m" + postID);
  110. var info = $.id("pi" + postID);
  111. if (!message) {
  112. return;
  113. }
  114. var messageText = PPBS.parseMessage(message.innerHTML);
  115. var tUrl = "https://www.deepl.com/translator#auto/en/" + messageText;
  116. window.open(tUrl,"_blank");
  117. };
  118.  
  119. PPBS.onClick = function(e) {
  120. var t = e.target;
  121. switch(t.getAttribute("data-cmd")) {
  122. case "post-menu":
  123. setTimeout(PPBS.updatePostMenu, 10);
  124. break;
  125. case "ppbs-translate-google":
  126. PPBS.translateGoogle(t.getAttribute("data-id"));
  127. break;
  128. case "ppbs-translate-bing":
  129. PPBS.translateBing(t.getAttribute("data-id"));
  130. break;
  131. case "ppbs-translate-yandex":
  132. PPBS.translateYandex(t.getAttribute("data-id"));
  133. break;
  134. case "ppbs-translate-deepl":
  135. PPBS.translateDeepL(t.getAttribute("data-id"));
  136. break;
  137. default: break;
  138. }
  139. };
  140.  
  141. var $={id:function(e){return document.getElementById(e)},cls:function(e,n){return(n||document).getElementsByClassName(e)},byName:function(e){return document.getElementsByName(e)},tag:function(e,n){return(n||document).getElementsByTagName(e)},el:function(e){return document.createElement(e)},qs:function(e,n){return(n||document).querySelector(e)},qsa:function(e,n){return(n||document).querySelectorAll(e)},extend:function(e,n){for(var t in n)e[t]=n[t]},parentByCls:function(e,n){for(var t=$.docEl,o=e;e!==t&&!$.hasClass(e,n);)e=e.parentNode;return o!==e?e:null}};document.documentElement.classList?($.hasClass=function(e,n){return e.classList.contains(n)},$.addClass=function(e,n){e.classList.add(n)},$.removeClass=function(e,n){e.classList.remove(n)}):($.hasClass=function(e,n){return-1!=(" "+e.className+" ").indexOf(" "+n+" ")},$.addClass=function(e,n){e.className=""===e.className?n:e.className+" "+n},$.removeClass=function(e,n){e.className=(" "+e.className+" ").replace(" "+n+" ","")}),$.on=function(e,n,t){e.addEventListener(n,t,!1)},$.off=function(e,n,t){e.removeEventListener(n,t,!1)},$.xhr=function(e,n,t,o,r){var a,u,s;if((u=new XMLHttpRequest).open(e,n,!0),t)for(a in t)u[a]=t[a];if(o)if("string"==typeof o)u.setRequestHeader("Content-type","application/x-www-form-urlencoded");else{for(a in s=new FormData,o)s.append(a,o[a]);o=s}else o=null;return u.withCredentials=r||0,u.send(o),u},$.getItem=function(e){return localStorage.getItem(e)},$.setItem=function(e,n){return localStorage.setItem(e,n)},$.removeItem=function(e){return localStorage.removeItem(e)},$.getCookie=function(e){var n,t,o,r;for(r=e+"=",o=document.cookie.split(";"),n=0;t=o[n];++n){for(;" "==t.charAt(0);)t=t.substring(1,t.length);if(0===t.indexOf(r))return decodeURIComponent(t.substring(r.length,t.length))}return null},$.getToken=function(){return document.body.getAttribute("data-tkn")},$.ago=function(e){var n,t,o,r;return(n=Date.now()/1e3-e)<1?"moments ago":n<60?(0|n)+" seconds ago":n<3600?(t=0|n/60)>1?t+" minutes ago":"one minute ago":n<86400?(o=(t=0|n/3600)>1?t+" hours":"one hour",(r=0|n/60-60*t)>1&&(o+=" and "+r+" minutes"),o+" ago"):(o=(t=0|n/86400)>1?t+" days":"one day",(r=0|n/3600-24*t)>1&&(o+=" and "+r+" hours"),o+" ago")},$.pluralise=function(e,n,t){return 1===e?n||"":t||"s"},$.now=function(){return Math.round((new Date).getTime()/1e3)},$.dispatch=function(e,n){var t=document.createEvent("Event");t.initEvent(e,!1,!1),document.dispatchEvent(t)},$.docEl=document.documentElement;
  142.  
  143. if (/catalog/.test(window.location.href)) {
  144. return;
  145. }
  146.  
  147. $.on(document, "click", PPBS.onClick);
  148. })();
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement