Read this post and wrote a simple Tampermonkey script as a solution.

// ==UserScript==
// @name         Fix community link
// @version      0.1
// @description  try to take over the world!
// @match        https://sh.itjust.works/post/*
// ==/UserScript==

(function() {
    'use strict';
    const postLinks = document.getElementById("postContent").querySelectorAll("a:not(.community-link)") // get every links that is NOT a community link

    const fixLink = (aTags) => {
        for (let aTag of aTags) {
            const isCommunityLink = aTag.pathname.startsWith("/c/");
            aTag.href = isCommunityLink?aTag.pathname + "@" + aTag.host:aTag.href
        };
    }

    fixLink(postLinks)

    const comments = document.getElementsByClassName("comment-content");

    for (let comment of comments) {
        let commentLinks = comment.querySelectorAll("a:not(.community-link)");
        fixLink(commentLinks)
    }
})();

Any advice? I especially hate the fact that the way to check if it's a link for lemmy community is through pathname but I thought there's can't be a real solution besides listing all the lemmy instances or actually making a request somehow.

Any inputs are welcome!

  • Cynber
    link
    fedilink
    9
    edit-2
    9 months ago

    We're actually working on a browser extension for this! It currently supports both communities and posts

    !Instance_Assistant@lemmy.ca

    We ran into the same issue, federated sites are hard to work with. Right now, the extension has it so that a user needs to right click on a link to be redirected. That way the user can choose which links get redirected, and there's no chance of accidentally redirecting the wrong thing.

    There are other solutions (using the API for example), but they seemed to slow the browser down too much. Another proposed feature that hasn't been implemented yet was to redirect when holding down a key (when holding down "r", try to redirect the link).

    Feel free to take a look, try it, and you can totally contribute code. It's all open source and we've tried to keep the code simple and easy to verify/contribute.

    • @BeanCounter@sh.itjust.worksOP
      link
      fedilink
      English
      29 months ago

      I am not that familiar with browser extensions dev enough to contribute rn but that looks awesome. If there's any chance, I'll definitely contribute!

      • Cynber
        link
        fedilink
        59 months ago

        Sounds good! This was my first dive into browser extensions as well. It's not too bad once you go over the basics. If you give it a try, see the contributing page on the repo's wiki for some resources on how to get started with browser extensions.

        A super short summary is:

        • manifest.json is the entry point, it links to HTML files (which represent things like the popup, sidebar) and scripts (which do most of the work)
        • the background script runs all the time (see background.js), and the content scripts run on specific pages (ex. There's one for Lemmy community pages, one for error pages)

        If you DO give it a try, we were part way through migrating features from the LemmyTools userscript and that might be a good place to start. I wasn't familiar with userscripts so I didn't make much progress, and can't get back to it for a little while. The issues page of the repo should have LemmyTools related features tagged. If any details are missing, let me know and I'll add them in :)