User:Ilmari Karonen/userboxes.js

/* This user script allows the semiautomatic substitution of deleted userboxes on user talk pages. To enable it, add the following line to Special:Mypage/monobook.js:

User:Ilmari Karonen/userboxes.js

You may also change the list of userboxes to be substed and the location they are substed from by assigning your own values to window.userboxRegExp and window.userboxPrefix. These will override the default values below.

The format expected from the regexp is a list of userbox names, without the "Template:User_" prefix, separated by pipes as in "box1|box2|box3". Most types of regexp syntax work as expected, but note that the regexp is postprocessed so that "." matches a literal period, and "_" and " " both match either a space or an underscore. And don't forget to double any backslashes.

*/

if (!window.userboxRegExp) window.userboxRegExp = "";  // none currently being substed if (!window.userboxPrefix) window.userboxPrefix = "User:Ilmari_Karonen/userboxes/"; if (!window.userboxSummary) window.userboxSummary = "Javascript-assisted substitution of deleted userboxes";

addOnloadHook(function {    var editForm = document.forms.editform;

function unSubst (text, template, re, paramNames, paramDefs) { while (true) { var match = re.exec(text); if (!match) break; var params = ""; for (var i = 1; i < match.length; i++) { var paramName = paramNames[i-1]; var paramDef = paramDefs[i-1]; var param = (match[i] == null ? paramDef : "" + match[i]); while (true) { var tmp = param.replace(/\{\{\{[^|{}]*(\|(.*))?\}\}\}/, "$2"); if (tmp == param) break; param = tmp; }               param = param.replace(/^\s+/,"").replace(/\s+$/,""); if (param == "" && paramName.match(/(border-|-f)c$/)) param = "black"; else if (param == "" && paramName.match(/-c$/)) param = "white"; if (param != paramDef) params += "|" + paramName + "=" + param; }           text = text.replace(match[0], ""); }       return text; }

function unSubstUserbox { var text = editForm.wpTextbox1.value;

text = text.replace(/ [\s\S]*?<\/noinclude>/g, ""); // rm noinclude text = text.replace(/\[\[\s*Category:[\s\S]*?\]\]/ig, "");    // rm categories text = text.replace(/\s+$/, "");                              // rm trailing whitespace

var saveText = text;

text = unSubst(text, "Userbox-2",       / *\n\{\| *cellspacing="0" *style="width: *(23\d|240)px; *background: *([^";]*?); *(?:text-align: *center; *)?" *\n\| *style="width: *4\dpx; *height: *4\dpx; *background: *([^";]*?); *(?:text-align: *center; *)?(?:font-size: *(\S+)pt; *)?(?:color: *([^";]*?); *)?" *\| *(?:)?([\s\S]*?)(?:)? *\n\| *style="font-size: *(\S+)pt; *(?:color: *([^";]*?); *)?padding: *(\S+)pt; *line-height: *1\.25em; *(?:text-align: *center; *)?" *\| *([\s\S]*?) *\n\| *style="width: *4\dpx; *height: *4\dpx; *background: *([^";]*?); *(?:text-align: *center; *)?(?:font-size: *(\S+)pt; *)?(?:color: *([^";]*?); *)?" *\| *(?:)?([\s\S]*?)(?:)? *\n\|\}\s*<\/div>/,        ["float","border-s","border-c","border-s","width","info-c","id1-c","id1-s","id1-fc","id1","info-s","info-p","info-fc","info"    ,"id2-c","id2-s","id2-fc","id2"], ["left" ,"1",      ""        ,""        ,"238"  ,"#EEE"  ,"#DDD" ,"14"   ,"black" ,"id1","8"     ,"4"     ,"black"  ,"info",""     ,""     ,"black" ,"id2"]);

text = unSubst(text, "Userbox-r",       / *\n\{\| *cellspacing="0" *style="width: *(23\d|240)px; *background: *([^";]*?); *(?:text-align: *center; *)?" *\n\| *style="font-size: *(\S+)pt; *padding: *(\S+)pt; *line-height: *1\.25em; *(?:color: *([^";]*?); *)?(?:text-align: *center; *)?" *\| *([\s\S]*?) *\n\| *style="width: *4\dpx; *height: *4\dpx; *background: *([^";]*?); *(?:text-align: *center; *)?(?:font-size: *(\S+)pt; *)?(?:color: *([^";]*?); *)?" *\| *(?:)?([\s\S]*?)(?:)? *\n\|\}\s*<\/div>/,        ["float","border-s","border-c","border-s","width","info-c","info-s","info-p","info-fc","info"    ,"id-c","id-s","id-fc","id"],        ["left" ,"1",       ""        ,""        ,"238"  ,"#EEE"  ,"8"     ,"4"     ,"black"  ,"info","#DDD","14"  ,"black","id"]);

text = unSubst(text, "Userbox",       / *\n\{\| *cellspacing="0" *style="width: *(23\d|240)px; *background: *([^";]*?); *(?:text-align: *center; *)?" *\n\| *style="width: *4\dpx; *height: *4\dpx; *background: *([^";]*?); *(?:text-align: *center; *)?(?:font-size: *(\S+)pt; *)?(?:color: *([^";]*?); *)?(?:text-align: *center; *)?" *\| *(?:)?([\s\S]*?)(?:)? *\n\| *style="font-size: *(\S+)pt; *padding: *(\S+)pt; *line-height: *1\.25em; *(?:color: *([^";]*?); *)?" *\| *([\s\S]*?) *\n\|\}\s*<\/div>/,        ["float","border-s","border-c","border-s","width","info-c","id-c","id-s","id-fc","id","info-s","info-p","info-fc","info"    ],        ["left" ,"1",       ""        ,""        ,"238"  ,"#EEE"  ,"#DDD","14"  ,"black","id","8"     ,"4"     ,"black"  ,"info"]);

text += " \n\nThis is a temporary page which exists only to be substed. It will be deleted after the substitution is completed. \n\n\n\nThis is what the original looked like:\n\n"+saveText+" ";

editForm.wpTextbox1.value = text; editForm.wpSummary.value = "copy to subst"; editForm.wpPreview.click; }

function fixUserboxes { var userBoxesToSubst = window.userboxRegExp.replace(/\./g, "\\.").replace(/[\s_]/g, "[_ ]"); var newText = editForm.wpTextbox1.value; var babel_re = new RegExp ("\n?\\{\\{(?:[Tt]emplate:|msg:)?\\s*([Bb]abel|UBX)-(?:\\d+|X)\\s*\\|(?:\\s*if\\s*=\\s*\\|)?(([^{}|=]*\\|)*\\s*(" + userBoxesToSubst + ")\\s*(\\|[^{}|=]*)*)\\}\\}\n?"); while (true) { var babelMatch = babel_re.exec(newText); if (!babelMatch || babelMatch.length < 2) break; var babelOrig = babelMatch[0]; var babelType = babelMatch[1]; var babelSubst = babelMatch[2]; babelSubst = babelSubst.replace(/[|\s]+($|\|)/, "$1").replace(/\|\s*/g, "}}\n|-\n| {{User "); babelSubst = "\n{| name=\"userboxes\" id=\"userboxes\" style=\"float: right; margin-left: 1em; margin-bottom: 1ex; width: 242px; border: 1px solid #99B3FF; clear: right\"\n|-\n| style=\"text-align: center\" | " + (babelType == "UBX" ? "Userboxes" : "Babel") + "\n|-\n| {{User " + babelSubst + "}}\n" + (babelType == "UBX" ? "" : "|-\n| style=\"text-align: center\" | Search user languages\n") + "|}\n"; //alert("Replacing "+babelOrig+" with:\n"+babelSubst); newText = newText.replace(babelOrig, babelSubst); }       var userbox_re = new RegExp ("\\{\\{(?:[Tt]emplate:|msg:)?\\s*[Uu]ser[ _](" + userBoxesToSubst + ")[|\\s]*\\}\\}", "g"); editForm.wpTextbox1.value = newText.replace(userbox_re, ""); editForm.wpSummary.value = window.userboxSummary; editForm.wpMinoredit.checked = true; editForm.wpSave.click; // I'm feeling lucky! }

function addCopyTab { var pageName = /[\/=]Template(?:%3A|:)(User[_ ][^?&#]+)/i.exec(window.location.href); if (!pageName) return; pageName = decodeURIComponent(pageName[1]); addLink("p-cactions",               "http://en.wikipedia.org/w/index.php?title="                + encodeURIComponent(window.userboxPrefix + pageName)                + "&action=edit&unsubstuserbox=1&preload="                + encodeURIComponent("Template:" + pageName),                "copy for substing",                "ca-copyuserbox",                "Copy userbox to "+window.userboxPrefix+" for subst:ing"); }

function addFixLinks { if (window.location.href.indexOf("Special:Whatlinkshere") < 0) return; var userBoxesToSubst = window.userboxRegExp.replace(/\./g, "\\.").replace(/\s/g, "[_ ]"); if (!new RegExp("([=/]Special:Whatlinkshere/|&target=)Template:User[_ ]("+userBoxesToSubst+")").test(unescape(window.location.href))) return; var links = document.getElementById('content').getElementsByTagName('a'); for (var i = 0; i < links.length; i++) { if (!links[i].title || !links[i].title.match(/^User/) ||               !links[i].nextSibling || links[i].nextSibling.nodeType != 3 ||                links[i].nextSibling.nodeValue.indexOf("(inclusion)") < 0) continue; var fixLink = document.createElement('a'); fixLink.href = "http://en.wikipedia.org/w/index.php?title=" + encodeURIComponent(links[i].title) + "&action=edit&fixuserboxes=1"; fixLink.appendChild(document.createTextNode("subst")); links[i].parentNode.insertBefore(fixLink, links[i].nextSibling); links[i].parentNode.insertBefore(document.createTextNode(" ("), fixLink);           links[i].parentNode.insertBefore(document.createTextNode(") "), fixLink.nextSibling); }   }

if (editForm && window.location.search && window.location.search.match(/[?&]action=edit(&|$)/)) { if (window.location.search.match(/[?&]unsubstuserbox=/)) unSubstUserbox; if (window.location.search.match(/[?&]fixuserboxes=/)) fixUserboxes; }   if (unescape(window.location.href).match(/[\/=]Template:User[_ ]/)) { //alert("Hey! This looks like a userbox!"); if (!window.location.search.match(/[?&]unsubstuserbox=/)) addCopyTab; if (window.location.href.indexOf("Special:Whatlinkshere") > 0) addFixLinks; } });

//