Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const setupMorePostsObserver = (functionTocall) => {
- // set up a mutation observer to modify the post bottom bar when new posts are loaded
- const postBottomBarObserver = new MutationObserver((mutations) => {
- const hasAddedNodes = mutations.some((mutation) => {
- // check if the added node is a thing link
- return (
- mutation.addedNodes.length > 0 &&
- ($(mutation.addedNodes[0]).is(".thing.link") ||
- $(mutation.addedNodes[0]).is(".sitetable")) // in case of infinite scroll
- );
- });
- if (hasAddedNodes) {
- // call the function to modify the post
- functionTocall();
- }
- });
- // observe the body for new posts
- postBottomBarObserver.observe(document, {
- childList: true,
- subtree: true,
- });
- };
- const addXRedditButtons = () => {
- const thingLinks = $(".thing.link").not(".xreddit-modified");
- thingLinks.each((index, element) => {
- // mark the post as modified
- $(element).addClass("xreddit-modified");
- const thingLinkHref = $(element).attr("data-permalink");
- const buttonList = $(element).find(".flat-list.buttons");
- // add buttons for vxreddit and rxddit
- const vxredditButton = $(
- `<li><a href="javascript:;" class="xreddit-button">vxreddit</a></li>`
- );
- vxredditButton.on("click", () => {
- // copy the link to clipboard
- const vxredditLink = `https://www.vxreddit.com${thingLinkHref}`;
- navigator.clipboard.writeText(vxredditLink).then(() => {
- // alert the user that the link has been copied
- alert("vxreddit link copied to clipboard!");
- });
- });
- const rxdditButton = $(
- `<li><a href="javascript:;" class="xreddit-button">rxddit</a></li>`
- );
- rxdditButton.on("click", () => {
- // copy the link to clipboard
- const rxdditLink = `https://www.rxddit.com${thingLinkHref}`;
- navigator.clipboard.writeText(rxdditLink).then(() => {
- // alert the user that the link has been copied
- alert("rxddit link copied to clipboard!");
- });
- });
- buttonList.append(vxredditButton);
- buttonList.append(rxdditButton);
- });
- };
- // initial call to add buttons to existing posts
- addXRedditButtons();
- setupMorePostsObserver(() => {
- // when new posts are loaded
- addXRedditButtons();
- });
Advertisement
Add Comment
Please, Sign In to add comment