Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name ПУК-СРЕНЬК
- // @version 2.1
- // @description Encode and decode 2ch.hk fart code
- // @author Anonymous
- // @include *://2ch.*
- // @grant none
- // @run-at document-end
- // ==/UserScript==
- const syms = [
- "ПУК",
- "СРЕНЬК",
- "ХРЮК",
- "УИИИ",
- "ЛАХТА",
- "ЛИБЕРАХА",
- "ХОХЛЫ",
- "СВИНОСОБАКА",
- "ШВАЙНОКАРАСЬ",
- "ЛОЛ",
- "КЕК",
- "АБУ",
- "ДВАЧ",
- "ПАРАША",
- "ПОРИДЖ",
- "ХРЮЧЕВО",
- "СВИН",
- "ПЕРДИКС",
- "ПЫНЯ",
- "ЧАЮ",
- "ШУЕ",
- "ЛЕВАК",
- "ПРАВАК",
- "КОМИГЛИСТ",
- "ШВЯТЫЕ",
- "ВАТА",
- "ВОЛОДИН",
- "ДОЛБИЛЬНЯ",
- "ПЕРЕФОРС",
- "КУНЧИК",
- "АВАТАРКА",
- "АНАЛЬЧИК",
- ];
- const keysLen = Math.log2(syms.length);
- const symsEncode = () => {
- let result = {};
- syms.forEach((item, index) => {
- result[
- ("0".repeat(keysLen) + index.toString(2)).slice(-keysLen)
- ] = item;
- });
- return result;
- };
- const symsDecode = () => {
- let result = {};
- Object.entries(symsEncode()).forEach(([key, value]) => {
- result[value] = key;
- });
- return result;
- };
- const rle = (string) => {
- let arr = string.split("-"),
- encoding = [],
- previous = arr[0],
- count = 1;
- for (let i = 1; i < arr.length; i++) {
- if (arr[i] !== previous) {
- encoding.push(count, previous);
- count = 1;
- previous = arr[i];
- } else {
- count++;
- }
- }
- encoding.push(count, previous);
- return encoding.join("-").replace(/1-/g, "").replace(/(\d)-/g, "$1");
- };
- const decode = (string) => {
- const data = symsDecode();
- let ba = string
- .split("-")
- .map((item) => {
- const count = /(\d*)(\w+)-?/.exec(item);
- if (count) {
- const key = item.replace(count[0], "");
- return data[key].repeat(parseInt(count[0]));
- }
- return data[item];
- })
- .join("");
- const padLen = ba.length % 8;
- ba = ba.substring(0, ba.length - padLen);
- return new TextDecoder().decode(
- new Uint8Array(ba.match(/.{1,8}/g).map((c) => parseInt(c, 2)))
- );
- };
- const encode = (string) => {
- let ba = new TextEncoder()
- .encode(string)
- .reduce((s, b) => s + b.toString(2).padStart(8, "0"), "");
- const padLen = keysLen - (ba.length % keysLen);
- ba = ba + "0".repeat(padLen);
- const data = symsEncode();
- return rle(
- ba
- .match(new RegExp(`.{1,${keysLen}}`, "g"))
- .map((item) => {
- return data[item];
- })
- .join("-")
- );
- };
- (() => {
- const parse = (node) => {
- const post = node.querySelector(".post__message");
- if (post && post.textContent) {
- const words = post.textContent
- .replace(/\t+/g, "")
- .split(/[\n,\s]+/);
- const re = new RegExp(
- `(\\d+)?(${syms.join("|")})\-(\\S+)\-(\\d+)?(${syms.join("|")})`
- );
- words.forEach((word) => {
- post.innerHTML = post.innerHTML.replace(re, (m) => {
- try {
- const thread = node.parentElement.parentElement.id.replace(
- /thread-/,
- ""
- );
- const board = window.location.pathname.split("/")[1];
- return `<span style="color:green;font-size:1.21em">${decode(
- m
- )
- .replace(/<.*?>/g, "[Я у мамы идиот]")
- .replace(
- /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim,
- '<a href="$1" target="_blank" target="_blank" rel="nofollow noopener noreferrer">$1</a>'
- )
- .replace(
- /(^|[^\/])(www\.[\S]+(\b|$))/gim,
- '$1<a href="http://$2" target="_blank" rel="nofollow noopener noreferrer">$2</a>'
- )
- .replace(
- /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim,
- '<a href="mailto:$1" target="_blank" rel="nofollow noopener noreferrer">$1</a>'
- )
- .replace(
- /\>>(\d{9})/gim,
- '<a href="/' +
- board +
- "/res/" +
- thread +
- '.html#$1" class="post-reply-link" data-thread="' +
- thread +
- '" data-num="$1">>>$1</a>'
- )
- .replace(/\n/g, "<br>")}</span>`;
- } catch (err) {
- return m;
- }
- });
- });
- }
- };
- const encodeAction = (e) => {
- e.preventDefault();
- const comment = e.target.parentElement.parentElement.querySelector('[name="comment"]');
- const selection = comment.value.substring(
- comment.selectionStart,
- comment.selectionEnd
- );
- if (selection) {
- const encoded = encode(selection);
- const start = comment.selectionStart;
- comment.value =
- comment.value.slice(0, start) +
- encoded +
- comment.value.slice(comment.selectionEnd);
- comment.focus();
- comment.setSelectionRange(start, start + encoded.length);
- } else {
- comment.value = encode(comment.value);
- }
- };
- const decodeAction = (e) => {
- e.preventDefault();
- try {
- const comment = e.target.parentElement.parentElement.querySelector('[name="comment"]');
- const selection = comment.value.substring(
- comment.selectionStart,
- comment.selectionEnd
- );
- if (selection) {
- const decoded = decode(selection);
- const start = comment.selectionStart;
- comment.value =
- comment.value.slice(0, start) +
- decoded +
- comment.value.slice(comment.selectionEnd);
- comment.focus();
- comment.setSelectionRange(start, start + decoded.length);
- } else {
- comment.value = decode(comment.value);
- }
- } catch (err) {}
- };
- const initialPosts = document.querySelectorAll(".post");
- if (initialPosts) {
- initialPosts.forEach((post) => parse(post));
- }
- const observer = new MutationObserver((o) => {
- o.forEach((i) => {
- if (i.addedNodes) {
- i.addedNodes.forEach((n) => parse(n));
- }
- });
- });
- const config = { childList: true };
- const newPosts = document.querySelector(".thread");
- if (newPosts) {
- observer.observe(newPosts, config);
- }
- const rePosts = document.querySelector("#posts-form");
- if (rePosts) {
- observer.observe(rePosts, config);
- }
- let enBtnTopBottom = document.createElement("input");
- enBtnTopBottom.className = "button";
- enBtnTopBottom.type = "button";
- enBtnTopBottom.style = "color:green";
- enBtnTopBottom.value = `Кодировать`;
- enBtnTopBottom.addEventListener("click", encodeAction);
- const enQuick = enBtnTopBottom.cloneNode(true);
- enQuick.addEventListener("click", encodeAction);
- let deBtnTopBottom = document.createElement("input");
- deBtnTopBottom.className = "button";
- deBtnTopBottom.type = "button";
- deBtnTopBottom.style = "color:red";
- deBtnTopBottom.value = `Декодировать`;
- deBtnTopBottom.addEventListener("click", decodeAction);
- const deQuick = deBtnTopBottom.cloneNode(true);
- deQuick.addEventListener("click", decodeAction);
- const topBottomForm = document.querySelector("#postform .postform__raw.desktop:not(.options)");
- if (topBottomForm) {
- topBottomForm.append(enBtnTopBottom);
- topBottomForm.append("\n");
- topBottomForm.append(deBtnTopBottom);
- }
- const quickForm = document.querySelector("#qr-postform .postform__raw.desktop");
- if (quickForm) {
- quickForm.append(enQuick);
- quickForm.append("\n");
- quickForm.append(deQuick);
- }
- })();
Add Comment
Please, Sign In to add comment