Jump to content

User:Pathoschild/regex.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.
 // <pre>
/*************
*** Semi-auto regex replacement toolbar
*************/

if(wgAction=='edit' || wgAction=='submit') {
	/* create toolbar */
	function regexReplaceMenu() {
		/* get elements */
		// note: these must be global.
		editbox = document.getElementById('wpTextbox1');
		editreason = document.getElementById('wpSummary');
		sidebar = document.getElementById('column-one');

		/* create menu */
		var container = document.createElement('div');
		container.setAttribute('class','portlet');
		container.setAttribute('id','p-regex');
		sidebar.appendChild(container);
		
		var header = document.createElement('h5');
		header.appendChild(document.createTextNode('Templates'));
		container.appendChild(header);
		
		var toolbox = document.createElement('div');
		toolbox.setAttribute('class','p-body');
		container.appendChild(toolbox);
		
		var toollist = document.createElement('ul');
		toolbox.appendChild(toollist);
		
		/* define menu items */
		function regexTool(name,functionname) {
			var newline = document.createElement('li');
			newline.setAttribute('style','list-style:none !important;');
			
			var newlink = document.createElement('a');
			newlink.setAttribute('href','javascript:'+functionname);
			newlink.setAttribute('title',name);
			newlink.setAttribute('class','sidebar-link');
			newlink.setAttribute('style','color:gray !important;'); // temporary hack, move to CSS later
			newlink.appendChild(document.createTextNode(name));
			
			newline.appendChild(newlink);
			toollist.appendChild(newline);
		}
		
		/* create menu */
		regexTool('Redirect to talk','redirtalk()');
		regexTool('Block template cleanup','blockcleanup()');
	}
	
	/* simplify code */
	function regex(search,replace) {
		editbox.value = editbox.value.replace(search,replace);
	}
	function reason(reason,mode) {
		if(mode=='append' && editreason.value.match(/[^\s]/)) {
			editreason.value = editreason.value+', '+reason;
		}
		else {
			editreason.value = reason;
		}
	}
	
	/* define tools */
	function redirtalk() {
		// replace all with redirect
		editbox.value = '#REDIRECT [[{{subst:TALKPAGENAME}}]]';
		reason('Redirected to talk page');
	}

	function blockcleanup() {
		/* remove template modifiers */
		regex(/{{(?:msg:|template:)/ig,'{{');
	
		/* fix redirects */
		// indefblockeduser
		regex(/{{(?:blockedindef|indef(?:block|blocked|blockuser|blockeduser-big|blockeduser-nocat|vandal)?|page(?:blank|move)vandal|vpblock)(?:\|[^\}]*)?}}/ig,'{{indefblockeduser}}');
		// sockpuppeteer
		regex(/{{puppetmaster/ig,'{{sockpuppeteer');
		regex(/{{sockpuppeteerProven}}/ig,'{{sockpuppeteer|blocked}}');
		// sockpuppets
		regex(/{{blockedsock\|([^}]+)}}/ig,'{{sockpuppet|$1|2=blocked}}');
		regex(/{{sockpuppet(?:proven|Block|Blocked)\|([^}]*)/ig,'{{sockpuppet|$1|2=blocked}}');
		
		/* fix redundancy */
		// multiple indefs (probably from redirect fixes)
		regex(/{{indefblockeduser}}([\s\S]*){{indefblockeduser}}/ig,'{{indefblockeduser}}$1');

		/* adjust edit summary */
		reason('template cleanup','append');
	}
	
	/* load script */
	addOnloadHook(regexReplaceMenu);
}

 // </pre>