User:Polygnotus/Scripts/MySubpages.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Polygnotus/Scripts/MySubpages.
$.when( mw.loader.using( ['mediawiki.util'] ), $.ready ).done( function() {
var userName = mw.config.get( 'wgUserName' );
var $portletLink = $( mw.util.addPortletLink(
'p-personal',
mw.util.getUrl( 'Special:PrefixIndex/User:' + userName + '/' ),
'Subpages',
'pt-mysubpages',
'Show your subpages',
null,
'#pt-preferences'
) );
// Fire the hook so Navigation Popups/Popups process the new link
if ( $portletLink.length ) {
mw.hook( 'wikipage.content' ).fire( $portletLink );
}
mw.util.addCSS(
'#pt-userspace-search input.userspace-no-results {' +
' outline: 2px solid red;' +
' transition: outline-color 1.5s ease;' +
'}' +
'#pt-userspace-search input.userspace-no-results-fade {' +
' outline: 2px solid transparent;' +
' transition: outline-color 1.5s ease;' +
'}'
);
var $li = $( '<li>' ).attr( 'id', 'pt-userspace-search' );
var $icon = $( '<a>' )
.attr( 'title', 'Search in your userspace' )
.attr( 'href', '#' )
.attr( 'role', 'button' )
.css( 'textDecoration', 'none' )
.text( '🔍' );
var $input = $( '<input>' )
.attr( 'type', 'text' )
.attr( 'placeholder', 'Search userspace…' )
.css( {
display : 'none',
width : '140px',
marginLeft : '4px',
font : 'inherit',
fontSize : '0.85em'
} );
var fadeTimer = null;
function showNoResults() {
if ( fadeTimer ) { clearTimeout( fadeTimer ); }
$input.removeClass( 'userspace-no-results-fade' ).addClass( 'userspace-no-results' );
fadeTimer = setTimeout( function() {
$input.removeClass( 'userspace-no-results' ).addClass( 'userspace-no-results-fade' );
setTimeout( function() {
$input.removeClass( 'userspace-no-results-fade' );
}, 1500 );
}, 600 );
}
$icon.on( 'click', function( e ) {
e.preventDefault();
$input.toggle();
if ( $input.is( ':visible' ) ) {
$input.trigger( 'focus' );
}
} );
$input.on( 'keydown', function( e ) {
if ( e.key === 'Enter' ) {
var query = $.trim( $( this ).val() );
if ( !query ) { return; }
var searchString = query + ' prefix:"User:' + userName + '"';
$.getJSON( mw.config.get( 'wgScriptPath' ) + '/api.php', {
action : 'query',
list : 'search',
srsearch : searchString,
srnamespace: 2,
srlimit : 1,
srinfo : 'totalhits',
format : 'json'
} ).done( function( data ) {
var hits = data &&
data.query &&
data.query.searchinfo &&
data.query.searchinfo.totalhits;
if ( hits > 0 ) {
window.location.href =
mw.config.get( 'wgScript' ) +
'?title=Special%3ASearch' +
'&search=' + encodeURIComponent( searchString ) +
'&ns2=1' +
'&profile=advanced' +
'&fulltext=1';
} else {
showNoResults();
}
} ).fail( function() {
window.location.href =
mw.config.get( 'wgScript' ) +
'?title=Special%3ASearch' +
'&search=' + encodeURIComponent( searchString ) +
'&ns2=1' +
'&profile=advanced' +
'&fulltext=1';
} );
} else if ( e.key === 'Escape' ) {
$input.val( '' ).hide();
}
} );
$( document ).on( 'click', function( e ) {
if ( !$li.is( e.target ) && $li.has( e.target ).length === 0 ) {
$input.val( '' ).hide();
}
} );
$li.append( $icon, $input );
$( '#pt-mysubpages' ).after( $li );
} );