User:Jack Phoenix/global.js

ShoutWiki — express yourself and be heard!
Jump to navigation Jump to search

Note: After saving, 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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
// *****************************************************************
// Custom automatic delete buttons
// *****************************************************************
// Adds customizable one-click deletion buttons to any deletable page.
// Requires Splarka's fastdelete.js
window.fdButtons = [];
if ( typeof mw.config.get( 'wgContentLanguage' ) != 'undefined' && mw.config.get( 'wgContentLanguage' ) != 'fi' ) {
	// For non-Finnish wikis
	var spamSummary = ( window.location.hostname.match( /shoutwiki\.com/, 'gi' ) ? '[[s:Help:Spam|spam]]' : 'Spam' );
	window.fdButtons[window.fdButtons.length] = {
		'summary': spamSummary,
		'label': 'huff spam!',
		'hotkey': '1'
	};
	window.fdButtons[window.fdButtons.length] = {
		'summary': 'Vandalism',
		'label': 'v'
	};
	window.fdButtons[window.fdButtons.length] = {
		'summary': 'Junk',
		'label': 'j'
	};
} else if ( typeof mw.config.get( 'wgContentLanguage' ) != 'undefined' && mw.config.get( 'wgContentLanguage' ) == 'fi' ) { // Finnish delete reasons
	window.fdButtons[window.fdButtons.length] = {
		'summary': '[[s:w:fi:Ohje:Spam|spämmiä]]',
		'label': 's',
		'hotkey': '1'
	};
	window.fdButtons[window.fdButtons.length] = {
		'summary': 'Vandalismia',
		'label': 'v'
	};
	window.fdButtons[window.fdButtons.length] = {
		'summary': 'Roskaa',
		'label': 'r'
	};
}

/**
 * FastDelete -- adds customizable one-click deletion buttons to any deletable
 * page.
 *
 * @author Splarka
 * @author Jack Phoenix <jack@countervandalism.net> (rewrite to object-oriented
 * JS on 15 October 2011)
 * @date 18 June 2016
 */
