Lemmings Fix

Redirect to your local Lemmy instance

< 脚本Lemmings Fix的反馈

提问/评论

§
发表于:2023-06-16

I modified the script a lot:

  • Removes the big obvious button, replaces the ![community] links instead
    • on mouseenter to get around the elements getting reset by something after loading
  • It works even if the url shown by the browser hasn't changed yet (weird)
  • I've only tested it on Firefox
  • Use it if you want.
// ==UserScript==
// @name         Lemmings Fix
// @version      1.0
// @description  Redirect to your local Lemmy instance
// @author       @lemmy.world/u/soy @beehaw.org/u/abclop99
// @match        https://*/c/*
// @icon         https://join-lemmy.org/static/assets/icons/favicon.svg
// ==/UserScript==

// URL of your local lemmy instance
const localLemmy = "beehaw.org";

// Check html metadata description
var isLemmy =
  document.head.querySelector("[name~=Description][content]").content ===
  "Lemmy";

if (isLemmy) {
  // Create url to local instance
  var splitUrl = location.href.split("/");
  var instanceUrl = splitUrl[2];
  var community = splitUrl[4];
  var localizedUrl = createLocalizedUrl(localLemmy, community)

  // Create redirect button if not on local
  if (instanceUrl !== localLemmy) {
    // Selector for links
    // a[href="/c/community"]g
    var querySelector = 'a[href="/c/' + community + '"]';

    // Get elements
    var elements = document.querySelectorAll (querySelector);

    // Add event listener to change link when the mouse enters the element
    // This is so the link does not get overwritten by other js
    elements.forEach(e => e.addEventListener(
      "mouseenter",
      () => e.setAttribute("href", localizedUrl),
    ))
  }
}

function createLocalizedUrl(localLemmy, community) {
  var localizedUrl =
    "https://" + localLemmy + "/c/" + community + "@" + instanceUrl;

  return localizedUrl;
}

发表回复

登录(不可用)以发表回复。

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址