Guest User

add xReddit buttons to Old Reddit Posts

a guest
Sep 30th, 2025
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 2.31 KB | Source Code | 0 0
  1. const setupMorePostsObserver = (functionTocall) => {
  2.   // set up a mutation observer to modify the post bottom bar when new posts are loaded
  3.   const postBottomBarObserver = new MutationObserver((mutations) => {
  4.     const hasAddedNodes = mutations.some((mutation) => {
  5.       // check if the added node is a thing link
  6.       return (
  7.         mutation.addedNodes.length > 0 &&
  8.         ($(mutation.addedNodes[0]).is(".thing.link") ||
  9.           $(mutation.addedNodes[0]).is(".sitetable")) // in case of infinite scroll
  10.       );
  11.     });
  12.     if (hasAddedNodes) {
  13.       // call the function to modify the post
  14.       functionTocall();
  15.     }
  16.   });
  17.  
  18.   // observe the body for new posts
  19.   postBottomBarObserver.observe(document, {
  20.     childList: true,
  21.     subtree: true,
  22.   });
  23. };
  24.  
  25. const addXRedditButtons = () => {
  26.   const thingLinks = $(".thing.link").not(".xreddit-modified");
  27.  
  28.   thingLinks.each((index, element) => {
  29.     // mark the post as modified
  30.     $(element).addClass("xreddit-modified");
  31.  
  32.     const thingLinkHref = $(element).attr("data-permalink");
  33.  
  34.     const buttonList = $(element).find(".flat-list.buttons");
  35.     // add buttons for vxreddit and rxddit
  36.  
  37.     const vxredditButton = $(
  38.       `<li><a href="javascript:;" class="xreddit-button">vxreddit</a></li>`
  39.     );
  40.  
  41.     vxredditButton.on("click", () => {
  42.       // copy the link to clipboard
  43.       const vxredditLink = `https://www.vxreddit.com${thingLinkHref}`;
  44.       navigator.clipboard.writeText(vxredditLink).then(() => {
  45.         // alert the user that the link has been copied
  46.         alert("vxreddit link copied to clipboard!");
  47.       });
  48.     });
  49.  
  50.     const rxdditButton = $(
  51.       `<li><a href="javascript:;" class="xreddit-button">rxddit</a></li>`
  52.     );
  53.  
  54.     rxdditButton.on("click", () => {
  55.       // copy the link to clipboard
  56.       const rxdditLink = `https://www.rxddit.com${thingLinkHref}`;
  57.       navigator.clipboard.writeText(rxdditLink).then(() => {
  58.         // alert the user that the link has been copied
  59.         alert("rxddit link copied to clipboard!");
  60.       });
  61.     });
  62.  
  63.     buttonList.append(vxredditButton);
  64.     buttonList.append(rxdditButton);
  65.   });
  66. };
  67.  
  68. // initial call to add buttons to existing posts
  69. addXRedditButtons();
  70. setupMorePostsObserver(() => {
  71.   // when new posts are loaded
  72.   addXRedditButtons();
  73. });
  74.  
Advertisement
Add Comment
Please, Sign In to add comment