User:Suffusion of Yellow/filterTest.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.
/*
 * filterTest: Adds a button at Special:AbuseFilter/nnn pages to open
 * Special:AbuseFilter/test with what's currently in the _edit window_.
 * The built-in link only tests the pattern currently in the _database_,
 * which is probably not what you want.
 */
//<nowiki>
(function() {
	/* globals $, mw, OO */
	'use strict';

	function openTest() {
		let id = mw.config.get('wgPageName').match(/\d+$/);
		let suffix = id ? "/" + id[0] : "";

        let $form = $('<form></form>', {
            style: "display: none;",
            action: "/wiki/Special:AbuseFilter/test" + suffix,
            target: "_blank",
            method: "POST"
        });

        $('<input>', {
			type: 'hidden',
            name: 'wpFilterRules',
            value: $('#wpFilterRules').val()
        }).appendTo($form);

        $('<input>', {
			type: 'hidden',
            name: 'wpShowNegative',
            value: '1'
        }).appendTo($form);

        $form.appendTo('body').submit().remove();
	}

	function addButtons() {
		if (!$("#mw-abusefilter-editing-form").length)
			return;

		let testButton = new OO.ui.ButtonWidget({
			id: "efb-test-changes",
			label: "Test changes",
			title: "Test the modified pattern against recent changes",
		});

		testButton.on('click', openTest);

		$('#mw-abusefilter-syntaxcheck, #efb-show-changes')
			.last().after(testButton.$element);

		/* Prevent buttons from shifting when "Check syntax" is clicked */
		mw.util.addCSS(".mw-spinner { display: none; } ");
	}

	if (mw.config.get('wgCanonicalSpecialPageName') == "AbuseFilter")
		$.when($.ready,
			   mw.loader.using(['mediawiki.util',
								'oojs-ui',
							   ])).then(addButtons);
})();
//</nowiki>