window.FastDelete = {
	// prevent duplication
	deleteButtonsLoaded: false,

	baseURL: mw.config.get( 'wgScriptPath' ) + '/index.php?title=' +
		encodeURIComponent( mw.config.get( 'wgPageName' ) ) +
		'&action=delete&submitdelete=true&wpReason=',

	// Add delete buttons depending on skin
	addDeleteButtons: function() {
		// Early return if no FastDelete buttons have been defined
		if ( !window.fdButtons ) {
			return;
		}

		switch ( mw.config.get( 'skin' ) ) {
			case 'modern': /* pass to monobook */
			case 'monobook':
				if ( !document.getElementById( 'ca-delete' ) ) {
					return;
				}
				for ( var i = 0; i < window.fdButtons.length; i++ ) {
					if ( window.fdButtons[i].hotkey ) {
						mw.util.addPortletLink(
							'p-cactions',
							FastDelete.baseURL + encodeURIComponent( window.fdButtons[i].summary ),
							window.fdButtons[i].label,
							'ca-delete' + i,
							'one-click delete: ' + window.fdButtons[i].summary,
							window.fdButtons[i].hotkey
						);
					} else {
						mw.util.addPortletLink(
							'p-cactions',
							FastDelete.baseURL + encodeURIComponent( window.fdButtons[i].summary ),
							window.fdButtons[i].label,
							'ca-delete' + i,
							'one-click delete: ' + window.fdButtons[i].summary
						);
					}
				}
				break;

			case 'vector':
				for ( var i = 0; i < window.fdButtons.length; i++ ) {
					if ( fdButtons[i].hotkey ) {
						window.mw.util.addPortletLink(
							'p-views',
							FastDelete.baseURL + encodeURIComponent( window.fdButtons[i].summary ),
							window.fdButtons[i].label,
							'ca-delete' + i,
							'one-click delete: ' + window.fdButtons[i].summary,
							window.fdButtons[i].hotkey
						);
					} else {
						mw.util.addPortletLink(
							'p-views',
							FastDelete.baseURL + encodeURIComponent( window.fdButtons[i].summary ),
							window.fdButtons[i].label,
							'ca-delete' + i,
							'one-click delete: ' + window.fdButtons[i].summary
						);
					}
				}
				break;

			// Cologne Blue support for modern (1.17+) wikis with
			// ResourceLoader and mw.util stuff
			case 'cologneblue':
				for ( var i = 0; i < window.fdButtons.length; i++ ) {
					mw.util.addPortletLink(
						'p-cactions',
						FastDelete.baseURL + encodeURIComponent( window.fdButtons[i].summary ),
						window.fdButtons[i].label,
						'ca-delete' + i,
						'one-click delete: ' + window.fdButtons[i].summary,
						( window.fdButtons[i].hotkey !== undefined ? window.fdButtons[i].hotkey : null )
					);
				}
				break;

			case 'aurora':
				// Don't generate two sets of buttons!
				if ( $( 'li.fastdelete-link' ).length === window.fdButtons.length ) {
					return;
				}
				for ( var i = 0; i < window.fdButtons.length; i++ ) {
					$( '#p-navigation-real' ).after(
						'<li class="p-menu fastdelete-link fastdelete-link-' + i + '" id="p-toolbox">' +
						'<div class="icon">' +
						'<a href="' + FastDelete.baseURL +
							encodeURIComponent( window.fdButtons[i].summary ) +
						'" title="one-click delete: ' + window.fdButtons[i].summary + '">' +
						'<img alt="" src="' + mw.config.get( 'stylepath' ) + '/Aurora/resources/images/icon-small-tools.png">' +
						'</a>' +
						'</div><div class="p-menu-container"></div>' +
						'</li>'
					);
				}
				break;

			case 'bluecloud':
				if ( !document.getElementById( 'ca-delete' ) ) {
					return;
				}
				for ( var i = 0; i < window.fdButtons.length; i++ ) {
					FastDelete.addToBlueCloudTabs(
						FastDelete.baseURL + encodeURIComponent( window.fdButtons[i].summary ),
						window.fdButtons[i].label
					);
				}

			break;

			case 'games':
				// The .registered class for submenus is more or less of a hack,
				// and if FastDelete adds its own tabs without removing that class,
				// the contents of a submenu look funky (too much empty space on
				// the left side), which is why we're getting rid of that class here.
				if ( $( 'div.sub-menu' ).hasClass( 'registered' ) ) {
					$( 'div.sub-menu' ).removeClass( 'registered' );
				}
				for ( var i = 0; i < window.fdButtons.length; i++ ) {
					$( 'div.nav-tabs div.cleared' ).before(
						'<p class="small-tab" id="menu-100' + i + '"><a href="' +
						FastDelete.baseURL +
						encodeURIComponent( window.fdButtons[i].summary ) +
						'" title="one-click delete: ' + window.fdButtons[i].summary +
						'">' + window.fdButtons[i].label + '</a></p>'
					);
				}
				break;

			case 'sports':
				for ( var i = 0; i < window.fdButtons.length; i++ ) {
					$( 'div#tabs div.cleared' ).before(
						'<div class="tab-off" id="menu-100' + i + '">' +
						'<a href="' + FastDelete.baseURL +
							encodeURIComponent( window.fdButtons[i].summary ) +
						'" title="one-click delete: ' + window.fdButtons[i].summary +
						'"><span>' + window.fdButtons[i].label + '</span></a>'
					);
				}
				break;

			case 'hope':
				if ( !document.getElementById( 'ca-delete' ) ) {
					return;
				}
				for ( var i = 0; i < window.fdButtons.length; i++ ) {
					FastDelete.addHopeLink(
						FastDelete.baseURL + encodeURIComponent( window.fdButtons[i].summary ),
						window.fdButtons[i].label,
						'one-click delete: ' + window.fdButtons[i].summary
					);
				}
				break;

			case 'monaco':
				if ( !document.getElementById( 'ca-delete' ) ) {
					return;
				}
				for ( var i = 0; i < window.fdButtons.length; i++ ) {
					FastDelete.addPageBarlink(
						FastDelete.baseURL + encodeURIComponent( window.fdButtons[i].summary ),
						window.fdButtons[i].label
					);
				}
				break;

			case 'nimbus':
				for ( var i = 0; i < window.fdButtons.length; i++ ) {
					$( 'div#more-tab' ).after(
						'<a class="tab-off" href="' +
							FastDelete.baseURL +
							encodeURIComponent( window.fdButtons[i].summary ) +
						'"><span id="ca-fastdelete-' + i + '">' + window.fdButtons[i].label + '</span></a>'
					);
				}
				break;

			case 'quartz':
				if ( !document.getElementById( 'ca-delete' ) ) {
					return;
				}
				for ( var i = 0; i < window.fdButtons.length; i++ ) {
					FastDelete.addArticleBarlink(
						FastDelete.baseURL + encodeURIComponent( window.fdButtons[i].summary ),
						window.fdButtons[i].label
					);
				}
				break;

			case 'phpbb':
			// new name of the phpBB skin, as of MediaWiki 1.18 (because its
			// skin system can't handle the name "phpbb" somehow)
			case 'eminence':
				if ( !document.getElementById( 'ca-delete' ) ) {
					return;
				}
				for ( var i = 0; i < window.fdButtons.length; i++ ) {
					FastDelete.addToPhpbbTabs(
						FastDelete.baseURL + encodeURIComponent( window.fdButtons[i].summary ),
						window.fdButtons[i].label,
						'one-click delete: ' + window.fdButtons[i].summary
					);
				}
				break;

			case 'truglass':
				for ( var i = 0; i < window.fdButtons.length; i++ ) {
					$( 'table.cbar td' ).append(
						'<span id="ca-fastdelete-' + i + '"><a href="' +
						FastDelete.baseURL +
							encodeURIComponent( window.fdButtons[i].summary ) +
						'">' + window.fdButtons[i].label + '</a></span>'
					);
				}
				break;
		}
	},

	checkDelete: function() {
		if ( mw.util.getParamValue( 'autoclose' ) === 'true' && document.getElementById( 'deleteconfirm' ) ) {
			document.getElementById( 'deleteconfirm' ).action += '&autoclose=now';
			document.getElementById( 'deleteconfirm' ).target = '_top';
		}
		if ( mw.util.getParamValue( 'submitdelete' ) === 'true' ) {
			var btn;
			if ( document.getElementById( 'mw-filedelete-submit' ) ) {
				btn = document.getElementById( 'mw-filedelete-submit' );
			} else {
				btn = document.getElementById( 'wpConfirmB' );
			}
			if ( btn ) {
				btn.click();
			}
		}
		if ( mw.util.getParamValue( 'autoclose' ) === 'now' ) {
			try {
				window.close();
			} catch ( e ) {
				// not a JS-created window.
			}
		}
	},

	// BlueCloud support
	addToBlueCloudTabs: function( url, text ) {
		var cactions = document.getElementById( 'p-cactions' );
		if ( cactions.length < 1 ) {
			return;
		}
		var link = document.createElement( 'a' );
		link.href = url;
		link.appendChild( document.createTextNode( text ) );
		var nuli = document.createElement( 'li' );
		nuli.id = 'ca-fastdelete';
		nuli.appendChild( link );
		cactions.appendChild( nuli );
	},

	// Hope support
	addHopeLink: function( url, text, title ) {
		var articleOptions = document.getElementById( 'article-options' );
		if ( articleOptions.length < 1 ) {
			return;
		}
		var link = document.createElement( 'a' );
		link.href = url;
		link.title = title;
		link.appendChild( document.createTextNode( text ) );
		var nuli = document.createElement( 'li' );
		nuli.id = 'ca-fastdelete';
		nuli.appendChild( link );
		articleOptions.appendChild( nuli );
	},

	// Monaco support
	addPageBarlink: function( url, text ) {
		var pc = document.getElementById( 'page_controls' );
		if ( pc.length < 1 ) {
			return;
		}
		var d = document.createElement( 'div' );
		var link = document.createElement( 'a' );
		link.href = url;
		link.appendChild( document.createTextNode( text ) );
		var nuli = document.createElement( 'li' );
		nuli.id = 'control_delete';
		nuli.appendChild( d );
		nuli.appendChild( link );
		pc.appendChild( nuli );
	},

	// Quartz support
	addArticleBarlink: function( url, text ) {
		var ab = document.getElementById( 'articleWrapper' ).getElementsByClassName( 'articleBar' );
		if ( ab.length < 1 ) {
			return;
		}
		var obj = ab[0].getElementsByTagName( 'div' )[0];
		var link = document.createElement( 'a' );
		link.style.color = 'red';
		link.href = url;
		link.appendChild( document.createTextNode( text ) );
		obj.appendChild( link );
	},

	// phpBB/Eminence support
	addToPhpbbTabs: function( url, text, title ) {
		var tabs = document.getElementById( 'tabs' );
		if ( tabs.length < 1 ) {
			return;
		}
		var s = document.createElement( 'span' );
		var link = document.createElement( 'a' );
		link.href = url;
		link.title = title;
		link.appendChild( s );
		s.appendChild( document.createTextNode( text ) );
		var nuli = document.createElement( 'li' );
		nuli.id = 'ca-delete';
		nuli.appendChild( link );
		tabs.appendChild( nuli );
	},

	/**
	 * Initialize FastDelete on non-special namespaces and only when it isn't
	 * already initialized.
	 */
	initialize: function() {
		// this = [object Window] in this context (pun not intended)
		if ( mw.config.get( 'wgNamespaceNumber' ) !== -1 && !FastDelete.deleteButtonsLoaded ) {
			FastDelete.deleteButtonsLoaded = true;
			$( FastDelete.checkDelete );
			$( FastDelete.addDeleteButtons );
		}
	}
};

