Guest User

Untitled

a guest
Dec 24th, 2021
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         ПУК-СРЕНЬК
  3. // @namespace    puksrenk
  4. // @version      2.0
  5. // @description  Encode and decode 2ch.hk fart code
  6. // @author       Anonymous
  7. // @match        https://2ch.hk/*/res/*.html
  8. // @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAA21BMVEVHcEzr6OX/+Pb9tJP/9/f+3dP79/P/////9fH////97ej18e/m4uD///////Dd2df949r69vT07+39vqX81Mf89vL94tj88ez/+PX/9PT9zr378Oz8+PT89PH59vL9yrb48/Dy7uvy7ur69vTxw7T+1cr+kkmnemrthkz+j0r6jknFwsD+kEy+elz+pXnJgGH+mmXbgFD9kVX+lmHVflL+lFazqqfY1dKglJCbg3u4fmjGta/RzsvXpZXMycazr6yhmJXm3tqtqKaygnKmn53jvbKciYPKfFaahoCxQDFuAAAAJnRSTlMA7CXxHq5FAjsFd7/zCRD7loXM6dJQiX4yFr5lW1mT0q7v08jXrHUXgQ4AAAEmSURBVDjLlZPXcoMwEEVpphlsB/eWHjDNYMAlbolJ/f8vCkI8iGSlmdzXe0d7tIXj/iVJLSWYFF+bJEmaxvHpToMDemwjLXY8HBiLpe85xxnoK411GfjKmwoY6C9xgU9DAP1Wt/Rdxx/ChB1cIHu/BwkVFRcIoo9eo1BnoM+lWgtu7EqB67pBocXhlgwMU7suzznrhC+Iv3w7fKl9dnp62+/DKMpWHvazV35MVlAtw2hvj2f/kOBmfLflv6OSpJHcqwD8K8o8RQiAkBxDAIQGAQJ4BgCqfnaZABw3Qv0ON03aynF68cnVjpeoSztBANs+famXCOCavvXTtR3mlkYPPCEAge5rYgEwZ9yVnDIBiq2Mcstk+MrjxhBYD7QulxnztM0HaAQ/wGgseU57utgAAAAASUVORK5CYII=
  9. // @grant        none
  10. // @run-at       document-end
  11. // ==/UserScript==
  12.  
  13. const syms = [
  14.     "ПУК",
  15.     "СРЕНЬК",
  16.     "ХРЮК",
  17.     "УИИИ",
  18.     "ЛАХТА",
  19.     "ЛИБЕРАХА",
  20.     "ХОХЛЫ",
  21.     "СВИНОСОБАКА",
  22.     "ШВАЙНОКАРАСЬ",
  23.     "ЛОЛ",
  24.     "КЕК",
  25.     "АБУ",
  26.     "ДВАЧ",
  27.     "ПАРАША",
  28.     "ПОРИДЖ",
  29.     "ХРЮЧЕВО",
  30.     "СВИН",
  31.     "ПЕРДИКС",
  32.     "ПЫНЯ",
  33.     "ЧАЮ",
  34.     "ШУЕ",
  35.     "ЛЕВАК",
  36.     "ПРАВАК",
  37.     "КОМИГЛИСТ",
  38.     "ШВЯТЫЕ",
  39.     "ВАТА",
  40.     "ВОЛОДИН",
  41.     "ДОЛБИЛЬНЯ",
  42.     "ПЕРЕФОРС",
  43.     "КУНЧИК",
  44.     "АВАТАРКА",
  45.     "АНАЛЬЧИК",
  46. ];
  47.  
  48. const keysLen = Math.log2(syms.length);
  49.  
  50. const symsEncode = () => {
  51.     let result = {};
  52.  
  53.     syms.forEach((item, index) => {
  54.         result[
  55.             ("0".repeat(keysLen) + index.toString(2)).slice(-keysLen)
  56.         ] = item;
  57.     });
  58.  
  59.     return result;
  60. };
  61.  
  62. const symsDecode = () => {
  63.     let result = {};
  64.  
  65.     Object.entries(symsEncode()).forEach(([key, value]) => {
  66.         result[value] = key;
  67.     });
  68.  
  69.     return result;
  70. };
  71.  
  72. const rle = (string) => {
  73.     let arr = string.split("-"),
  74.         encoding = [],
  75.         previous = arr[0],
  76.         count = 1;
  77.  
  78.     for (let i = 1; i < arr.length; i++) {
  79.         if (arr[i] !== previous) {
  80.             encoding.push(count, previous);
  81.             count = 1;
  82.             previous = arr[i];
  83.         } else {
  84.             count++;
  85.         }
  86.     }
  87.  
  88.     encoding.push(count, previous);
  89.     return encoding.join("-").replace(/1-/g, "").replace(/(\d)-/g, "$1");
  90. };
  91.  
  92. const decode = (string) => {
  93.     const data = symsDecode();
  94.  
  95.     let ba = string
  96.         .split("-")
  97.         .map((item) => {
  98.             const count = /(\d*)(\w+)-?/.exec(item);
  99.  
  100.             if (count) {
  101.                 const key = item.replace(count[0], "");
  102.                 return data[key].repeat(parseInt(count[0]));
  103.             }
  104.  
  105.             return data[item];
  106.         })
  107.         .join("");
  108.  
  109.     const padLen = ba.length % 8;
  110.     ba = ba.substring(0, ba.length - padLen);
  111.  
  112.     return new TextDecoder().decode(
  113.         new Uint8Array(ba.match(/.{1,8}/g).map((c) => parseInt(c, 2)))
  114.     );
  115. };
  116.  
  117. const encode = (string) => {
  118.     let ba = new TextEncoder()
  119.         .encode(string)
  120.         .reduce((s, b) => s + b.toString(2).padStart(8, "0"), "");
  121.  
  122.     const padLen = keysLen - (ba.length % keysLen);
  123.     ba = ba + "0".repeat(padLen);
  124.  
  125.     const data = symsEncode();
  126.  
  127.     return rle(
  128.         ba
  129.             .match(new RegExp(`.{1,${keysLen}}`, "g"))
  130.             .map((item) => {
  131.                 return data[item];
  132.             })
  133.             .join("-")
  134.     );
  135. };
  136.  
  137. (() => {
  138.     const parse = (node) => {
  139.         const post = node.querySelector(".post__message");
  140.  
  141.         const words = post.textContent.replace(/\t+/g, "").split(/[\n, ]+/);
  142.         const re = new RegExp(
  143.             `(\\d+)?(${syms.join("|")})\-(\\S+)\-(\\d+)?(${syms.join("|")})`
  144.         );
  145.  
  146.         words.forEach((word) => {
  147.             post.innerHTML = post.innerHTML.replace(re, (m) => {
  148.                 try {
  149.                     return `<div style="color:green;font-size:1.21em">${decode(
  150.                         m
  151.                     )
  152.                         .replace(/<.*?>/g, "[Я у мамы идиот]")
  153.                         .replace(/\n/g, "<br>")}</div>`;
  154.                 } catch (err) {
  155.                     return m;
  156.                 }
  157.             });
  158.         });
  159.     };
  160.  
  161.     const posts = document.querySelectorAll(".thread__post");
  162.     parse(document.querySelector(".thread__oppost"));
  163.     posts.forEach((post) => parse(post));
  164.  
  165.     const observer = new MutationObserver((o) => {
  166.         o.forEach((i) => {
  167.             if (i.addedNodes) {
  168.                 i.addedNodes.forEach((node) => parse(node));
  169.             }
  170.         });
  171.     })
  172.  
  173.     const config = { childList: true };
  174.     observer.observe(document.querySelector(".thread"), config)
  175.     observer.observe(document.querySelector("#posts-form"), config);
  176.  
  177.     let div = document.createElement("div");
  178.     div.className = "options__box";
  179.  
  180.     let encrypted = false;
  181.  
  182.     let btn = document.createElement("button");
  183.     btn.className = "desktop button";
  184.     btn.style = "color:green;font-weight:bold";
  185.     btn.textContent = `Зашифровать`;
  186.     btn.addEventListener("click", (e) => {
  187.         e.preventDefault();
  188.  
  189.         const comment = document.querySelector('[name="comment"]');
  190.  
  191.         if (!encrypted) {
  192.             btn.textContent = `Расшифровать`;
  193.             encrypted = true;
  194.             comment.value = encode(comment.value);
  195.             document.querySelector(".postform__len").textContent =
  196.                 15000 - comment.value.length;
  197.         } else {
  198.             btn.textContent = `Зашифровать`;
  199.             encrypted = false;
  200.             comment.value = decode(comment.value);
  201.             btn.textContent =
  202.                 "Зашифровать" +
  203.                 (comment.value.length > 0
  204.                     ? " (" + (15000 - encode(comment.value).length) + ")"
  205.                     : "");
  206.             document.querySelector(".postform__len").textContent =
  207.                 15000 - comment.value.length;
  208.         }
  209.     });
  210.     div.append(btn);
  211.  
  212.     const options = document.querySelector(".options");
  213.     options.append(div);
  214.  
  215.     document
  216.         .querySelector(".postform__len")
  217.         .addEventListener("DOMSubtreeModified", () => {
  218.             if (!encrypted) {
  219.                 const comment = document.querySelector('[name="comment"]');
  220.                 btn.textContent =
  221.                     "Зашифровать" +
  222.                     (comment.value.length > 0
  223.                         ? " (" + (15000 - encode(comment.value).length) + ")"
  224.                         : "");
  225.             } else {
  226.                 btn.textContent = `Расшифровать`;
  227.             }
  228.         });
  229. })();
  230.  
Add Comment
Please, Sign In to add comment