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!

// ==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
// @grant        GM_addStyle
// ==/UserScript==

const localLemmy = 'lemmy.world';
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 = '<button id="localize" type="button">Open in local instance</button>';
        zNode.setAttribute('id', 'localizeContainer');
        document.body.appendChild(zNode);
        document.getElementById ("localize").addEventListener (
            "click", onLocalize
        );
    }
}

function onLocalize (zEvent) {
    window.location.replace(localizedUrl);
}

GM_addStyle ( `
    #localizeContainer {
        position: fixed;
        right: 0px;
        bottom: 0px;
    }
    #localize {
        cursor: pointer;
        padding: 16px;
    }
` );

  • Kris@lemmy.world
    link
    fedilink
    English
    arrow-up
    5
    ·
    1 year ago

    I wish lemmy instances had an indicator for if that instance is lemmy instead of using the url.

  • God
    link
    fedilink
    English
    arrow-up
    4
    ·
    1 year ago

    Henlo buddy, your script didn’t work for me, so I fixed it. Works for me now. I also changed lemmy.world to sh.itjust.works so beware.

    Here’s my final one:

    // ==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);
    }
    
  • meli nasa@feddit.de
    link
    fedilink
    English
    arrow-up
    2
    ·
    edit-2
    1 year ago

    Hah, just came here to suggest an improvement, but the script has already transformed completely while I was gone :D

  • TCGM@lemmy.world
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    If someone wouldn’t mind throwing this up on, say, userforks or similar, that would be great.

  • God@lemmy.world
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    i made a new userscript for submitting forms with Ctrl + Enter. Is that something you’d find useful?

    // ==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) => {
          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 && event.key === "Enter") {
          // If a textarea is focused and contains text
          if (
            currentFocusedTextarea &&
            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) => {
          if (mutation.type === "childList") {
            attachEventHandlers();
          }
        });
      });
    
      observer.observe(document.body, { childList: true, subtree: true });
    }
    
  • God
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    i made a new userscript for submitting forms with Ctrl + Enter. Is that something you’d find useful?

    // ==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) => {
          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 && event.key === "Enter") {
          // If a textarea is focused and contains text
          if (
            currentFocusedTextarea &&
            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) => {
          if (mutation.type === "childList") {
            attachEventHandlers();
          }
        });
      });
    
      observer.observe(document.body, { childList: true, subtree: true });
    }