$( document ).ready( function() {
	mw.loader.using( 'mediawiki.util', function() {
		FastDelete.initialize();
	} );
} );

// *****************************************************************
// Load various external scripts
// *****************************************************************
// Zocky's SearchBox (search 'n' replace utility)
//importScriptURI('http://en.wikipedia.org/w/index.php?title=User:Zocky/SearchBox.js&action=raw&ctype=text/javascript&dontcountme=s');

// Overridden version of the core addPortletLink function for BlueCloud skin
// for FastDelete
if ( mw.config.get( 'skin' ) == 'bluecloud' ) {
	window.addPortletLink = mw.util.addPortletLink = function( portlet, href, text, id, tooltip, accesskey, nextnode ) {
		var ourId = ourTooltip = ourAccesskey = ourNextnode = '';
		if ( id ) {
			ourId = '" id="' + id;
		}
		if ( tooltip ) {
			ourTooltip = '" title="' + tooltip;
		}
		if ( accesskey ) {
			ourAccesskey = '" accesskey="' + accesskey;
			ourTooltip = ourTooltip + ' [' + accesskey + ']';
		}
		$( '#' + portlet ).append(
			'<li><a href="' + href + ourId + ourAccesskey + ourTooltip +
			'">' + text + '</a></li>'
		);
	}
}

