Vector Layout for Wikipedia (Fast)

returns old wikipedia layout. (layout before 2023 redesign of website)

目前為 2023-02-04 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name Vector Layout for Wikipedia (Fast)
// @namespace -
// @version 1.1.3
// @description returns old wikipedia layout. (layout before 2023 redesign of website)
// @author NotYou
// @match *://wikipedia.org/*
// @match *://*.wikipedia.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 = 'DEBUG - Vector Layout for Wikipedia\n';

    let href = location.href;

    redirect(href);

    window.addEventListener('click', onClick);

    if(MAKE_CLEAN_URL) {
        let url = new URL(href);
        let searchParams = new URLSearchParams(url.search);

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

            let _searchParams = searchParams.toString();

            let newPath = url.pathname + (_searchParams ? '?' + _searchParams : _searchParams);

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

    function redirect(inputUrl) {
        let url;

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

        let searchParams = new URLSearchParams(url.search);
        let pathname = url.pathname;
        let cleanURL = url.origin + pathname;

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

            let hash = location.hash;
            let params = '?' + searchParams.toString();
            let props = params + hash;

            let resultURL = cleanURL + props;
            let newPath = pathname + props;

            if(IS_DEBUG_MODE) {
                console.log(DEBUG_TITLE, resultURL, newPath);
            } else {
                history.pushState({}, '', newPath);
                location.replace(resultURL);
            }
        }
    }

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

        if(link) {
            let url = new URL(link.href);
            let isValidLink = url.hostname.indexOf('wikipedia.org') > -1 && !link.getAttribute('href').startsWith('#'); // IF url contains wikipedia.org in host name AND url is NOT anchor

            if(isValidLink) {
                e.preventDefault();

                redirect(url);
            }
        }
    }

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

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