跳轉至台灣正體版的萌娘百科或維基百科

跳轉至華文萌娘百科或華文維基百科的台灣正體版頁面

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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);

    }
})();