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);
}
  • @[email protected]
    link
    fedilink
    English
    51 year ago

    This is awesome and removes a lot of the stumbling that I’ve been doing in the federated world. Thank you!

  • @Lyxea
    link
    English
    51 year ago

    God´s work with this scripts,keep working guys!

    • GodOPM
      link
      fedilink
      English
      51 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.

      • @Lyxea
        link
        English
        31 year ago

        AHAHHA rest well and thanks for the work XD

  • @[email protected]
    link
    fedilink
    English
    3
    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       @[email protected]
    // @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);
        }
    }
    
  • @boo
    link
    English
    31 year ago

    Thanks! This is the exact script that I was hoping to find!

    • GodOPM
      link
      fedilink
      English
      31 year ago

      easily one of the things i use the most 😎 if not THE one

  • @[email protected]
    link
    fedilink
    English
    3
    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

    • GodOPM
      link
      fedilink
      English
      31 year ago

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

      • @[email protected]
        link
        fedilink
        English
        31 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.

        • GodOPM
          link
          fedilink
          English
          21 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