Created
April 14, 2024 14:07
-
-
Save reallytiredofclowns/1889ecc4c3c697fbf1eaa8c4a0594cd6 to your computer and use it in GitHub Desktop.
Userscript to add comment permalinks to Discuit posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name discuit-comment-permalink | |
// @namespace discuit-aw-userscripts | |
// @version 2024-04-11 | |
// @description Provide a permalink for comments on post pages | |
// @author AuralWanderer | |
// @match https://discuit.net/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=discuit.net | |
// @grant none | |
// ==/UserScript== | |
"use strict"; | |
function nodeSelect(root, selector, handler) { | |
// check the node itself for matching the selector | |
if (root.matches(selector)) { | |
handler(root); | |
} | |
// check the subtree | |
let subMatches = root.querySelectorAll(selector); | |
for (let node of subMatches) { | |
handler(node); | |
} | |
} | |
function addPermalink(comment) { | |
let commentButtons = comment.querySelector("div.post-comment-buttons"); | |
if (commentButtons) { | |
let permalinkButton = document.createElement("a"); | |
permalinkButton.style.textDecoration = "none"; | |
// if the URL already is to a comment, it will have two codes .../post/code1/code2 | |
let URL = window.location.href.match(/((.+)\/post\/(.+?))(\/|$)/)[1]; | |
permalinkButton.href = URL + "/" + comment.id; | |
permalinkButton.innerHTML = "<button class='button-text' title=''>Permalink</button>"; | |
// third button is the one after the downvote count--want to insert permalink after that | |
let thirdButton = commentButtons.querySelectorAll("button")[2]; | |
commentButtons.insertBefore(permalinkButton, thirdButton); | |
} | |
} | |
// for each comment, find the report button, and if found, add another button for the permalink to the comment | |
function observeComments(mutations) { | |
for (let mutation of mutations) { | |
for (let node of mutation.addedNodes) { | |
if (! (node instanceof HTMLElement)) continue; | |
nodeSelect(node, "div.post-comment", addPermalink); | |
} | |
} | |
} | |
let observerInit = new MutationObserver(observeComments); | |
observerInit.observe(document.querySelector("body"), {childList: true, subtree: true}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment