Redirect to zh-tw version of Moegirlpedia or Wikipedia

Redirect to zh-tw version page of Mandarin Moegirlpedia or Mandarin Wikipedia

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name               Redirect to zh-tw version of Moegirlpedia or Wikipedia
// @name:zh-TW         跳轉至台灣正體版的萌娘百科或維基百科
// @name:ja            萌えっ娘百科事典とウィキペディアで台湾正体版にリダイレクト
// @description        Redirect to zh-tw version page of Mandarin Moegirlpedia or Mandarin Wikipedia
// @description:zh-TW  跳轉至華文萌娘百科或華文維基百科的台灣正體版頁面
// @description:ja     華文版の萌えっ娘百科事典と華文版のウィキペディアで台湾正体版のページにリダイレクトするユーザースクリプトです
// @namespace          pediazhtw
// @version            1.0.1
// @match              https://zh.moegirl.org.cn/*
// @match              https://zh.wikipedia.org/*
// @match              https://zh.m.wikipedia.org/*
// @run-at             document-start
// @author             lazy fox chan
// @license            MIT
// ==/UserScript==

(function() {
    'use strict';

    const target = "zh-tw";
    const regExpSubDir = /(\/wiki\/|\/zh\/|\/zh-hans\/|\/zh-hant\/|\/zh-cn\/|\/zh-hk\/|\/zh-mo\/|\/zh-my\/|\/zh-sg\/)/g;
    const targetSubDir = "/zh-tw/";
    const regExpGetParam = /variant=(zh(?!(-hans|-hant|-cn|-hk|-mo|-my|-sg|-tw))|zh-hans|zh-hant|zh-cn|zh-hk|zh-mo|zh-my|zh-sg)/g;
    const targetGetParam = "variant=zh-tw";

    // Get the current URL
    var currentUrl = window.location.href;

    // If already on zh-tw page and not inculude other language codes in URL, do nothing
    if (regExpSubDir.test(currentUrl) || regExpGetParam.test(currentUrl) || currentUrl.indexOf(target) === -1) {

        // Replace other language codes to zh-tw
        var newUrl = currentUrl.replace(regExpSubDir, targetSubDir).replace(regExpGetParam, targetGetParam);

        // If not inculude language codes in URL, Add GET parameter to URL
        if (newUrl.indexOf(targetSubDir) === -1 && newUrl.indexOf(targetGetParam) === -1) {
          if (newUrl.indexOf("?") === -1) {
            newUrl = newUrl + "?" + targetGetParam;
          }else{
            newUrl = newUrl + "&" + targetGetParam;
          }
        }

        // Redirect
        window.location.replace(newUrl);

    }
})();