Open main menu

Snippets/Toggle user scripts

How to use Snippets
List of Snippets
Crystal Clear action run.png
Toggle user scripts
Language(s): JavaScript
Compatible with: MediaWiki 1.17+ (Vector)

DescriptionEdit

Adds a link to the Toolbox portlet for easily disabling/enabling your user scripts while (may be useful when investigating some bugged gadget).

CodeEdit

(function () {
	/**
	 * Add a toolbox link to enable/disable my scripts
	 *
	 * @sources www.mediawiki.org/wiki/Snippets/Toggle_user_scripts
	 * @rev 3
	 */
	var enableMyScripts = $.cookie( 'enable-my-scripts' ) !== 'false';
	$(function () {
		var label = {
				// Current state <-> Label of toggle button (opposite state)
				'true': 'Disable my scripts',
				'false': 'Enable my scripts'
			},
			cookieOptions = {
				expires: 1,
				path: '/'
			};

		if ( $('#ca-toggle-js').length ) {
			return;
		}

		$( mw.util.addPortletLink(
			'p-tb',
			'#',
			label[ enableMyScripts ],
			'#ca-toggle-js',
			'Turn my scripts on or off and reload the page'
		) )
			.on( 'click', function (e) {
				// Prevent '#' from appearing in address bar
				e.preventDefault();
				$.cookie( 'enable-my-scripts', !enableMyScripts, cookieOptions );
				// Reloads the document (from cache)
				document.location.reload( false );
			} );
	} );

	if (!enableMyScripts) {
		mw.log( 'Note: Your scripts are disabled.' );
		return;
	}

	/* Begin user scripts */

	// Your scripts here

	/* End user scripts */
}());