Jump to content

User:YuviPanda/lost.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$( function() {
	if ( mw.config.get( 'wgNamespaceNumber' ) !== 0 ) {
		return;
	}
	mw.loader.using( 'mediawiki.ui', function() {
		var cats = $( "#mw-normal-catlinks ul a").map( function( i, a ) { return $(a).text() });
		var cat = cats[ Math.floor( Math.random() * cats.length ) ];
		$( "<a id='random-related' class='mw-ui-button mw-ui-primary'>MEOW RANDOM!</a>").click( function () {
			window.location.href = "/wiki/Special:RandomInCategory/" + cat;
		}).appendTo( "#siteSub" );

		var newLinks = $.map( $( "#mw-content-text a[href^='/wiki/']" ), function( node ) {
			return $(node).attr('href');
		});
		newLinks = $.grep( newLinks, function( str ) {
			return str.indexOf( ":" ) === -1;
		});

		var plainLink = newLinks[ Math.floor( Math.random() * newLinks.length ) ];

		$( "<a id='random-links' class='mw-ui-button mw-ui-primary'>LINKY RANDOM!</a>").click( function () {
			window.location.href = plainLink;
			localStorage.randomOldLinks = JSON.stringify(newLinks);
		}).appendTo( "#siteSub" );

		var oldLinks = typeof localStorage.randomOldLinks === 'undefined' ? [] : JSON.parse( localStorage.randomOldLinks );

		var intersectLinks = oldLinks.filter(function ( s ) { return newLinks.indexOf( s ) !== -1 } );

		if ( intersectLinks.length ) {
			var intersectLink = intersectLinks[ Math.floor( Math.random() * intersectLinks.length ) ];
			$( "<a id='random-inter-links' class='mw-ui-button mw-ui-primary'>CLOSER RANDOM!</a>").click( function () {
				window.location.href = intersectLink;
				localStorage.randomOldLinks = JSON.stringify(newLinks);
			}).appendTo( "#siteSub" );
		}

	} );
});