Advertisement
AnonymousFren

Baited Newfag Awards

Jul 30th, 2022 (edited)
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        Baited Newfag Awards
  3. // @description Baited Newfag Awards
  4. // @author      Anonymous
  5. // @version     1.1.0
  6. // @match       https://boards.4chan.org/*
  7. // @match       https://boards.4channel.org/*
  8. // @grant       none
  9. // ==/UserScript==
  10.  
  11. // Note that I'm not using jQuery because 4chan doesn't play nice with certain event handlers and errors get spammed
  12.  
  13. (function() {
  14.   "use strict";
  15.  
  16.   function GetOrdinal(int) {
  17.     const dString = String(int);
  18.     const last = +dString.slice(-2);
  19.  
  20.     if (last > 3 && last < 21) {
  21.       return "th";
  22.     }
  23.  
  24.     switch (last % 10) {
  25.       case 1: return "st";
  26.       case 2: return "nd";
  27.       case 3: return "rd";
  28.       default: return "th";
  29.     }
  30.   }
  31.  
  32.   function FallbackCopyTextToClipboard(text) {
  33.     const textArea = document.createElement("textarea");
  34.     textArea.value = text;
  35.  
  36.     // Avoid scrolling to bottom
  37.     textArea.style.top = "0";
  38.     textArea.style.left = "0";
  39.     textArea.style.position = "fixed";
  40.  
  41.     document.body.appendChild(textArea);
  42.     textArea.focus();
  43.     textArea.select();
  44.  
  45.     try {
  46.       const successful = document.execCommand("copy");
  47.       const msg = successful ? "successful" : "unsuccessful";
  48.       console.log(`Fallback: Copying text command was ${msg}`);
  49.     } catch (err) {
  50.       console.error("Fallback: Oops, unable to copy", err);
  51.     }
  52.  
  53.     document.body.removeChild(textArea);
  54.   }
  55.  
  56.   function CopyTextToClipboard(text) {
  57.     if (!navigator.clipboard) {
  58.       FallbackCopyTextToClipboard(text);
  59.       return;
  60.     }
  61.  
  62.     navigator.clipboard.writeText(text).then(() => {
  63.       console.log("Async: Copying to clipboard was successful!");
  64.     }, (err) => {
  65.       console.error("Async: Could not copy text: ", err);
  66.     });
  67.   }
  68.  
  69.   // Create new CSS styling for the Baited Newfag Awards button that will appear on the navigation at the top of the page
  70.   const cssText = `
  71.     .btn-baited-newfag-awards {
  72.       position: absolute;
  73.       border: 1px solid black;
  74.       background-color: white;
  75.       color: black;
  76.       padding: 2px 4px;
  77.       cursor: pointer;
  78.       user-select: none;
  79.       -ms-user-select: none;
  80.       -moz-user-select: none;
  81.       -khtml-user-select: none;
  82.       -webkit-user-select: none;
  83.     }
  84.  
  85.     .btn-baited-newfag-awards:hover {
  86.       background-color: lightgray;
  87.     }
  88.  
  89.     .btn-baited-newfag-awards:active {
  90.       background-color: gray;
  91.       color: white;
  92.     }
  93.   `;
  94.  
  95.   const style = document.createElement("style");
  96.   style.type = "text/css";
  97.   style.innerText = cssText;
  98.   document.head.appendChild(style);
  99.  
  100.   // Create the button and put it onto the navigation at the top of the page
  101.   const parentElement = document.getElementsByTagName("body")[0];
  102.   const button = document.createElement("span");
  103.   button.innerText = "Baited Newfag Awards";
  104.   button.classList.add("btn-baited-newfag-awards");
  105.   parentElement.insertBefore(button, parentElement.children[0]);
  106.  
  107.   let baitedNewfagBtnTop = localStorage.getItem("baited-newfag-btn-top");
  108.   let baitedNewfagBtnLeft = localStorage.getItem("baited-newfag-btn-left");
  109.  
  110.   if (baitedNewfagBtnTop === null || baitedNewfagBtnLeft === null) {
  111.     baitedNewfagBtnTop = "120px";
  112.     baitedNewfagBtnLeft = "10px";
  113.     localStorage.setItem("baited-newfag-btn-top", baitedNewfagBtnTop);
  114.     localStorage.setItem("baited-newfag-btn-left", baitedNewfagBtnLeft);
  115.   }
  116.  
  117.   button.style.top = baitedNewfagBtnTop;
  118.   button.style.left = baitedNewfagBtnLeft;
  119.  
  120.   let dragging = true;
  121.  
  122.   // Generate "Welcome to the baited newfag awards!" copypasta text whenever you click on the button
  123.   button.onclick = function() {
  124.     if (dragging === false) {
  125.       dragging = true;
  126.       return;
  127.     }
  128.  
  129.     const data = {};
  130.     const dataArr = [];
  131.     const postContainers = document.getElementsByClassName("postContainer");
  132.  
  133.     for (let i = 0; i < postContainers.length; i++) {
  134.       const postContainer = postContainers[i];
  135.       const user = postContainer.querySelectorAll("[title='Highlight posts by this ID']")[0];
  136.       const post = postContainer.querySelectorAll("[title='Reply to this post']")[0];
  137.       const userId = user.innerText;
  138.       const postNumber = post.innerText;
  139.  
  140.       if (userId in data) {
  141.         data[userId].push(`>>${postNumber}`);
  142.       } else {
  143.         data[userId] = [`>>${postNumber}`];
  144.       }
  145.     }
  146.  
  147.     // Only keep newfags that have at least two posts
  148.     for (const key in data) {
  149.       if (data[key].length === 1) {
  150.         delete data[key];
  151.       }
  152.     }
  153.  
  154.     // Transform the data into a data array so that it can be sorted
  155.     for (const key in data) {
  156.       dataArr.push({
  157.         user: key,
  158.         posts: data[key]
  159.       });
  160.     }
  161.  
  162.     // Sort the array so that the newfags with the most post appear first
  163.     dataArr.sort((a, b) => {return b.posts.length - a.posts.length;});
  164.  
  165.     let string = "Welcome to the Baited Newfag Awards! Here are the results.";
  166.     let place = 1;
  167.  
  168.     for (const obj of dataArr) {
  169.       const { user, posts } = obj;
  170.       const ordinal = GetOrdinal(place);
  171.       string += `\n\n${place}${ordinal} place:\n${user} with ${posts.length} posts:\n${posts.join(" ")}`;
  172.  
  173.       // Your post can only handle a maximum of 17 places
  174.       if (++place === 18) {
  175.         break;
  176.       }
  177.     }
  178.  
  179.     string += "\n\nTampermonkey Script: https://pastebin.com/UvXMFC5S";
  180.  
  181.     CopyTextToClipboard(string);
  182.     alert("The results of the Baited Newfag Awards has been copied to your clipboard");
  183.   };
  184.  
  185.   function dragElement(element) {
  186.     let pos1 = 0;
  187.     let pos2 = 0;
  188.     let pos3 = 0;
  189.     let pos4 = 0;
  190.  
  191.     function elementDrag(e) {
  192.       dragging = false;
  193.       console.log(dragging);
  194.  
  195.       e = e || window.event;
  196.       e.preventDefault();
  197.       // calculate the new cursor position:
  198.       pos1 = pos3 - e.clientX;
  199.       pos2 = pos4 - e.clientY;
  200.       pos3 = e.clientX;
  201.       pos4 = e.clientY;
  202.       // set the element's new position:
  203.       element.style.top = `${element.offsetTop - pos2}px`;
  204.       element.style.left = `${element.offsetLeft - pos1}px`;
  205.     }
  206.  
  207.     function closeDragElement() {
  208.       /* stop moving when mouse button is released:*/
  209.       document.onmouseup = null;
  210.       document.onmousemove = null;
  211.  
  212.       localStorage.setItem("baited-newfag-btn-top", `${button.getBoundingClientRect().top}px`);
  213.       localStorage.setItem("baited-newfag-btn-left", `${button.getBoundingClientRect().left}px`);
  214.     }
  215.  
  216.     function dragMouseDown(e) {
  217.       e = e || window.event;
  218.       e.preventDefault();
  219.       // get the mouse cursor position at startup:
  220.       pos3 = e.clientX;
  221.       pos4 = e.clientY;
  222.       document.onmouseup = closeDragElement;
  223.       // call a function whenever the cursor moves:
  224.       document.onmousemove = elementDrag;
  225.     }
  226.  
  227.     if (document.getElementById(`${element.id}header`)) {
  228.       /* if present, the header is where you move the DIV from:*/
  229.       document.getElementById(`${element.id}header`).onmousedown = dragMouseDown;
  230.     } else {
  231.       /* otherwise, move the DIV from anywhere inside the DIV:*/
  232.       element.onmousedown = dragMouseDown;
  233.     }
  234.   }
  235.  
  236.   dragElement(button);
  237. })();
  238.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement