Lemmy Plugins and Userscripts

2174 readers
1 users here now

A general repository for user scripts and plugins used to enhance the Lemmy browsing experience.

Post (or cross-post) your favorite Lemmy enhancements here!

General posting suggestions:

Thanks!

founded 1 year ago
MODERATORS
76
38
submitted 1 year ago* (last edited 1 year ago) by god to c/plugins
 
 

Wanted to do this for a while. Did it today instead of sleeping.

Screenshot:

You can install it from here: https://greasyfork.org/en/scripts/468948-user-details-on-hover

Link to GitHub repo: https://github.com/lemmygod/lemmy-hovercards/tree/main

Or you can copy-paste the following code:

::: spoiler click here to view code.

// ==UserScript==
// @name         User Details on Hover
// @namespace    http://tampermonkey.net/
// @version      0.12
// @description  Show user details on hover
// @author       You
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function () {
  "use strict";
  const isLemmy =
    document.head.querySelector("[name~=Description][content]").content ===
    "Lemmy";
  if (!isLemmy) return;
  // Inject styles for the user card
  function main() {
    const style = document.createElement("style");
    style.innerHTML = `
  .user-card {
    position: absolute;
    display: none;
    width: 350px;
    background-color: #242424;
    color: white;
    padding: 15px;
    border-radius: 10px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1000;
    grid-gap: 10px;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    line-height: 1.4;
  }

  .user-card .header {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
  }

  .user-card img {
    width: 80px;
    height: 80px;
    object-fit: cover;
    border-radius: 50%;
    margin-right: 15px;
  }

  .user-card .username {
    font-size: 1.3em;
    font-weight: bold;
  }

  .user-card .instance {
    font-size: 0.8em;
    color: #888;
  }

  .user-card .body {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-gap: 10px;
  }

  .user-card .key {
    font-weight: bold;
  }

  .user-card .value {
    color: #ddd;
    margin-top: 10px;
  }

  .user-card .bio {
    grid-column: 1 / -1;
    font-style: italic;
  }`;
    document.head.appendChild(style);

    // Create the user card
    const userCard = document.createElement("div");
    userCard.classList.add("user-card");
    userCard.id = "user-card";
    document.body.appendChild(userCard);

    let timer;
    // Find all user links
    const userLinks = document.querySelectorAll('a.text-info[href*="/u/"]');
    userLinks.forEach((userLink) => {
      userLink.setAttribute("title", "");
      // When mouse enters, show the user card
      userLink.addEventListener("mouseenter", async (event) => {
        const username = userLink.href.split("/u/")[1];

        // Fetch user details
        const userInfo = await getUserInfo(username);

        // Format the date
        const date = new Date(userInfo.creationDate);
        const formattedDate = `${date.getFullYear()}/${String(
          date.getMonth() + 1
        ).padStart(2, "0")}/${String(date.getDate()).padStart(2, "0")}`;

        // Update the user card
        userCard.innerHTML = `
              <div class="header">
                  <img src="${
                    userInfo.profilePicture ||
                    `https://api.dicebear.com/6.x/identicon/svg?seed=${username}`
                  }" alt="User avatar">
                  <div>
                      <div class="username">${
                        userInfo.name || username.split("@")[0]
                      }</div>
                      <a href="https://${
                        userInfo.instance
                      }/u/${username}" class="instance">${username}${
          username.indexOf("@") === -1 ? "@" + userInfo.instance : ""
        }
                      </a>
                  </div>
              </div>
              <div class="body">
                  <div><span class="key">ID:</span> <span class="value">${
                    userInfo.id
                  }</span></div>
                  <div style="display:flex; flex-direction: column; gap: 3px"><span class="key">
                    <svg class="icon"><use xlink:href="/static/assets/symbols.svg#icon-cake"></use><div class="sr-only"><title>cake</title></div></svg>
                    Cake Day:</span> <span class="value">${formattedDate}</span></div>
                  <div><span class="key">Posts:</span> <span class="value">${
                    userInfo.post_count
                  }</span></div>
                  <div><span class="key">Comments:</span> <span class="value">${
                    userInfo.comment_count
                  }</span></div>
                  <div><span class="key">Post Score:</span> <span class="value">${
                    userInfo.post_score
                  }</span></div>
                  <div><span class="key">Comment Score:</span> <span class="value">${
                    userInfo.comment_score
                  }</span></div>
                  ${
                    userInfo.bio ? `<div class="bio">${userInfo.bio}</div>` : ""
                  }
              </div>`;

        // Show the user card at the cursor
        const rect = userLink.getBoundingClientRect();
        userCard.style.left = `${window.pageXOffset + rect.left}px`;
        userCard.style.top = `${window.pageYOffset + rect.bottom + 5}px`;
        // setTimeout(() => {
        if (userLink.querySelector(":hover")) {
          userCard.style.display = "block";
        }
        // }, 250);
        timer = setTimeout(() => {
          // check if username is not being hovered anymore after 150ms, after which point we must change display to none
          if (!userLink.querySelector(":hover")) {
            userCard.style.display = "none";
          }
        }, 150);
      });

      // When mouse leaves, hide the user card after a slight delay
      userLink.addEventListener("mouseleave", () => {
        // after a slight delay, delete the node
        timer = setTimeout(() => {
          // delete the node
          // userCard.parentElement.removeChild(userCard);
          userCard.style.display = "none";
        }, 250);
        setTimeout(() => {
          // check if both are unhovered after 260ms, and if that's the case, removeChild anyway
          if (!userCard.parentElement) return;
          if (!userCard.querySelector(":hover")) {
            // userCard.parentElement.removeChild(userCard);
            userCard.style.display = "none";
          }
        }, 250);

        // timer = setTimeout(() => {
        //   userCard.style.display = "none";
        // }, 250);
      });
    });

    userCard.addEventListener("mouseenter", () => {
      clearTimeout(timer);
    });

    userCard.addEventListener("mouseleave", () => {
      userCard.style.display = "none";
      // userCard.parentElement.removeChild(userCard);
    });

    // Fetch user info from the API
    async function getUserInfo(userName) {
      const instanceName = location.href.split("/")[2];
      const response = await fetch(
        `https://${instanceName}/api/v3/user?username=${userName}`,
        {
          method: "GET",
          headers: {
            "Content-Type": "application/json",
          },
        }
      );
      const user = await response.json();
      const {
        published: creationDate,
        avatar: profilePicture,
        bio,
        display_name: name,
        name: username,
        id,
        banner,
      } = user.person_view.person;
      const { comment_count, comment_score, post_count, post_score } =
        user.person_view.counts;

      return {
        creationDate,
        profilePicture,
        bio,
        name,
        username,
        id,
        banner,
        instance: instanceName,
        comment_count,
        comment_score,
        post_count,
        post_score,
      };
    }
  }

  // detect react changed url but didn't reload the page by checking for url change
  var oldHref = document.location.href;
  setInterval(function () {
    if (document.location.href !== oldHref) {
      oldHref = document.location.href;
      // Wait for the page to load
      setTimeout(main, 1000);
      console.log("url changed!");
    }
  }, 500);

  // run on page load
  main();
})();
77
 
 

cross-posted from: https://lemmy.ca/post/658611

IMO, the default Lemmy style is ridiculous on desktop (especially widescreen). I saw a few posts on other servers with some Stylus CSS changes and they were a good start. I made a few changes to get rid of some of the crazy font sizes, padding/margin, and width.

The CSS: https://pastebin.com/b71sNaRe

Stylus Chrome extenstion: https://chrome.google.com/webstore/detail/stylus/clngdbkpkpeebahjckkjfobafhncgmne

Screenshots: https://i.imgur.com/UdKigJD.png https://i.imgur.com/PBNb1SK.png

Hopefully this makes things more bearable for a few people. (If you use dark mode, just delete lines 10, 11, and 12.)

78
 
 

This extension helps users subscribed to Lemmy communities by adding an icon before mentions or URLs related to other communities. These mentions can be in the format “ [email protected] [email protected]” or through URLs like “https://lemmy.ml/c/memes”.

By clicking the icon, users are directed to the kbin URL of that community. However, there’s currently a bug preventing it from working on /kbin (ironically), and I’ve already reported the issue on Git.

I’m in the process of publishing the extension on Firefox and Chrome extension stores, along with making the source code available.

I would appreciate your input on whether you find this extension useful.

github link: https://github.com/driccio98/kbin-link

Works on lemmy instances. You specify your lemmy host in the preferences and it automatically adds a small icon before all addresses, both on communities and in threads/posts

Already a firefox extension https://addons.mozilla.org/en-US/firefox/addon/kbin-link/

it even added the icon to an address on github

79
 
 

Edit: @[email protected] has designed a better solution using only CSS, and this should be used instead of the old script! If you're reading this page for the first time, ignore this message.

This userstyle adds a red heart next to people that are from your home server, and any other servers that you manually define. Spot your server buddies out in the wild!

