Jump to content

User:PleaseStand/subsearch.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.
/*  subsearch.js: A user script to add a checkbox that allows searching
    only the subpages of the current page.
    
    Copyright (c) 2010, PleaseStand
    
    This software is licensed under these licenses:
    1.  Creative Commons Attribution-Share Alike 3.0 Unported License
        (see <http://creativecommons.org/licenses/by-sa/3.0/> for the text)
    2.  GNU Free Documentation License, any published version.
        (see <http://www.gnu.org/copyleft/fdl.html> for the text)
    3.  Permission to use, copy, modify, and/or distribute this software for any
        purpose with or without fee is hereby granted, provided that the above
        copyright notice and this permission notice appear in all copies.
    
        THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
        ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
        OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    
    You may select the license(s) of your choice if you wish to copy, modify, or
    distribute this software. If you modify the software and do not wish to
    license your changes under one or more of the licenses, please remove
    the license(s) from the list above.
*/

( function ( mw, $ ) {
	
	mw.loader.using( 'mediawiki.Title', function () {
	
		$( function () {
		    var title, $checkbox, $label, settings = {
		        text: "\u00a0Subpages only",
		        tooltip: "Search only the subpages of the current page",
		        accesskey: "",
		        style: "display: block; font-size: 80%; margin-top: 0.4em;",
		        namespaces: // Update the following line from http://wiki.riteme.site/w/api.php?action=query&meta=siteinfo&siprop=namespaces&format=json
					{"query":{"namespaces":{"-2":{"id":-2,"case":"first-letter","*":"Media","canonical":"Media"},"-1":{"id":-1,"case":"first-letter","*":"Special","canonical":"Special"},"0":{"id":0,"case":"first-letter","*":"","content":""},"1":{"id":1,"case":"first-letter","*":"Talk","subpages":"","canonical":"Talk"},"2":{"id":2,"case":"first-letter","*":"User","subpages":"","canonical":"User"},"3":{"id":3,"case":"first-letter","*":"User talk","subpages":"","canonical":"User talk"},"4":{"id":4,"case":"first-letter","*":"Wikipedia","subpages":"","canonical":"Project"},"5":{"id":5,"case":"first-letter","*":"Wikipedia talk","subpages":"","canonical":"Project talk"},"6":{"id":6,"case":"first-letter","*":"File","canonical":"File"},"7":{"id":7,"case":"first-letter","*":"File talk","subpages":"","canonical":"File talk"},"8":{"id":8,"case":"first-letter","*":"MediaWiki","canonical":"MediaWiki"},"9":{"id":9,"case":"first-letter","*":"MediaWiki talk","subpages":"","canonical":"MediaWiki talk"},"10":{"id":10,"case":"first-letter","*":"Template","subpages":"","canonical":"Template"},"11":{"id":11,"case":"first-letter","*":"Template talk","subpages":"","canonical":"Template talk"},"12":{"id":12,"case":"first-letter","*":"Help","subpages":"","canonical":"Help"},"13":{"id":13,"case":"first-letter","*":"Help talk","subpages":"","canonical":"Help talk"},"14":{"id":14,"case":"first-letter","*":"Category","subpages":"","canonical":"Category"},"15":{"id":15,"case":"first-letter","*":"Category talk","subpages":"","canonical":"Category talk"},"100":{"id":100,"case":"first-letter","*":"Portal","subpages":"","canonical":"Portal"},"101":{"id":101,"case":"first-letter","*":"Portal talk","subpages":"","canonical":"Portal talk"},"108":{"id":108,"case":"first-letter","*":"Book","subpages":"","canonical":"Book"},"109":{"id":109,"case":"first-letter","*":"Book talk","subpages":"","canonical":"Book talk"},"446":{"id":446,"case":"first-letter","*":"Education Program","canonical":"Education Program"},"447":{"id":447,"case":"first-letter","*":"Education Program talk","subpages":"","canonical":"Education Program talk"},"710":{"id":710,"case":"first-letter","*":"TimedText","canonical":"TimedText"},"711":{"id":711,"case":"first-letter","*":"TimedText talk","canonical":"TimedText talk"},"828":{"id":828,"case":"first-letter","*":"Module","subpages":"","canonical":"Module"},"829":{"id":829,"case":"first-letter","*":"Module talk","subpages":"","canonical":"Module talk"}}}}
		    };
		    
		    $.extend( settings, window.SubsearchJsConfig );
		    
		    title = mw.Title.newFromText( mw.config.get( 'wgRelevantPageName' ) );
		    if ( !title || settings.namespaces.query.namespaces[title.getNamespaceId()]['subpages'] === undefined ) {
		        return;
		    }
		    
		    $checkbox = $( '<input type="checkbox" />' )
		    	.attr( 'accesskey', settings.accesskey );
		    	
		    $label = $( '<label />' )
		    	.attr( {
		    		title: settings.tooltip,
		    		style: settings.style
		    	} )
		    	.text( settings.text )
		    	.prepend( $checkbox );
		    	
		    $( '#searchform' )
		    	.append( $label )
		    	.submit( function () {
			    	var $textbox = $( '#searchInput' );
			    
				    if ( $checkbox.prop( 'checked' ) ) {
				        $textbox.val( $textbox.val() + ' prefix:' + title.getPrefixedText() + '/' );
				        $checkbox.prop( 'checked', false );
				    }
			    } );
		    
		});
		
	} );

}( mediaWiki, jQuery ) );