Redirect to zh-tw version of Moegirlpedia or Wikipedia

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

  1. // ==UserScript==
  2. // @name Redirect to zh-tw version of Moegirlpedia or Wikipedia
  3. // @name:zh-TW 跳轉至台灣正體版的萌娘百科或維基百科
  4. // @name:ja 萌えっ娘百科事典とウィキペディアで台湾正体版にリダイレクト
  5. // @description Redirect to zh-tw version page of Mandarin Moegirlpedia or Mandarin Wikipedia
  6. // @description:zh-TW 跳轉至華文萌娘百科或華文維基百科的台灣正體版頁面
  7. // @description:ja 華文版の萌えっ娘百科事典と華文版のウィキペディアで台湾正体版のページにリダイレクトするユーザースクリプトです
  8. // @namespace pediazhtw
  9. // @version 1.0.1
  10. // @match https://zh.moegirl.org.cn/*
  11. // @match https://zh.wikipedia.org/*
  12. // @match https://zh.m.wikipedia.org/*
  13. // @run-at document-start
  14. // @author lazy fox chan
  15. // @license MIT
  16. // ==/UserScript==
  17.  
  18. (function() {
  19. 'use strict';
  20.  
  21. const target = "zh-tw";
  22. const regExpSubDir = /(\/wiki\/|\/zh\/|\/zh-hans\/|\/zh-hant\/|\/zh-cn\/|\/zh-hk\/|\/zh-mo\/|\/zh-my\/|\/zh-sg\/)/g;
  23. const targetSubDir = "/zh-tw/";
  24. 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;
  25. const targetGetParam = "variant=zh-tw";
  26.  
  27. // Get the current URL
  28. var currentUrl = window.location.href;
  29.  
  30. // If already on zh-tw page and not inculude other language codes in URL, do nothing
  31. if (regExpSubDir.test(currentUrl) || regExpGetParam.test(currentUrl) || currentUrl.indexOf(target) === -1) {
  32.  
  33. // Replace other language codes to zh-tw
  34. var newUrl = currentUrl.replace(regExpSubDir, targetSubDir).replace(regExpGetParam, targetGetParam);
  35.  
  36. // If not inculude language codes in URL, Add GET parameter to URL
  37. if (newUrl.indexOf(targetSubDir) === -1 && newUrl.indexOf(targetGetParam) === -1) {
  38. if (newUrl.indexOf("?") === -1) {
  39. newUrl = newUrl + "?" + targetGetParam;
  40. }else{
  41. newUrl = newUrl + "&" + targetGetParam;
  42. }
  43. }
  44.  
  45. // Redirect
  46. window.location.replace(newUrl);
  47.  
  48. }
  49. })();