Greasy Fork

Greasy Fork is available in English.

Wiki Randomizer

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

当前为 2021-04-29 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Wiki Randomizer
// @namespace    https://script.zgc.im/
// @version      1.5.0
// @description  Press a key to view a random page on current Wiki.
// @author       MidAutumnMoon
//
// @include /^https://.*\.wiki(pedia|voyage|source|books)\.org/.*$/
// @match https://*.wiktionary.org/*
// @include /^https://wiki\.(archlinux|debian)\.org/.*$/
// @include /^https://.*\.wikihow\.(com|pet)/.*$/
// @match https://*.fandom.com/*
// @match https://*.moegirl.org.cn/*
// @match https://tcrf.net/*
// @match http*://www.rosettacode.org/*
// @match https://esolangs.org/*
//
// @icon         https://zh.wikipedia.org/favicon.ico
// @grant        none
// ==/UserScript==

// The default trigger key
const THAT_KEY = 'F8';


const MediawikiCommon         = 'Special:Random';
const MediawikiRootpageCommon = 'Special:RandomRootpage';
const MoinMoinCommon          = 'RandomPage';
const WikiHowCommon           = 'Special:Randomizer'

// The total rules
const RULES = new Map([
  // All about Wikipedia
  [ 'wikipedia.org',  'wiki/'+MediawikiCommon ],
  [ 'wikivoyage.org', 'wiki/'+MediawikiCommon ],
  [ 'wikisource.org', 'wiki/'+MediawikiCommon ],
  [ 'wikibooks.org',  'wiki/'+MediawikiCommon ],
  [ 'wiktionary.org', 'wiki/'+MediawikiCommon ],

  // Cutting Room Floor
  [ 'tcrf.net', MediawikiRootpageCommon ],

  // 萌百!
  [ 'moegirl.org.cn', MediawikiCommon ],

  // Fandom
  [ 'fandom.com', 'wiki/'+MediawikiRootpageCommon ],

  // Arch Linux wiki
  [ 'wiki.archlinux.org', 'index.php/'+MediawikiCommon ],

  // Debian wiki
  [ 'wiki.debian.org', MoinMoinCommon ],

  // Rosetta Code
  [ 'www.rosettacode.org', 'wiki/'+MediawikiCommon ],

  // The esoteric programming languages wiki
  [ 'esolangs.org', 'wiki/'+MediawikiCommon ],

  // WikiHow sites
  [ 'wikihow.com', WikiHowCommon ],
  [ 'wikihow.pet', WikiHowCommon ],
]);

// Main
(function() {
  'use strict';

  // Navigate to the `location` of current site.
  const navigate_to = ( location ) => {
    window.location.href = new URL( window.location.href ).origin + '/' + location;
  };

  // Get the rule associated with current site.
  const get_rule = () => {
    let domain = new URL( window.location.href ).host;
    let rule = '';

    for (;;) {
      rule = RULES.get(domain);

      if ( rule === undefined ) {
        // If no rules were found for current domain,
        // try matching 1 level upper instead.
        if ( ! validated_domain(domain) ) {
          return null;
        }
        // truncate one level of subdomain
        domain = domain.substring( domain.indexOf('.') + 1 );
      } else {
        // Otherwise just return the matched rule.
        return rule;
      }
    }

  };

  // There must be at least 2 dots in a valid domain name.
  const validated_domain = ( domain ) => {
    return ( (domain.match(/\./g) || []).length >= 2 );
  };

  // make codes clearer
  const main = () => {
    const location = get_rule();

    switch ( location ) {
      case null:
        console.log( 'No rules for current site!' );
        break;
      default:
        navigate_to( location );
        break;
    }
  };

  // ...when press that key.
  document.addEventListener('keydown', ( event ) => {
    if ( event.code === THAT_KEY )
      main();
  });

})();