Redirect to zh-tw version of Moegirlpedia or Wikipedia

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

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);

    }
})();