// ==UserScript== // @name Wiki Randomizer // @namespace https://script.zgc.im/ // @version 1.1.0 // @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/* // @match https://wiki.debian.org/* // // @icon https://zh.wikipedia.org/favicon.ico // @grant none // @downloadURL none // ==/UserScript== // The default trigger key const THAT_KEY = 'F8'; // Almost any Mediawiki sites use /Special:Random. const MediawikiCommon = 'Special:Random'; const MediawikiRootpageCommon = 'Special:RandomRootpage'; const MoinMoinCommon = 'RandomPage'; // The total rules const RULES = new Map([ // *.wikipedia.org [ 'wikipedia.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 ], ]); // 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 'R'. document.addEventListener('keydown', ( event ) => { if ( event.code === THAT_KEY ) main(); }); })();