User:Pathoschild/regex.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Pathoschild/regex. |
// <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>