| 1 | // ==UserScript== |
| 2 | // @name pluralkit-predict |
| 3 | // @namespace https://shale.paperclover.net/userscripts/discord-pluralkit-predict/ |
| 4 | // @version 1.0.0 |
| 5 | // @description Hides a PluralKit original the instant its proxy renders. |
| 6 | // @match https://discord.com/* |
| 7 | // @match https://ptb.discord.com/* |
| 8 | // @match https://canary.discord.com/* |
| 9 | // @run-at document-idle |
| 10 | // @inject-into content |
| 11 | // @grant none |
| 12 | // ==/UserScript== |
| 13 | |
| 14 | // general theory of how this script works made in an after by clover, |
| 15 | // written in two shots with ai. i have sincere apologies to those that |
| 16 | // dislike the tech, but i will at least say that i did not spend my |
| 17 | // own money on the slop machine. |
| 18 | |
| 19 | function normalize(s) { |
| 20 | return s.replace(/\s+/g, " ").trim(); |
| 21 | } |
| 22 | |
| 23 | function bigrams(s) { |
| 24 | const counts = new Map(); |
| 25 | for (let i = 0; i + 1 < s.length; i++) { |
| 26 | const gram = s.slice(i, i + 2); |
| 27 | counts.set(gram, (counts.get(gram) || 0) + 1); |
| 28 | } |
| 29 | return counts; |
| 30 | } |
| 31 | |
| 32 | function dice(a, b) { |
| 33 | const left = bigrams(a); |
| 34 | const right = bigrams(b); |
| 35 | let shared = 0; |
| 36 | for (const [gram, n] of left) shared += Math.min(n, right.get(gram) || 0); |
| 37 | return (2 * shared) / (a.length - 1 + b.length - 1); |
| 38 | } |
| 39 | |
| 40 | function score(proxied, candidate) { |
| 41 | const p = normalize(proxied); |
| 42 | const c = normalize(candidate); |
| 43 | if (!p && !c) return { confidence: 0.6, residue: 0 }; |
| 44 | if (p === c) return { confidence: 1, residue: 0 }; |
| 45 | if (p && c.includes(p) && c.length - p.length <= 8) { |
| 46 | return { confidence: 0.9, residue: c.length - p.length }; |
| 47 | } |
| 48 | if (p.length >= 12 && c.length >= 12) { |
| 49 | const coefficient = dice(p, c); |
| 50 | if (coefficient >= 0.92) return { confidence: coefficient, residue: 0 }; |
| 51 | } |
| 52 | return null; |
| 53 | } |
| 54 | |
| 55 | const HIDE_STYLE = "display: none !important"; |
| 56 | const THRESHOLD = 0.6; |
| 57 | const MAX_RULES = 200; |
| 58 | const MAX_LOOKBACK = 10; |
| 59 | const WINDOW_MS = 60_000; |
| 60 | const EMPTY_WINDOW_MS = 30_000; |
| 61 | const RECOVERY_MS = 10_000; |
| 62 | const AUTOPROXY_HITS = 2; |
| 63 | const AUTOPROXY_TTL_MS = 5 * 60_000; |
| 64 | |
| 65 | const sheet = document.head.appendChild(document.createElement("style")).sheet; |
| 66 | const hidden = new Map(); |
| 67 | const believedDeleted = new Set(); |
| 68 | const autoproxy = new Map(); |
| 69 | |
| 70 | function messageId(liId) { |
| 71 | return liId.slice(liId.lastIndexOf("-") + 1); |
| 72 | } |
| 73 | |
| 74 | function prevMessage(li) { |
| 75 | for (let n = li.previousElementSibling; n; n = n.previousElementSibling) { |
| 76 | if (n.matches('li[id^="chat-messages-"]')) return n; |
| 77 | } |
| 78 | return null; |
| 79 | } |
| 80 | |
| 81 | function header(li) { |
| 82 | for (let n = li; n; n = prevMessage(n)) { |
| 83 | if (n.querySelector('[id^="message-username-"]')) return n; |
| 84 | } |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | function isApp(li) { |
| 89 | return !!header(li)?.querySelector('[class*="botTag"]'); |
| 90 | } |
| 91 | |
| 92 | function author(li) { |
| 93 | return header(li)?.querySelector('[id^="message-username-"]').textContent || |
| 94 | null; |
| 95 | } |
| 96 | |
| 97 | // Autoproxy leaves content byte-identical, so a confirmed exact match is the only |
| 98 | // evidence that lets us hide the next message before its proxy exists. |
| 99 | function autoproxied(li) { |
| 100 | const name = author(li); |
| 101 | const seen = name && autoproxy.get(name); |
| 102 | const fresh = seen && seen.hits >= AUTOPROXY_HITS && |
| 103 | Date.now() - seen.lastAt < AUTOPROXY_TTL_MS; |
| 104 | return fresh ? name : null; |
| 105 | } |
| 106 | |
| 107 | function timestamp(li) { |
| 108 | const time = li.querySelector("time[datetime]"); |
| 109 | const parsed = time && Date.parse(time.dateTime); |
| 110 | return Number.isFinite(parsed) ? parsed : null; |
| 111 | } |
| 112 | |
| 113 | // Scoped by id because a reply's preview of its parent is also a message-content. |
| 114 | function content(li) { |
| 115 | return li.querySelector(`[id="message-content-${messageId(li.id)}"]`); |
| 116 | } |
| 117 | |
| 118 | function text(li) { |
| 119 | const root = content(li); |
| 120 | if (!root) return ""; |
| 121 | const walker = document.createTreeWalker( |
| 122 | root, |
| 123 | NodeFilter.SHOW_TEXT | NodeFilter.SHOW_ELEMENT, |
| 124 | { |
| 125 | acceptNode(node) { |
| 126 | if (node.nodeType === Node.TEXT_NODE) return NodeFilter.FILTER_ACCEPT; |
| 127 | if (node.matches('[class*="edited"]')) return NodeFilter.FILTER_REJECT; |
| 128 | return node.tagName === "IMG" && node.alt |
| 129 | ? NodeFilter.FILTER_ACCEPT |
| 130 | : NodeFilter.FILTER_SKIP; |
| 131 | }, |
| 132 | }, |
| 133 | ); |
| 134 | let out = ""; |
| 135 | for (let n = walker.nextNode(); n; n = walker.nextNode()) { |
| 136 | out += n.nodeType === Node.TEXT_NODE ? n.data : n.alt; |
| 137 | } |
| 138 | return out; |
| 139 | } |
| 140 | |
| 141 | function revoke(id) { |
| 142 | const entry = hidden.get(id); |
| 143 | clearTimeout(entry.timer); |
| 144 | hidden.delete(id); |
| 145 | believedDeleted.delete(id); |
| 146 | const selector = `li[id="${entry.liId}"]`; |
| 147 | for (let i = 0; i < sheet.cssRules.length; i++) { |
| 148 | if (sheet.cssRules[i].selectorText === selector) { |
| 149 | sheet.deleteRule(i); |
| 150 | return; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | function hide(li, credit) { |
| 156 | const id = messageId(li.id); |
| 157 | sheet.insertRule( |
| 158 | `li[id="${li.id}"] { ${HIDE_STYLE} }`, |
| 159 | sheet.cssRules.length, |
| 160 | ); |
| 161 | hidden.set(id, { |
| 162 | liId: li.id, |
| 163 | timer: setTimeout(() => { |
| 164 | if (document.getElementById(hidden.get(id).liId)) { |
| 165 | revoke(id); |
| 166 | if (credit) autoproxy.delete(credit); |
| 167 | } else { |
| 168 | believedDeleted.add(id); |
| 169 | if (credit) { |
| 170 | autoproxy.set(credit, { |
| 171 | hits: (autoproxy.get(credit)?.hits || 0) + 1, |
| 172 | lastAt: Date.now(), |
| 173 | }); |
| 174 | } |
| 175 | } |
| 176 | }, RECOVERY_MS), |
| 177 | }); |
| 178 | if (hidden.size > MAX_RULES) revoke(hidden.keys().next().value); |
| 179 | } |
| 180 | |
| 181 | function claim(proxy) { |
| 182 | const proxied = normalize(text(proxy)); |
| 183 | const proxyAt = timestamp(proxy); |
| 184 | let best = null; |
| 185 | let candidate = prevMessage(proxy); |
| 186 | for ( |
| 187 | let back = 1; |
| 188 | candidate && back <= MAX_LOOKBACK; |
| 189 | back++, candidate = prevMessage(candidate) |
| 190 | ) { |
| 191 | const at = timestamp(candidate); |
| 192 | const age = proxyAt !== null && at !== null ? proxyAt - at : 0; |
| 193 | if (age > WINDOW_MS) break; |
| 194 | if (hidden.has(messageId(candidate.id)) || isApp(candidate)) continue; |
| 195 | if (!proxied && (back > 1 || age > EMPTY_WINDOW_MS)) continue; |
| 196 | const match = score(proxied, text(candidate)); |
| 197 | if (!match || match.confidence < THRESHOLD) continue; |
| 198 | if ( |
| 199 | !best || match.confidence > best.confidence || |
| 200 | (match.confidence === best.confidence && match.residue < best.residue) |
| 201 | ) { |
| 202 | best = { ...match, li: candidate }; |
| 203 | } |
| 204 | } |
| 205 | if (best) hide(best.li, best.confidence === 1 ? author(best.li) : null); |
| 206 | } |
| 207 | |
| 208 | // Discord renders a proxy as a group start while the hidden original sits between it |
| 209 | // and the previous proxy, until PluralKit deletes the original. |
| 210 | function ungroup(proxy) { |
| 211 | let prev = prevMessage(proxy); |
| 212 | if (!prev || !hidden.has(messageId(prev.id))) return; |
| 213 | while (prev && hidden.has(messageId(prev.id))) prev = prevMessage(prev); |
| 214 | if (!prev || !isApp(prev) || author(prev) !== author(proxy)) return; |
| 215 | const start = proxy.querySelector('[class*="groupStart"]'); |
| 216 | const body = content(proxy); |
| 217 | if (!start || !body) return; |
| 218 | start.classList.remove( |
| 219 | [...start.classList].find((c) => c.startsWith("groupStart")), |
| 220 | ); |
| 221 | for (const sibling of body.parentElement.children) { |
| 222 | if (sibling !== body) sibling.style.display = "none"; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | function onInserted(li) { |
| 227 | const id = messageId(li.id); |
| 228 | // A message PluralKit deleted can never re-mount, so a re-mount proves we hid a live one. |
| 229 | if (believedDeleted.has(id)) { |
| 230 | revoke(id); |
| 231 | return; |
| 232 | } |
| 233 | if (hidden.has(id)) return; |
| 234 | if (isApp(li)) { |
| 235 | claim(li); |
| 236 | ungroup(li); |
| 237 | return; |
| 238 | } |
| 239 | const name = autoproxied(li); |
| 240 | if (name) { |
| 241 | hide(li, name); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | const observer = new MutationObserver((records) => { |
| 246 | for (const record of records) { |
| 247 | for (const node of record.addedNodes) { |
| 248 | if (node.nodeType !== Node.ELEMENT_NODE) continue; |
| 249 | if (node.matches('li[id^="chat-messages-"]')) onInserted(node); |
| 250 | else {for ( |
| 251 | const li of node.querySelectorAll('li[id^="chat-messages-"]') |
| 252 | ) onInserted(li);} |
| 253 | } |
| 254 | } |
| 255 | }); |
| 256 | |
| 257 | let observed = null; |
| 258 | function attach() { |
| 259 | if (observed?.isConnected) return; |
| 260 | const list = document.querySelector('[data-list-id="chat-messages"]'); |
| 261 | if (!list) return; |
| 262 | observer.disconnect(); |
| 263 | observer.observe(list, { childList: true, subtree: true }); |
| 264 | observed = list; |
| 265 | } |
| 266 | |
| 267 | attach(); |
| 268 | setInterval(attach, 1000); |