Original post, containing the original code @soy@lemmy.world 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);
}
  • flauschke@feddit.de
    link
    fedilink
    English
    arrow-up
    2
    ·
    edit-2
    1 year ago

    I’ve adapted it to work on kbin - let me know if anyone finds any issues

    // ==UserScript==
    // @name         kbin lemmy redirect
    // @version      1.0
    // @description  Redirect kbin to your local Lemmy instance
    // @author       @flauschke@feddit.de
    // @match        https://*/m/*
    // @icon         https://join-lemmy.org/static/assets/icons/favicon.svg
    // ==/UserScript==
    
    // best effort guess
    const isKbin = typeof KBIN_USER !== 'undefined' && typeof KBIN_MAGAZINE !== 'undefined';
    
    if (isKbin) {
        // Get URL info
        const localLemmy = "feddit.de";
        const splitUrl = location.href.split("/");
        const instanceUrl = splitUrl[2];
        const community = splitUrl[4];
        const localizedUrl = "https://" + localLemmy + "/c/" + community + "@" + instanceUrl;
    
        // Create redirect button if not on local
        if (instanceUrl !== localLemmy) {
            const wrapper = document.createElement("div");
            wrapper.setAttribute("style", "width: 100%;position: fixed;top: 0;")
    
            const container = document.createElement("div");
            container.setAttribute("style", "position:relative;max-width: 1650px;width: 100%;margin: 0 auto;")
            const zNode = document.createElement("button");
            zNode.innerText = "Open in local instance";
            zNode.setAttribute("id", "localizeContainer");
            // add styles to the button embedded
            zNode.setAttribute(
                "style",
                "cursor: pointer; padding: 5px; margin-top: 55px; background-color: red; border-radius: 10%; border-width: 3px; border-style: solid; z-index: 10;max-width:200px;position:absolute;top:0;right:0;"
            );
            zNode.addEventListener("click", e => window.location.replace(localizedUrl));
            container.appendChild(zNode);
            wrapper.appendChild(container);
            document.body.appendChild(wrapper);
        }
    }
    
    • God@sh.itjust.worksOPM
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 year ago

      And God rested on the seventh day or something. I didn’t sleep today from so much Lemmying. I should sleep soon or perish.

  • Paneer@burggit.moe
    link
    fedilink
    English
    arrow-up
    2
    ·
    1 year ago

    Thank you for this! This works even on mobile. Download Iceraven, which is a fork of Firefox for Android that supports extensions like GreaseMonkey.

    • God@sh.itjust.worksOPM
      link
      fedilink
      English
      arrow-up
      2
      ·
      1 year ago

      nice, i use kiwi and it also accepts extensions, but since i don’t use mobile as much and i use it differently i have not needed the extensions

  • SilentStorms@lemmy.fmhy.ml
    link
    fedilink
    English
    arrow-up
    2
    ·
    edit-2
    1 year ago

    Thanks for this script! With next to no scripting experience, I managed to edit this to cover the other instance’s logo with the one from mine. img

    • God@sh.itjust.worksOPM
      link
      fedilink
      English
      arrow-up
      2
      ·
      1 year ago

      Hahah, not the most respectful thing to do but that’s okay c: glad you got it working the way you wanted.

      • SilentStorms@lemmy.fmhy.ml
        link
        fedilink
        English
        arrow-up
        2
        ·
        1 year ago

        Oh I didn’t even consider that it might be disrespectful, I just wanted it to blend in :(. I was trying to get it to be an icon on the navbar, but I couldn’t figure out how to make it not snap to a corner lol.

        I do browse other instances a lot by visiting their homepage, so I’m not totally missing the logos.

        • God@sh.itjust.worksOPM
          link
          fedilink
          English
          arrow-up
          2
          ·
          1 year ago

          That’s all good 😎 just browse to your own liking! Nothing matters, god is dead, morality is a spook, Epstein killed himself and Santa killed all the elves