User:Pathoschild/regex.js

From Wikipedia, the free encyclopedia

If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
 // <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>