Wiki Randomizer

Press a key to view a random page on current Wiki.

  1. // ==UserScript==
  2. // @name Wiki Randomizer
  3. // @namespace https://script.zgc.im/
  4. // @version 1.7.2
  5. // @description Press a key to view a random page on current Wiki.
  6. // @author MidAutumnMoon
  7. //
  8. // @include /^.*://.*\.wiki(pedia|voyage|source|books)\.org/.*$/
  9. // @match *://*.wiktionary.org/*
  10. // @include /^.*://wiki\.(archlinux|debian)\.org/.*$/
  11. // @include /^.*://.*\.wikihow\.(com|pet)/.*$/
  12. // @match *://*.fandom.com/*
  13. // @match *://*.moegirl.org.cn/*
  14. // @match *://tcrf.net/*
  15. // @match *://*.rosettacode.org/*
  16. // @match *://esolangs.org/*
  17. // @match *://*.uesp.net/*
  18. // @match *://thwiki.cc/*
  19. //
  20. // @icon https://zh.wikipedia.org/favicon.ico
  21. // @grant none
  22. // ==/UserScript==
  23.  
  24. //
  25. // Issues and requests: https://gitlab.com/MidAutumnMoon/butterpotato/
  26. //
  27.  
  28. // The default trigger key
  29. const THAT_KEY = 'F8';
  30.  
  31.  
  32. const MediawikiCommon = 'Special:Random';
  33. const MediawikiRootpageCommon = 'Special:RandomRootpage';
  34. const MoinMoinCommon = 'RandomPage';
  35. const WikiHowCommon = 'Special:Randomizer'
  36.  
  37. // The total rules
  38. const RULES = new Map([
  39. // All about Wikipedia
  40. [ 'wikipedia.org', `wiki/${MediawikiCommon}` ],
  41. [ 'wikivoyage.org', `wiki/${MediawikiCommon}` ],
  42. [ 'wikisource.org', `wiki/${MediawikiCommon}` ],
  43. [ 'wikibooks.org', `wiki/Special:RandomInCategory/Book:Wikibooks_Stacks/Books` ],
  44. [ 'wiktionary.org', `wiki/${MediawikiCommon}` ],
  45.  
  46. // Cutting Room Floor
  47. [ 'tcrf.net', MediawikiRootpageCommon ],
  48.  
  49. // 萌百!
  50. [ 'moegirl.org.cn', MediawikiCommon ],
  51.  
  52. // Fandom
  53. [ 'fandom.com', `wiki/${MediawikiRootpageCommon}` ],
  54.  
  55. // Arch Linux wiki
  56. [ 'wiki.archlinux.org', `index.php/${MediawikiCommon}` ],
  57.  
  58. // Debian wiki
  59. [ 'wiki.debian.org', MoinMoinCommon ],
  60.  
  61. // Rosetta Code
  62. [ 'www.rosettacode.org', `wiki/${MediawikiCommon}` ],
  63.  
  64. // The esoteric programming languages wiki
  65. [ 'esolangs.org', `wiki/${MediawikiCommon}` ],
  66.  
  67. // WikiHow sites
  68. [ 'wikihow.com', WikiHowCommon ],
  69. [ 'wikihow.pet', WikiHowCommon ],
  70.  
  71. // Officially the Unofficial Elder Scrolls Pages
  72. [ 'uesp.net', `wiki/${MediawikiCommon}` ],
  73.  
  74. // Million Karts
  75. [ 'thwiki.cc', MediawikiCommon ],
  76. ]);
  77.  
  78. // Main
  79. (function() {
  80. 'use strict';
  81.  
  82. // Navigate to the `location` of current site.
  83. const navigate_to = ( location ) => {
  84. window.location.href = new URL( window.location.href ).origin + '/' + location;
  85. };
  86.  
  87. // Get the rule associated with current site.
  88. const get_rule = () => {
  89. let domain = new URL( window.location.href ).host;
  90. let rule = '';
  91.  
  92. for (;;) {
  93. rule = RULES.get(domain);
  94.  
  95. if ( rule === undefined ) {
  96. // If no rules were found for current domain,
  97. // try matching 1 level upper instead.
  98. if ( ! validated_domain(domain) ) {
  99. return null;
  100. }
  101. // truncate one level of subdomain
  102. domain = domain.substring( domain.indexOf('.') + 1 );
  103. } else {
  104. // Otherwise just return the matched rule.
  105. return rule;
  106. }
  107. }
  108.  
  109. };
  110.  
  111. // There must be at least 2 dots in a valid domain name.
  112. const validated_domain = ( domain ) => {
  113. return ( (domain.match(/\./g) || []).length >= 2 );
  114. };
  115.  
  116. // make codes clearer
  117. const main = () => {
  118. const location = get_rule();
  119.  
  120. switch ( location ) {
  121. case null:
  122. console.log( 'No rules for current site!' );
  123. break;
  124. default:
  125. navigate_to( location );
  126. break;
  127. }
  128. };
  129.  
  130. // ...when press that key.
  131. document.addEventListener('keydown', ( event ) => {
  132. if ( event.code === THAT_KEY )
  133. main();
  134. });
  135.  
  136. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址