Vector Layout for Wikipedia (Fast)

returns old Wikipedia layout. (layout before 2023 redesign of the website)

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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 Vector Layout for Wikipedia (Fast)
// @namespace -
// @version 1.1.13
// @description returns old Wikipedia layout. (layout before 2023 redesign of the website)
// @author NotYou
// @match *://wikipedia.org/*
// @match *://*.wikipedia.org/*
// @match *://wiktionary.org/*
// @match *://*.wiktionary.org/*
// @match *://wikinews.org/*
// @match *://*.wikinews.org/*
// @match *://wikivoyage.org/*
// @match *://*.wikivoyage.org/*
// @match *://wikiquote.org/*
// @match *://*.wikiquote.org/*
// @match *://wikiversity.org/*
// @match *://*.wikiversity.org/*
// @match *://wikibooks.org/*
// @match *://*.wikibooks.org/*
// @match *://wikifunctions.org/*
// @match *://*.wikifunctions.org/*
// @match *://www.mediawiki.org/*
// @match *://wikitech.wikimedia.org/*
// @match *://foundation.wikimedia.org/*
// @match *://meta.wikimedia.org/*
// @match *://species.wikimedia.org/*
// @match *://incubator.wikimedia.org/*
// @match *://outreach.wikimedia.org/*
// @match *://wikimania.wikimedia.org/*
// @run-at document-start
// @license GPL-3.0-or-later
// @grant none
// ==/UserScript==

(function() {
    'use strict';

    const MAKE_CLEAN_URL = false; // removes "useskin=vector" after loading

    const IS_DEBUG_MODE = false; // instead of redirecting, logs information in console

    const DEBUG_TITLE = 'VLfW — Debug\n';
    const { href } = location;

    redirect(href, false, () => {
        if(MAKE_CLEAN_URL) {
            const url = new URL(href);
            const { searchParams, pathname } = url;

            if(searchParams.get('useskin') === 'vector') {
                searchParams.delete('useskin');

                const newSearchParams = searchParams.toString();
                const newPath = pathname + (newSearchParams ? '?' + newSearchParams : newSearchParams);

                history.replaceState({}, '', newPath);
            }
        }
    });

    window.addEventListener('click', onClick);

    function redirect(inputUrl, saveHistory, onFinish) {
        let url;

        try {
            url = new URL(inputUrl);
        } catch(e) {
            throw new Error('"' + inputUrl + '" is not valid URL!');
        }

        const { searchParams, pathname, origin } = url;
        const cleanURL = origin + pathname;

        if(searchParams.get('useskin') !== 'vector' && url.pathname !== '/') {
            searchParams.set('useskin', 'vector');

            const params = '?' + searchParams.toString();

            const resultURL = cleanURL + params;
            const newPath = pathname + params;

            if(IS_DEBUG_MODE) {
                console.log(DEBUG_TITLE, resultURL, newPath);
            } else {
                replaceURL(resultURL, saveHistory);
            }
        }

        if (typeof onFinish === 'function') {
            onFinish()
        }
    }

    function onClick(e) {
        const node = e.target;
        const link = getLink(node);

        if(link && !(e.ctrlKey || e.metaKey)) {
            const url = new URL(link.href);
            const isOrigin = url.hostname.indexOf('wikipedia.org') > -1;
            const isNotAnchor = !link.getAttribute('href').startsWith('#');
            const isOnlyLink = !link.getAttribute('role');

            if(isOrigin && isNotAnchor && isOnlyLink) {
                e.preventDefault();

                redirect(url, false);
            }
        }
    }

    function replaceURL(url, saveHistory) {
        if(saveHistory) {
            location.assign(url);
        } else {
            location.replace(url);
        }
    }

    function getLink(node) {
        if(node.tagName === 'A') {
            return node;
        } else if(node.tagName === 'HTML' || !node.tagName) {
            return null;
        }

        return getLink(node.parentNode);
    }
})();