• 0 Posts
  • 17 Comments
Joined 1 year ago
cake
Cake day: June 6th, 2023

help-circle













  • 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);
        }
    }