KBin Redirect dev

redirects Lemmy shortlinks to the appropriate page on KBin

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         KBin Redirect dev
// @namespace    http://herzberd.dev/
// @version      0.1.3
// @description  redirects Lemmy shortlinks to the appropriate page on KBin
// @author       @[email protected] https://github.com/herzberd https://herzberd.dev/
// @match        https://*.kbin.social/*
// @match        https://*.fedia.io/*
// @match        https://*.karab.in/*
// @match        https://*.readit.buzz/*
// @match        https://*.open-source.social/*
// @match        https://*.forum.fail/*
// @match        https://*.fedi196.gay/*
// @match        https://*.kbin.cafe/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=kbin.social
// @grant        none
// @license      CC0 1.0 Universal
// ==/UserScript==

'use strict';
const bangPattern = /!([A-Za-z0-9_]+)@([A-Za-z0-9.-]+)\b/g
const cSlashPattern = /^\/c\/([A-Za-z0-9_]+)(@([A-Za-z0-9.-]+))?$/g;
const linkRegex = /^https?:\/\/([^/]+)\/c\/(\w+)$/;

var elements =  document.getElementsByTagName('a')
var mediaLinks = document.getElementsByClassName('kbin-media-link')

for (var i = 0; i < elements.length; i++) {
    const matches = [...elements[i].innerText.matchAll(bangPattern)]
    const cSlashMatches = [...elements[i].innerText.matchAll(cSlashPattern)]
    if (matches.length > 0) {
        matches.forEach((match) => {
            const topic = match[1]
            const domain = match[2]
            elements[i].href = '/m/' + topic + '@' + domain
        })
    }
    else if (cSlashMatches.length > 0) {
        cSlashMatches.forEach((match) => {
            const topic = match[1]
            const domain = match[2]
            elements[i].href = '/m/' + topic + '@' + domain
        })
    }
}


console.log("CHECK FOR EXISTING LINKS")
// if already linked, match
for(var j = 0; j < mediaLinks.length; j++){
    const checkHref = mediaLinks[j].href
    const newMatch = checkHref.match(linkRegex)
    if(newMatch && newMatch.length > 0){
        const newLink = "/m/" + newMatch[2] + "@" + newMatch[1]
        mediaLinks[j].href = newLink
    }
}