Instructions:

  1. Install Stylus extension for firefox/chrome

  2. "Write new style" in the addon settings

  3. Copy paste the CSS code below in

  4. Modify the code around line ~11 in order to reflect your homeserver and any additional frendservers that you want to highlight

  5. Modify the code around line ~19 to reflect your homeserver

  6. (Optional) If you’d like your homeserver buddies to have a different marker, uncomment the various sections around line ~27 through ~50 by removing the /* and */ bits

  7. (Optional) Play around with different markers and colors!

CSS/Userstyle: https://gist.github.com/redyoshi49q/f1b2d1da0a8f7536aba1f8c3110d2dd8

80
 
 

The developers will try to make it posible

81
 
 

cross-posted from: https://feddit.de/post/808717

I made a little thing to more easily move to another instance:

A way to quickly grab a list of your subscribed communities.

Go to your list of subscribed communities, /communities/listing_type/Subscribed/page/1 and create a bookmark with the following code as link:

code

javascript:(function() {
    const currentHostname = window.location.hostname;
    const table = document.getElementById('community_table');
    const anchorTags = table.getElementsByTagName('a');
    const communityUrls = [];

    for (let i = 0; i < anchorTags.length; i++) {
      const title = anchorTags[i].title.substring(1);
      const parts = title.split('@');
      const community = parts[0].trim();
      const domain = parts[1] ? parts[1].trim() : currentHostname;
      const communityUrl = `https://${domain}/c/${community}`;
      communityUrls.push(communityUrl);
    }

    const urlsText = communityUrls.join('\n');

    navigator.clipboard.writeText(urlsText)
      .then(() => {
        alert('Community URLs copied to clipboard!');
      })
      .catch((error) => {
        alert('Failed to copy Community URLs to clipboard:', error);
      });
})();
Clicking this bookmark will automatically copy a list of all your subscribed communities and format the links so that you can pop them in the search bar of another instance to subscribe to them from another account. It can only scrape what's on screen, so if your subscribed communities list is several pages long just click the bookmark for each page.
82
10
submitted 1 year ago* (last edited 1 year ago) by god to c/plugins
 
 

Credits: source

Change Source Author
Original CSS comment#610 ___
Original userscript comment#902 yay
Userscript URL fix comment#2219 god
Final / Affects player size comment#21062 FreePussy4All

original css: by https://lemmynsfw.com/u/___

original userscript: https://lemmynsfw.com/comment/610 by my tiny fix: me

script:

// ==UserScript==
// @name lemmynsfw no blur and embed size tweak
// @version 1.2
// @description unblur
// @match https://lemmynsfw.com/*
// @match https://*/c/*@lemmynsfw.com
// @grant GM_addStyle
// @run-at document-start
// ==/UserScript==

GM_addStyle(`
  .img-blur {
    filter: none !important;
    -webkit-filter: none !important;
    -moz-filter: none !important;
    -o-filter: none !important;
    -ms-filter: none !important;
  }
  div.post-listing > iframe {
    width: 100%;
    height: 26rem;
  }
`);
83
 
 

userscript called "old.reddit" found here: https://github.com/soundjester/lemmy_monkey

  • (recently updated for Lemmy v0.18)

  • original thread link here

  • This is primarily for desktop clients. At the moment, formatting get a little crazy below 1280 px wide. There are ways to address this, but I have not at this time.

  • script will be updated as suggested

    • significant changes have been made to address alignment, spacing, and other format issues. v1.1 will be where I stop for a while.
  • there are two script versions: old.reddit and old.reddit.compact. The primary difference is that the "compact" version greatly reduces thumbnail size and padding space.

  • notice: current script unblurs NSFW

(linked thumbnail shows old.reddit.compact version of the script)

Screenshot of old.reddit script results:

84
 
 

Original post, containing the original code @[email protected] made: https://sh.itjust.works/post/33762

his original message:

For those that use Tampermonkey or GreaseMonkey, here’s a quick script I whipped up that creates a button that redirects communities from other instances to your local instance. If your instance isn’t lemmy.world, then change the localLemmy var at the top. Enjoy!

My small fix:

For personal use, I changed localLemmy to sh.itjust.works because that's my instance. I also embedded some styles and made it so it's top right instead of bottom left, and so that it's red and works better for me. Soy's original code didn't work for me so I had to make these small alterations to get it working.

