Jump to content

User:DannyS712/AbuseFilterMessageLinks.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>
// Quick script to add links to the "warn" and "disallow" messages used in
// edit filters
// @author DannyS712
$(() => {
const AbuseFilterMessageLinks = {};
window.AbuseFilterMessageLinks = AbuseFilterMessageLinks;

AbuseFilterMessageLinks.init = function () {
	mw.loader.using(
		[ 'oojs-ui-core', 'oojs-ui-widgets' ],
		AbuseFilterMessageLinks.run
	);
};

AbuseFilterMessageLinks.addHandler = function ( selectId, inputId, toggleId ) {
	const selectWidget = OO.ui.infuse( $( '#' + selectId ) );
	const inputWidget = OO.ui.infuse( $( '#' + inputId ) );

	const $link = $( '<a>' ).attr( 'target', '_blank' );
	const updateLink = ( messageName ) => {
		$link.text( 'MediaWiki:' + messageName );
		$link.attr( 'href', '/wiki/MediaWiki:' + messageName );
	};
	updateLink( inputWidget.getValue() );
	$( '#' + toggleId ).after( ' ', $link );
	
	selectWidget.on( 'change', updateLink );
	inputWidget.on( 'change', updateLink );
};

AbuseFilterMessageLinks.run = function () {
	// Warn
	AbuseFilterMessageLinks.addHandler(
		'mw-abusefilter-warn-message-existing',
		'mw-abusefilter-warn-message-other',
		'mw-abusefilter-warn-preview-button'
	);
	// Disallow
	AbuseFilterMessageLinks.addHandler(
		'mw-abusefilter-disallow-message-existing',
		'mw-abusefilter-disallow-message-other',
		'mw-abusefilter-disallow-preview-button'
	);
};


});

$(document).ready(() => {
	if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'AbuseFilter'
		&& mw.config.get( 'wgPageName' ).match( /^Special:AbuseFilter\/\d+$/ )
	) {
		window.AbuseFilterMessageLinks.init();
	}
});

// </nowiki>