Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name 4chan Translation Links
- // @version 1.2
- // @description Translation Plugin for 4chan
- // @author some /int/ guy (borrowing from cyg + fsn here and there)
- // @include https://boards.4chan.org/*
- // @include https://boards.4channel.org/*
- // @exclude https://boards.4chan.org/*/catalog
- // @exclude https://boards.4channel.org/*/catalog
- // @grant window.focus
- // @run-at document-start
- // ==/UserScript==
- (function() {
- 'use strict';
- var PPBS = {};
- PPBS.updatePostMenu = function() {
- var menu = $.id("post-menu"), repNode, rcNode, aNode;
- if (!menu) {
- return;
- }
- var pid = menu.firstChild.firstChild.getAttribute("data-id");
- var post = $.id(`p${pid}`);
- //google translate option
- repNode = $.el("LI");
- aNode = $.el("a");
- aNode.setAttribute("data-cmd", "ppbs-translate-google");
- aNode.setAttribute("data-id", pid);
- aNode.textContent = "Translate with Google";
- repNode.appendChild(aNode);
- menu.firstChild.insertBefore(repNode, $.cls("dd-admin")[0]);
- //bing translate option
- repNode = $.el("LI");
- aNode = $.el("a");
- aNode.setAttribute("data-cmd", "ppbs-translate-bing");
- aNode.setAttribute("data-id", pid);
- aNode.textContent = "Translate with Bing";
- repNode.appendChild(aNode);
- menu.firstChild.insertBefore(repNode, $.cls("dd-admin")[0]);
- //yandex translate option
- repNode = $.el("LI");
- aNode = $.el("a");
- aNode.setAttribute("data-cmd", "ppbs-translate-yandex");
- aNode.setAttribute("data-id", pid);
- aNode.textContent = "Translate with Yandex";
- repNode.appendChild(aNode);
- menu.firstChild.insertBefore(repNode, $.cls("dd-admin")[0]);
- //DeepL translate option
- repNode = $.el("LI");
- aNode = $.el("a");
- aNode.setAttribute("data-cmd", "ppbs-translate-deepl");
- aNode.setAttribute("data-id", pid);
- aNode.textContent = "Translate with DeepL";
- repNode.appendChild(aNode);
- menu.firstChild.insertBefore(repNode, $.cls("dd-admin")[0]);
- menu.style.zIndex = "10001";
- };
- PPBS.parseMessage = function(messageText) {
- messageText = messageText.replace(/<br>/gi, "\n"); // conserve linebreaks
- messageText = messageText.replace(/(<([^>]+)>)/gi, ""); // filter out rest of html
- messageText = messageText.replace(/>/gi, ">"); // deal with < and >
- messageText = messageText.replace(/</gi, "<");
- messageText = encodeURIComponent(messageText); // encode other special characters etc
- return messageText;
- }
- PPBS.translateGoogle = function(postID) {
- var message = $.id("m" + postID);
- var info = $.id("pi" + postID);
- if (!message) {
- return;
- }
- var messageText = PPBS.parseMessage(message.innerHTML);
- var tUrl = "https://translate.google.com/?hl=en&sl=auto&tl=en&op=translate&text=" + messageText;
- window.open(tUrl,"_blank");
- };
- PPBS.translateBing = function(postID) {
- var message = $.id("m" + postID);
- var info = $.id("pi" + postID);
- if (!message) {
- return;
- }
- var messageText = PPBS.parseMessage(message.innerHTML);
- var tUrl = "https://www.bing.com/translator/?to=en&text=" + messageText;
- window.open(tUrl,"_blank");
- };
- PPBS.translateYandex = function(postID) {
- var message = $.id("m" + postID);
- var info = $.id("pi" + postID);
- if (!message) {
- return;
- }
- var messageText = PPBS.parseMessage(message.innerHTML);
- var tUrl = "https://translate.yandex.com/?text=" + messageText;
- window.open(tUrl,"_blank");
- };
- PPBS.translateDeepL = function(postID) {
- var message = $.id("m" + postID);
- var info = $.id("pi" + postID);
- if (!message) {
- return;
- }
- var messageText = PPBS.parseMessage(message.innerHTML);
- var tUrl = "https://www.deepl.com/translator#auto/en/" + messageText;
- window.open(tUrl,"_blank");
- };
- PPBS.onClick = function(e) {
- var t = e.target;
- switch(t.getAttribute("data-cmd")) {
- case "post-menu":
- setTimeout(PPBS.updatePostMenu, 10);
- break;
- case "ppbs-translate-google":
- PPBS.translateGoogle(t.getAttribute("data-id"));
- break;
- case "ppbs-translate-bing":
- PPBS.translateBing(t.getAttribute("data-id"));
- break;
- case "ppbs-translate-yandex":
- PPBS.translateYandex(t.getAttribute("data-id"));
- break;
- case "ppbs-translate-deepl":
- PPBS.translateDeepL(t.getAttribute("data-id"));
- break;
- default: break;
- }
- };
- 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;
- if (/catalog/.test(window.location.href)) {
- return;
- }
- $.on(document, "click", PPBS.onClick);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement