Wiki Randomizer

Press 'R' to jump to a random wiki page.

目前為 2021-04-19 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Wiki Randomizer
// @namespace    https://script.zgc.im/
// @version      0.7
// @description  Press 'R' to jump to a random wiki page.
// @author       MidAutumnMoon
//
// @include /^https://.*\.wikipedia\.org/.*$/
// @include /^https://.*\.fandom\.com/.*$/
// @include /^https://.*\.moegirl\.org\.cn/.*$/
// @match https://tcrf.net/*
// @match https://wiki.archlinux.org/*
//
// @icon         https://zh.wikipedia.org/favicon.ico
// @grant        none
// ==/UserScript==

// Press this single key to do navigate (JavaScript keycode)
const THAT_KEY = 'KeyR';

// Almost any Mediawiki sites use /Special:Random.
const MediawikiCommon = 'Special:Random';

const RootpageCommon = 'Special:RandomRootpage';

// The total rules
const RULES = new Map([
  // *.wikipedia.org
  [ 'wikipedia.org', 'wiki/'+MediawikiCommon ],

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

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

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

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

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

  // ...when press 'R'.
  document.addEventListener('keydown', ( event ) => {
    if ( event.code === THAT_KEY ) {
      const location = get_rule();

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

    }
  });
})();