MediaWiki:Gadget-Preload.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/** <nowiki>
* Replaces FANDOM's template preloads with a customisable dropdown list.
* Appends dropdown list to Monobook's edit summary area.
* Also adds an input for a custom template preload.
*
*/
( function ( $, mw ) {
'use strict';
var conf = mw.config.get( [
'wgAction',
] ),
self = {
/**
* Generic load function
*/
init: function () {
if ( $ ( '#temp-preload' ).length ) {
return;
}
if ( ['edit', 'submit'].indexOf( conf.wgAction ) > -1 ) {
self.loadPreloads();
}
},
/**
* Gets list of preload templates from Template:Gswpreloads
*/
loadPreloads: function () {
$.get( mw.util.wikiScript(), { title: 'Template:Gswpreloads', action: 'raw', ctype: 'text/plain' }, function ( data ) {
var templates = data.split( '\n' ),
i,
value,
options = '<option value="(browse)">(Browse template preloads)</option>';
for ( i = 1; i < templates.length; i += 1 ) {
switch ( 0 ) {
case templates[i].indexOf( '--' ):
value = templates[i].substring( 2 )
.trim();
options += mw.html.element('option', { value: 'Template:' + value.replace( / /g, '_' ) + '/preload' }, ' ' + value);
break;
// ignore lines starting with // so we can use comments
case templates[i].indexOf( '//' ):
// ignore empty lines
case templates[i].length:
break;
default:
value = templates[i].trim();
options += mw.html.element('option', { value: '', disabled: true }, value);
break;
}
}
self.insertModule( options );
});
},
/**
* Inserts the template module
*
* @param list - html string of option tags to be appended to dropdown
*/
insertModule: function ( list ) {
var module = $( '<div>' )
.attr( {
id: 'temp-preload'
} )
.append(
$( '<div>' )
.attr( {
id: 'std-preload'
} )
.append(
'Standard preloads:',
$( '<select>' )
.attr( {
id: 'std-preload-list'
} )
.html( list )
.change( function () {
var page = $( this ).val();
if ( page === '(browse)' ) {
return;
}
self.insertPreload( page );
} )
),
$( '<div>' )
.attr( {
id: 'cust-preload'
} )
.append(
'Custom preload pagename:',
$( '<input>' )
.attr( {
id: 'cust-preload-input',
type: 'text'
} ),
$( '<input>' )
.attr( {
type: 'button',
id: 'cust-preload-button',
value: 'Insert'
} )
.click( function () {
var input = $( '#cust-preload-input' )
.val()
.trim()
.replace( / /g, '_' );
self.insertPreload( input );
} )
)
);
$( '.mw-editTools' ).prepend( module );
},
/**
* Loads page and inserts the preload into the edit area
*
* @param page - page to be loaded
* @todo check this works in ie10
*/
insertPreload: function ( page ) {
$.get( mw.util.wikiScript(), { title: page, action: 'raw', ctype: 'text/plain' }, function ( data ) {
/**
* Insert at cursor position modified from
* <http://stackoverflow.com/a/11077016/1942596>
*/
var textarea = document.getElementById( 'wpTextbox1' ),
sel,
startPos,
endPos;
// IE support
if ( document.selection ) {
textarea.focus();
sel = document.selection.createRange();
sel.text = data;
// MOZILLA/NETSCAPE support
} else if ( textarea.selectionStart || textarea.selectionStart === '0' ) {
startPos = textarea.selectionStart;
endPos = textarea.selectionEnd;
textarea.value = textarea.value.substring( 0, startPos ) +
data +
textarea.value.substring( endPos, textarea.value.length );
// default to appending to textarea
} else {
textarea.value += data;
}
});
}
};
$( self.init );
}( jQuery, mediaWiki ) );