Guest User

Untitled

a guest
Dec 24th, 2021
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         ПУК-СРЕНЬК
  3. // @version      2.1
  4. // @description  Encode and decode 2ch.hk fart code
  5. // @author       Anonymous
  6. // @include      *://2ch.*
  7. // @grant        none
  8. // @run-at       document-end
  9. // ==/UserScript==
  10.  
  11. const syms = [
  12.     "ПУК",
  13.     "СРЕНЬК",
  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. const keysLen = Math.log2(syms.length);
  47.  
  48. const symsEncode = () => {
  49.     let result = {};
  50.  
  51.     syms.forEach((item, index) => {
  52.         result[
  53.             ("0".repeat(keysLen) + index.toString(2)).slice(-keysLen)
  54.         ] = item;
  55.     });
  56.  
  57.     return result;
  58. };
  59.  
  60. const symsDecode = () => {
  61.     let result = {};
  62.  
  63.     Object.entries(symsEncode()).forEach(([key, value]) => {
  64.         result[value] = key;
  65.     });
  66.  
  67.     return result;
  68. };
  69.  
  70. const rle = (string) => {
  71.     let arr = string.split("-"),
  72.         encoding = [],
  73.         previous = arr[0],
  74.         count = 1;
  75.  
  76.     for (let i = 1; i < arr.length; i++) {
  77.         if (arr[i] !== previous) {
  78.             encoding.push(count, previous);
  79.             count = 1;
  80.             previous = arr[i];
  81.         } else {
  82.             count++;
  83.         }
  84.     }
  85.  
  86.     encoding.push(count, previous);
  87.     return encoding.join("-").replace(/1-/g, "").replace(/(\d)-/g, "$1");
  88. };
  89.  
  90. const decode = (string) => {
  91.     const data = symsDecode();
  92.  
  93.     let ba = string
  94.         .split("-")
  95.         .map((item) => {
  96.             const count = /(\d*)(\w+)-?/.exec(item);
  97.  
  98.             if (count) {
  99.                 const key = item.replace(count[0], "");
  100.                 return data[key].repeat(parseInt(count[0]));
  101.             }
  102.  
  103.             return data[item];
  104.         })
  105.         .join("");
  106.  
  107.     const padLen = ba.length % 8;
  108.     ba = ba.substring(0, ba.length - padLen);
  109.  
  110.     return new TextDecoder().decode(
  111.         new Uint8Array(ba.match(/.{1,8}/g).map((c) => parseInt(c, 2)))
  112.     );
  113. };
  114.  
  115. const encode = (string) => {
  116.     let ba = new TextEncoder()
  117.         .encode(string)
  118.         .reduce((s, b) => s + b.toString(2).padStart(8, "0"), "");
  119.  
  120.     const padLen = keysLen - (ba.length % keysLen);
  121.     ba = ba + "0".repeat(padLen);
  122.  
  123.     const data = symsEncode();
  124.  
  125.     return rle(
  126.         ba
  127.             .match(new RegExp(`.{1,${keysLen}}`, "g"))
  128.             .map((item) => {
  129.                 return data[item];
  130.             })
  131.             .join("-")
  132.     );
  133. };
  134.  
  135. (() => {
  136.     const parse = (node) => {
  137.         const post = node.querySelector(".post__message");
  138.         if (post && post.textContent) {
  139.             const words = post.textContent
  140.                 .replace(/\t+/g, "")
  141.                 .split(/[\n,\s]+/);
  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.                         const thread = node.parentElement.parentElement.id.replace(
  150.                             /thread-/,
  151.                             ""
  152.                         );
  153.                         const board = window.location.pathname.split("/")[1];
  154.  
  155.                         return `<span style="color:green;font-size:1.21em">${decode(
  156.                             m
  157.                         )
  158.                             .replace(/<.*?>/g, "[Я у мамы идиот]")
  159.                             .replace(
  160.                                 /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim,
  161.                                 '<a href="$1" target="_blank" target="_blank" rel="nofollow noopener noreferrer">$1</a>'
  162.                             )
  163.                             .replace(
  164.                                 /(^|[^\/])(www\.[\S]+(\b|$))/gim,
  165.                                 '$1<a href="http://$2" target="_blank" rel="nofollow noopener noreferrer">$2</a>'
  166.                             )
  167.                             .replace(
  168.                                 /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim,
  169.                                 '<a href="mailto:$1" target="_blank" rel="nofollow noopener noreferrer">$1</a>'
  170.                             )
  171.                             .replace(
  172.                                 /\>>(\d{9})/gim,
  173.                                 '<a href="/' +
  174.                                     board +
  175.                                     "/res/" +
  176.                                     thread +
  177.                                     '.html#$1" class="post-reply-link" data-thread="' +
  178.                                     thread +
  179.                                     '" data-num="$1">&gt;&gt;$1</a>'
  180.                             )
  181.                             .replace(/\n/g, "<br>")}</span>`;
  182.                     } catch (err) {
  183.                         return m;
  184.                     }
  185.                 });
  186.             });
  187.         }
  188.     };
  189.  
  190.     document.querySelectorAll(".post").forEach((post) => parse(post));
  191.  
  192.     const observer = new MutationObserver((o) => {
  193.         o.forEach((i) => {
  194.             if (i.addedNodes) {
  195.                 i.addedNodes.forEach((n) => parse(n));
  196.             }
  197.         });
  198.     });
  199.     const config = { childList: true };
  200.     observer.observe(document.querySelector(".thread"), config);
  201.     observer.observe(document.querySelector("#posts-form"), config);
  202.  
  203.     let container = document.createElement("div");
  204.     container.className = "options__box";
  205.  
  206.     let enBtn = document.createElement("button");
  207.     enBtn.className = "desktop button";
  208.     enBtn.style = "color:green;font-weight:bold";
  209.     enBtn.textContent = `Зашифровать`;
  210.     enBtn.addEventListener("click", (e) => {
  211.         e.preventDefault();
  212.  
  213.         const comment = document.querySelector('[name="comment"]');
  214.         const selection = comment.value.substring(
  215.             comment.selectionStart,
  216.             comment.selectionEnd
  217.         );
  218.  
  219.         if (selection) {
  220.             const encoded = encode(selection);
  221.             const start = comment.selectionStart;
  222.             comment.value =
  223.                 comment.value.slice(0, start) +
  224.                 encoded +
  225.                 comment.value.slice(comment.selectionEnd);
  226.             comment.focus();
  227.             comment.setSelectionRange(start, start + encoded.length);
  228.         } else {
  229.             comment.value = encode(comment.value);
  230.         }
  231.     });
  232.     container.append(enBtn);
  233.  
  234.     let deBtn = document.createElement("button");
  235.     deBtn.className = "desktop button";
  236.     deBtn.style = "color:red;font-weight:bold";
  237.     deBtn.textContent = `Расшифровать`;
  238.     deBtn.addEventListener("click", (e) => {
  239.         e.preventDefault();
  240.  
  241.         try {
  242.             const comment = document.querySelector('[name="comment"]');
  243.             const selection = comment.value.substring(
  244.                 comment.selectionStart,
  245.                 comment.selectionEnd
  246.             );
  247.  
  248.             if (selection) {
  249.                 const decoded = decode(selection);
  250.                 const start = comment.selectionStart;
  251.                 comment.value =
  252.                     comment.value.slice(0, start) +
  253.                     decoded +
  254.                     comment.value.slice(comment.selectionEnd);
  255.                 comment.focus();
  256.                 comment.setSelectionRange(start, start + decoded.length);
  257.             } else {
  258.                 comment.value = decode(comment.value);
  259.             }
  260.         } catch (err) {}
  261.     });
  262.     container.append(deBtn);
  263.  
  264.     document.querySelector(".options").append(container);
  265. })();
  266.  
Add Comment
Please, Sign In to add comment