// ==UserScript==
// @name         Lemmings Fix
// @version      1.0
// @description  Redirect to your local Lemmy instance
// @author       @lemmy.world/u/soy
// @match        https://*/c/*
// @icon         https://join-lemmy.org/static/assets/icons/favicon.svg
// ==/UserScript==

const localLemmy = "sh.itjust.works";
var isLemmy =
  document.head.querySelector("[name~=Description][content]").content ===
  "Lemmy";

if (isLemmy) {
  // Get URL info
  var splitUrl = location.href.split("/");
  var instanceUrl = splitUrl[2];
  var community = splitUrl[4];
  var localizedUrl =
    "https://" + localLemmy + "/c/" + community + "@" + instanceUrl;

  // Create redirect button if not on local
  if (instanceUrl !== localLemmy) {
    var zNode = document.createElement("div");
    zNode.innerHTML = "Open in local instance";
    zNode.setAttribute("id", "localizeContainer");
    // add styles to the button embedded
    zNode.setAttribute(
      "style",
      "cursor: pointer; padding: 16px; background-color: red; border-radius: 10%; border-width: 3px; border-style: solid; padding: 3px; z-index: 10;max-width:200px;position:fixed;top:0;right:0;"
    );
    zNode.addEventListener("click", onLocalize);
    document.body.appendChild(zNode);
  }
}

function onLocalize() {
  window.location.replace(localizedUrl);
}
85
6
submitted 1 year ago* (last edited 1 year ago) by god to c/plugins
 
 

Clarifications

  1. This works most of the time. Sometimes it doesn't. Needs a bit more testing.
  2. the new version of Lemmy (link to PR) will have a few shortcuts, and this userscript will be deprecated. But until then, I will keep using it.

Original post:

cross-posted from: https://sh.itjust.works/post/42893

Decided to make a little script that listens to textareas in post & comments, and if you press Ctrl Enter while focusing on them, it submits them. I use this to post comments faster and with less bother. It's reminiscent of the RES feature to do the same thing.

If you use Greasemonkey or Tampermonkey, you can install this easily and instantly have CtrlEnter to submit. Let me know any improvements I can make.

// ==UserScript==
// @name         Lemmy Form Submit with Ctrl+Enter
// @version      1.0
// @description  Submit forms with Ctrl+Enter in Lemmy instances so you don't have to click the button every time you want to post something.
// @author       God (https://sh.itjust.works/u/god)
// @match        https://*/post/*
// @match        https://*/comment/*
// @icon         https://join-lemmy.org/static/assets/icons/favicon.svg
// ==/UserScript==

var isLemmy =
  document.head.querySelector("[name~=Description][content]").content ===
  "Lemmy";

if (isLemmy) {
  // Define a global variable to keep track of the currently focused textarea.
  var currentFocusedTextarea = null;

  // Function to attach focus and blur event handlers to all textareas.
  function attachEventHandlers() {
    document.querySelectorAll("textarea").forEach((textarea) =&gt; {
      if (!textarea.dataset.ctrlEnterHandled) {
        textarea.dataset.ctrlEnterHandled = true;

        // Check if this textarea is currently focused
        const wasFocused = document.activeElement === textarea;

        textarea.addEventListener("focus", function () {
          currentFocusedTextarea = this;
        });

        textarea.addEventListener("blur", function () {
          currentFocusedTextarea = null;
        });

        // If this textarea was focused, blur and re-focus it to ensure event handlers get triggered
        if (wasFocused) {
          textarea.blur();
          textarea.focus();
        }
      }
    });
  }

  // Attach a keydown event handler to the entire document.
  document.addEventListener("keydown", function (event) {
    // If Ctrl + Enter is pressed
    if (event.ctrlKey &amp;&amp; event.key === "Enter") {
      // If a textarea is focused and contains text
      if (
        currentFocusedTextarea &amp;&amp;
        currentFocusedTextarea.value.trim() !== ""
      ) {
        // Your submit logic here
        handleSubmit(currentFocusedTextarea);
      }
    }
  });

  function handleSubmit(textarea) {
    // find the closest type="submit" button and press it.
    textarea.closest("form").querySelector('[type="submit"]').click();
  }

  // Call the function initially to cover textareas that exist when the page is first loaded.
  attachEventHandlers();

  // Observe the document for changes and reattach event handlers when new textareas are added.
  const observer = new MutationObserver(function (mutations) {
    mutations.forEach((mutation) =&gt; {
      if (mutation.type === "childList") {
        attachEventHandlers();
      }
    });
  });

  observer.observe(document.body, { childList: true, subtree: true });
}