维基百科自动跳转到简体中文桌面版页面

维基百科页面自动跳转,自动跳转到简体中文桌面版页面

// ==UserScript==
// @name         维基百科自动跳转到简体中文桌面版页面
// @namespace    https://greasyfork.org/zh-CN/scripts/451260
// @version      0.1.0
// @description  维基百科页面自动跳转,自动跳转到简体中文桌面版页面
// @author       Phuker
// @license      GNU GPLv3
// @include      https://zh.wikipedia.org/*
// @include      https://zh.m.wikipedia.org/*
// ==/UserScript==

(function() {
    'use strict';

    let hostname_replace = 'zh.wikipedia.org';
    let lang_search = [
        'wiki',
        'zh',
        'zh-hans',
        'zh-hant',
        'zh-hk',
        'zh-mo',
        'zh-my',
        'zh-sg',
        'zh-tw',
    ];
    let lang_replace = 'zh-cn';

    function my_log(...args) {
        var _datetime_str = (new Date()).toLocaleString();
        console.log('[Wikipedia auto redirect][' + _datetime_str + ']', ...args);
    }
    
    function auto_redirect() {
        let url = new URL(document.location.href);
        url.hostname = hostname_replace;
    
        let pathname_parts = url.pathname.split('/');
        if(lang_search.indexOf(pathname_parts[1]) >= 0) {
            pathname_parts[1] = lang_replace;
            url.pathname = pathname_parts.join('/');
        }
    
        if(document.location.href != url.href) {
            my_log('Redirect', document.location.href, '-->', url.href);
            document.location.href = url.href;
        }
    }
    
    my_log('Script running');
    auto_redirect();
})();