// *****************************************************************
// Purge tab on every non-special page
// By Splarka
// *****************************************************************
if ( mw.config.get( 'wgNamespaceNumber' ) > -1 ) {
	$( function() {
		var appendTo;
		if ( mw.config.get( 'skin' ) == 'vector' ) {
			appendTo = 'p-views';
		} else if ( mw.config.get( 'skin' ) == 'monaco' ) {
			appendTo = 'page_tabs';
		} else {
			appendTo = 'p-cactions';
		}
		mw.loader.using( 'mediawiki.util', function() {
			mw.util.addPortletLink(
				appendTo,
				mw.config.get( 'wgServer' ) + mw.config.get( 'wgScript' ) +
					'?title=' + encodeURIComponent( mw.config.get( 'wgPageName' ) ) +
					'&action=purge',
				'purge'
			);
		} );
	} );
}

// *****************************************************************
// Adds a tab to view the page with ?uselang=fi to every page
// *****************************************************************
$( function() {
	var appendTo;
	if ( mw.config.get( 'skin' ) == 'vector' ) {
		appendTo = 'p-views';
	} else if ( mw.config.get( 'skin' ) == 'monaco' ) {
		appendTo = 'page_tabs';
	} else {
		appendTo = 'p-cactions';
	}

	var fiLink = document.location.href;
	if ( fiLink.indexOf( '?' ) == -1 ) {
		fiLink += '?uselang=fi';
	} else {
		fiLink += '&uselang=fi';
	}

	mw.loader.using( 'mediawiki.util', function() {
		mw.util.addPortletLink(
			appendTo,
			fiLink,
			'fi',
			't-fi',
			'Display this page in Finnish'
		);
	} );
} );

// *****************************************************************
// &bot=1 on contribs pages (Special:Contributions/Foo).
// *****************************************************************
function hideRollback() {
	var botlink = document.location.href;
	if ( botlink.indexOf( '?' ) == -1 ) {
		botlink += '?bot=1';
	} else {
		botlink += '&bot=1';
	}

	var appendTo;
	if ( mw.config.get( 'skin' ) == 'vector' ) {
		appendTo = 'p-views';
	} else if ( mw.config.get( 'skin' ) == 'monaco' ) {
		appendTo = 'page_tabs';
	} else {
		appendTo = 'p-cactions';
	}

	mw.loader.using( 'mediawiki.util', function() {
		mw.util.addPortletLink( appendTo, botlink, '&bot=1', 'ca-bot' );
	} );
}
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) == 'Contributions' ) {
	$( hideRollback );
}

// *****************************************************************
// Toggle list numbers on/off on Special:NewestPages
// *****************************************************************
$( function() {
	if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'NewestPages' ) {
		var $element = $( '#mw-content-text p:eq(2)' ); // get the _third_ paragraph (arrays are zero-indexed)
		var originalText = $element.text();
		$element.html(
			$( '<a>' )
				.attr( 'href', '#' )
				.click( function( e ) {
					e.preventDefault(); // don't jump to the top of the page
					$( 'ol' ).css( 'list-style-type', $( 'ol' ).css( 'list-style-type' ) === 'none' ? '' : 'none' );
				} )
				// put back the original text because we overwrite the third <p>aragraph
				// tag with our new pseudo-link above
				.text( originalText )
		);
	}
} );


// *****************************************************************
// Prefill username and reason fields on [[Special:Maintenance/deleteBatch]]
// *****************************************************************
$( function () {
	if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Maintenance' ) {
		if ( mw.config.get( 'wgTitle' ).split( '/' )[1] === 'deleteBatch' ) {
			$( '#wpU' ).val( mw.config.get( 'wgUserName' ) );
			$( '#wpR' ).val( '[[s:Help:Spam|spam]]' );
		}
	}
} );