Medium to Scribe Redirector

Redirect Medium articles to Scribe for free reading

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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         Medium to Scribe Redirector
// @namespace    https://scribe.rip/
// @version      1.0
// @description  Redirect Medium articles to Scribe for free reading
// @author       Arkina Romeon
// @match        *://medium.com/*
// @match        *://*.medium.com/*
// @exclude      *://medium.com/
// @exclude      *://*.medium.com/
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // Check if current URL is a Medium article page
    function isMediumArticle(url) {
        // Medium articles typically follow these patterns:
        // - medium.com/@username/article-title-hash
        // - username.medium.com/article-title-hash
        const mediumPattern = /(?:medium\.com\/[^\/]+\/[^\/]+|\.medium\.com\/[^\/]+)/;
        return mediumPattern.test(url);
    }

    // Convert Medium URL to Scribe URL
    function convertToScribeUrl(mediumUrl) {
        try {
            const url = new URL(mediumUrl);
            let scribeUrl = 'https://scribe.rip' + url.pathname;
            
            // Preserve query parameters (if any)
            if (url.search) {
                scribeUrl += url.search;
            }
            
            // Preserve hash fragment (if any)
            if (url.hash) {
                scribeUrl += url.hash;
            }
            
            return scribeUrl;
        } catch (e) {
            console.error('Error converting URL:', e);
            return null;
        }
    }

    // Main function
    function main() {
        const currentUrl = window.location.href;
        
        // Check if it's an article page (exclude homepage)
        if (isMediumArticle(currentUrl) && 
            !currentUrl.includes('/me/') && 
            !currentUrl.includes('/new-story') &&
            !currentUrl.includes('/edit/') &&
            !currentUrl.includes('/draft/')) {
            
            const scribeUrl = convertToScribeUrl(currentUrl);
            
            if (scribeUrl && scribeUrl !== currentUrl) {
                console.log(`Redirecting from Medium to Scribe: ${scribeUrl}`);
                window.location.replace(scribeUrl);
            }
        }
    }

    // Execute redirect check immediately
    main();

    // Listen for URL changes (for SPAs)
    let lastUrl = location.href;
    new MutationObserver(() => {
        const currentUrl = location.href;
        if (currentUrl !== lastUrl) {
            lastUrl = currentUrl;
            setTimeout(main, 100); // Small delay to ensure DOM updates
        }
    }).observe(document, { subtree: true, childList: true });

})();