User:Murph9000/rollback-toggle.js

/** * Toggle standard MediaWiki rollback links * * Adds an option to the skin's toolbox to toggle the links. * The status is stored in a key "rollbackEnabled" (boolean) * in the JavaScript localStorage, to persist across sessions. * * The standard rollback links (CSS class mw-rollback-link) are * hidden using CSS injected into the page by this script. * No separate CSS file is required. * * Copyright © 2017 User:Murph9000 @ English Wikipedia. All rights reserved. * * Released under the Creative Commons Attribution-ShareAlike 3.0 Unported License * Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */

( function ( mw, $ ) {	'use strict';	var FILE = 'User:Murph9000/rollback-toggle.js';	var KEY = 'rollbackEnabled';	mw.log( FILE, 'startup' );	mw.loader.using( [ 'mediawiki.api', 'mediawiki.api.messages', 'mediawiki.storage', 'mediawiki.user', 'mediawiki.util' ] ).then( function { mw.log( FILE, 'API requests' ); return $.when(			mw.user.getRights,			new mw.Api.loadMessagesIfMissing( [ 'hide', 'show', 'rollbacklink', 'word-separator', ], {				smaxage: 86400, maxage: 86400, } ),			$.ready		); } ).done( function ( rights ) { mw.log( FILE, 'ready', rights ); if ( rights.includes( 'rollback' ) ) { var element, rollbackEnabled = ( mw.storage.get( KEY ) !== 'false' ), rollbackCSS = mw.util.addCSS(					'.mw-rollback-link { display: none; }'				), rollbackLink = mw.util.addPortletLink(					'p-tb',					null,					'Toggle' + mw.msg( 'word-separator' ) + mw.msg( 'rollbacklink' ),					't-rollback-toggle'				); if ( rollbackLink === null ) { /* Link was not added. * This probably means that the skin does not have the portlet. * This happens on the mobile view's skin. * Our CSS has already been added, so rollback is disabled * without a way to enable it. This is intended. */				mw.log.warn( FILE, 'addPortletLink failed, disabled' ); return; }

if ( !( rollbackLink instanceof HTMLAnchorElement ) &&				( element = rollbackLink.querySelector( 'a' ) ) !== null ) { rollbackLink = element; }

var update = function { rollbackCSS.disabled = rollbackEnabled; rollbackLink.textContent = mw.msg( rollbackEnabled ? 'hide' : 'show' ) + mw.msg( 'word-separator' ) + mw.msg( 'rollbacklink' ); };

update;

$( rollbackLink ).on( 'click', function ( event ) {				mw.log( FILE, event );				event.preventDefault;				rollbackEnabled = !rollbackEnabled;				mw.storage.set( KEY, rollbackEnabled );				update;			} ); $( window ) /**				 * Navigating back / farward in browser history, storage events * may have been missed (the JS environment is inactive for				 * pages in the history), so sync to current stored state. */				.on( 'pageshow', function ( event ) {					mw.log( FILE, event );					rollbackEnabled = ( mw.storage.get( KEY ) !== 'false' );					update;				} ) /**				 * Sync to changes in other windows / tabs via events * generated on storage changes. */				.on( 'storage', function ( event ) {					mw.log( FILE, event );					event = event.originalEvent;					if ( event.key === KEY ) {						rollbackEnabled = ( event.newValue !== 'false' );						update;					}				} ); } else { mw.log.warn( FILE, 'no rollback right, script disabled' ); }		mw.log( FILE, 'done' ); } ); } )( mediaWiki, jQuery );