Advertisement
Guest User

DM improvement script

a guest
Mar 27th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     // ==UserScript==
  2. // @name         dm-log
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.0.1
  5. // @description  log previous DMs in case you are forgetful
  6. // @author       isabelle
  7. // @match        https://brainworm.surgery
  8. // @icon         https://brainworm.surgery/src/40ab5a51d61fd1e6.gif
  9. // @grant        none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13.     // 'use strict';
  14.     const require = window.require
  15.     const MAX_MESSAGES = 4
  16.     const handleDM = require.config("dm").handleDM // original function
  17.     const BannerModal = require.config("base/banner").BannerModal
  18.  
  19.     class dmLogClass extends BannerModal {
  20.         constructor() { super(document.getElementById("dmlog")) }
  21.     }
  22.  
  23.     // render the list of messages inside #dm-list
  24.     function updateDMList() {
  25.         let dms = JSON.parse(localStorage.getItem("scriptanon_dms")) || []
  26.         let list = document.getElementById("dmlog-list")
  27.         list.innerHTML = ""
  28.         for (let i=0; i < dms.length; i++) {
  29.             let msg = dms[i]
  30.             // list item
  31.             let li = document.createElement("li")
  32.             // post link
  33.             let sameBoard = window.pagestate.board === msg.board
  34.             let link = document.createElement("a")
  35.             // >>>/board/num notation might not be valid, but i think it looks nice.
  36.             // on brainworm posts are global so nobody ends up typing like this
  37.             link.innerText = sameBoard ? `>>${msg.from}` : `>>>/${msg.board}/${msg.from}`
  38.             link.href = `#p${msg.from}`
  39.             //  brainworm yses data-id to show the post on hover. very cool!
  40.             link.setAttribute("data-id", msg.from)
  41.             link.classList.add("post-link")
  42.  
  43.             // appending stuff
  44.             // staff marker
  45.             if (msg.staff) {
  46.                 // <b> inside an <li>.. that's legal, right?
  47.                 let staffText = document.createElement("b")
  48.                 staffText.innerText = "(s) "
  49.                 staffText.style.color = "#ff0c0c"
  50.                 li.appendChild(staffText)
  51.             }
  52.             // link and message
  53.             li.appendChild(link)
  54.             li.appendChild(document.createTextNode(`: ${msg.text}`))
  55.  
  56.             // add to real list
  57.             list.appendChild(li)
  58.         }
  59.     }
  60.  
  61.     // store messages when they are recieved, and update the UI
  62.     function newHandleDM(m) {
  63.         // pass the message on to the original function
  64.         handleDM(m)
  65.         console.log(m)
  66.         let dms = JSON.parse(localStorage.getItem("scriptanon_dms")) || []
  67.         dms.push(m)
  68.         console.log(dms)
  69.         // store the array of DMs. note to self: i do -MAX + 1 because messages are 0-indexed and not 1-indexed;
  70.         // also negative array slices start from the end of the array
  71.         localStorage.setItem("scriptanon_dms", JSON.stringify(dms.slice(-MAX_MESSAGES)))
  72.         // update list so users can see
  73.         updateDMList()
  74.     }
  75.  
  76.     // create the box with the DMs inside. apparently the page is allergic to just appending the HTML,
  77.     // which is probably not good for some reason anyway
  78.     function createDiv() {
  79.         let div = document.createElement("div")
  80.         div.id = "dmlog"
  81.         div.classList.add("modal", "glass")
  82.         let title = document.createElement("h3")
  83.         title.classList.add("info-header")
  84.         title.innerText = "DMs"
  85.         let ul = document.createElement("ul")
  86.         ul.id = "dmlog-list"
  87.         ul.style = "list-style: none; padding-left: 0;"
  88.         div.appendChild(title)
  89.         div.appendChild(ul)
  90.         return div
  91.     }
  92.  
  93.     //
  94.     // when the site is loaded, add the panel and button, populate it, then set up logging of messages
  95.     // TODO: find out when to run this
  96.     //
  97.  
  98.     // set up the HTML stuff
  99.     let modalOverlay = document.getElementById("modal-overlay")
  100.     let buttonRow = document.getElementById("banner").getElementsByClassName("center-vertically")[1]
  101.  
  102.     let button = document.createElement("a")
  103.     button.id = "banner-dmlog"
  104.     button.innerText = "DMs"
  105.  
  106.     modalOverlay.appendChild(createDiv())
  107.     buttonRow.appendChild(button)
  108.  
  109.     // make it work
  110.     let dmLog = new dmLogClass()
  111.  
  112.     // set the DM handler to our own
  113.     let conn = require.config("connection/index")
  114.     conn.handlers[conn.message.sendDM] = newHandleDM
  115.  
  116.     // load saved DMs
  117.     updateDMList()
  118.  
  119.     // show the DM list when a keybind is pressed
  120.     document.addEventListener("keydown", (e) => {
  121.         if (e.key === "a" && e.altKey === true) {
  122.             document.getElementById("dmlog").classList.toggle("show")
  123.         }
  124.     })
  125.  
  126. })();
Tags: brainworm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement