User:MarkusRost/common.js

/* Gamepedia replacement for insource and intitle search */ if ( mw.config.get('wgCanonicalSpecialPageName') === 'Search' ) mw.loader.using(['mediawiki.api']).done( function {

var a = new mw.Api; a.loadMessagesIfMissing(['wikiasearch2-noresults', 'wikiasearch2-results-for']);

$('.unified-search__form button[type="submit"]').click( function(click) {	var searchterm = $('input.unified-search__input__query').val.trim;	if ( !/^\-?in(?:source|title):/.test(searchterm) ) return;	click.preventDefault;	var termList = searchterm.replace( /^\-?in(?:source|title):/, '' ).trim;	var searchterms = [];	if ( /^".*[^\\]"$/.test(termList) ) searchterms = termList.substring(1).split(/(?<!\\)"(?:\s+"|$)/).filter( function(term) {return term.length} );	else if ( /^'.*[^\\]'$/.test(termList) ) searchterms = termList.substring(1).split(/(?<!\\)'(?:\s+'|$)/).filter( function(term) {return term.length} );	else if ( /^\/.*[^\\]\/$/.test(termList) ) {		searchterms = termList.substring(1).split(/(?<!\\)\/(?:\s+\/|$)/).filter( function(term) {return term.length} ).map( function(term) {return RegExp(term, 'g')} );	}	else searchterms = termList.split(' ').filter( function(term) {return term.length} ); console.log('[insource] Searching for:', searchterms); var inTitle = /^\-?intitle:/.test(searchterm); // https://wikibot.fandom.com/api.php?formatversion=2&action=query&generator=allpages&gaplimit=500&gapfilterredir=nonredirects&prop=revisions&rvslots=main&rvprop=content var requestBase = { formatversion: 2, action: 'query', generator: 'allpages', gapfilterredir: 'nonredirects', gaplimit: 'max' };	var revisions = { gaplimit: 500, prop: 'revisions', rvslots: 'main', rvprop: 'content' };	var namespaces = $('.unified-search__form .unified-search__advanced__checkbox--namespace input[name="ns[]"]:checked').get.map( function(ns) {return ns.value} ); if ( !namespaces.length ) namespaces.push(( $('.insource-selected').length ? $('.insource-selected').attr('value') : '0' )); var startNS = ( $('.insource-selected').length && namespaces.includes( $('.insource-selected').attr('value') ) ? $('.insource-selected').attr('value') : namespaces[0] ); $('.unified-search__layout__main').html( $('').text( 'Search in progress...' ) ); searchPages( Object.assign({gapnamespace: startNS}, requestBase, ( inTitle || revisions )), 0 ); $('.unified-search__layout__right-rail').html(''); $('.unified-search__profiles').html(''); namespaces.forEach( function(namespace) {		var profile = $('');		if ( namespace === startNS ) profile.addClass(['unified-search__profiles__profile--selected', 'insource-selected']);		$('.unified-search__profiles').append( $('').append(			profile.attr('value', namespace).text($('label[for="search-ns-' + namespace + '"]').text.trim)		) );		profile.click( function { $('.insource-selected').removeClass(['unified-search__profiles__profile--selected', 'insource-selected']); profile.addClass(['unified-search__profiles__profile--selected', 'insource-selected']); $('.unified-search__layout__main').html( $('').text( 'Search in progress...' ) ); searchPages( Object.assign({gapnamespace: namespace}, requestBase, ( inTitle || revisions )), 0 ); } );	} );	function searchPages(request, isContinue, fromPagination) { a.get(request).done( function(data) {			var pagelist = ( data.query ? data.query.pages.filter( function(page) {				var content = ( inTitle ? page.title : ( page.revisions && page.revisions[0] && page.revisions[0].slots.main.content ) );				if ( content === undefined ) return false;				var isHit = searchterms.every( function(term, i) { var result = false; var index = 100; if ( typeof term === 'string' ) { index = content.indexOf( term ); result = ( index !== -1 ); }					else { result = term.test(content); index = term.lastIndex; }					if ( i === 0 ) page.description = content.substring(index-100, index+100) return result; } );				return ( searchterm.startsWith( '-' ) ? !isHit : isHit );			} ) : [] );			if ( pagelist.length && !$('.unified-search__results').length ) $('.unified-search__layout__main').append( $('').html( 					mw.msg('wikiasearch2-results-for', $(' ').append( $(' ').text( searchterm ) ).html)				), $(''), $('.unified-search__pagination') );			pagelist.forEach( function(page) { var link = mw.util.getUrl(page.title); $('.unified-search__layout__main ul.unified-search__results').append( $('').append( $(' ').append(					$(' ').append( $('').attr('href', link).text(page.title) ),					$('').text(page.description),					$(' ').append( $('').attr('href', link).text(window.location.origin + link) )				) ) ) } );			if ( $('.unified-search__results').length && !$('.unified-search__pagination').length ) {				$('.unified-search__layout__main').append( $('').append(					$(' ').append( $('').append( $('').append( $('').text(1).attr('data-query',							JSON.stringify(Object.assign({gapnamespace: startNS}, requestBase, ( inTitle || revisions )))						) ) ) )				) );			}			if ( data.continue ) {				isContinue++				if ( isContinue >= 20 ) {					$('#insource-searching').remove;					if ( !$('.unified-search__results').length ) $('.unified-search__layout__main').prepend( $('').text( mw.msg('wikiasearch2-noresults') ) );					if ( !fromPagination ) {						$('.unified-search__pagination .wikia-paginator ul').append( $('').append( 							$('<a class="paginator-page">').text($('.unified-search__pagination .wikia-paginator ul li').length+1).attr('data-query', JSON.stringify(Object.assign(request, data.continue)) )						) );						$('.unified-search__pagination .wikia-paginator ul li a.paginator-page').click( function { $('.unified-search__pagination .wikia-paginator ul li a.active.paginator-page').removeClass(['active']); $(this).addClass(['active']); $('.unified-search__layout__main').html( $(' ').append( $('<i id="insource-searching">').text( 'Search in progress...' ), $('.unified-search__pagination') ).html ); searchPages(JSON.parse($(this).attr('data-query')), 0, true); } );					}					return;				}				delete request.continue;				delete request.rvcontinue;				delete request.gapcontinue;				Object.assign(request, data.continue);				return searchPages(request, isContinue);			}			$('#insource-searching').remove;			if ( !$('.unified-search__results').length ) $('.unified-search__layout__main').prepend( $('<i>').text( mw.msg('wikiasearch2-noresults') ) );		} ).fail( function(data, response) {			console.log( "[insource] Error while querying pages:", data, response );		} ); } } );

} ).fail( function(data) { console.log( "[insource] Failed to load the API module:", data); } );