User:Ilmari Karonen/userboxes.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:Ilmari Karonen/userboxes. |
/* This user script allows the semiautomatic substitution of deleted userboxes on user talk pages.
To enable it, add the following four lines to [[User:(YOUR_NAME)/monobook.js]]:
// [[User:Ilmari Karonen/userboxes.js]]
document.write('<'+'script type="text/javascript" src="'
+ 'http://en.wikipedia.org/w/index.php?title=User:Ilmari Karonen/userboxes.js'
+ '&action=raw&ctype=text/javascript&dontcountme=s"><'+'/script>');
You may also assign you own regular expression to window.userboxRegExp. This will override the default expression below.
<pre><nowiki> */
if (!window.userboxRegExp) window.userboxRegExp = "[Cc]hristian";
addOnloadHook(function () {
var editForm = document.forms.editform;
if (!editForm || !window.location.search ||
!window.location.search.match(/[?&]action=edit(&|$)/) ||
!window.location.search.match(/[?&]fixuserboxes=/) ) return;
var userBoxesToSubst = window.userboxRegExp.replace(/\./g, "\\.").replace(/\s/g, "[_ ]");
var newText = editForm.wpTextbox1.value;
var babel_re = new RegExp ("\\{\\{([Tt]emplate:|msg:)?\\s*(?:[Bb]abel|UBX)-(?:\\d+|X)\\s*\\|(([^{}|]*\\|)*\\s*("
+ userBoxesToSubst + ")\\s*(\\|[^{}|]*)*)\\}\\}");
while (true) {
var babelMatch = babel_re.exec(newText);
if (!babelMatch || babelMatch.length < 2) break;
var babelOrig = babelMatch[0];
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\" | '''[[Wikipedia:Babel]]'''\n|-\n| {{User " + babelSubst + "}}\n|-\n| style=\"text-align: center\" | [[:Category:User languages|Search user languages]]\n|}\n";
//alert("Replacing "+babelOrig+" with:\n"+babelSubst);
newText = newText.replace(babelOrig, babelSubst.replace(/^\|-\n\| \{\{User if=\}\}\n/m, ""));
}
var userbox_re = new RegExp ("\\{\\{([Tt]emplate:|msg:)?\\s*[Uu]ser[ _](" + userBoxesToSubst + ")[|\\s]*\\}\\}", "g");
editForm.wpTextbox1.value = newText.replace(userbox_re, "{{subst:User:Ilmari_Karonen/userboxes/User $2}}");
editForm.wpSummary.value = "[[User:Ilmari Karonen/userboxes.js|Javascript-assisted]] substitution of deleted userboxes";
editForm.wpMinoredit.checked = true;
editForm.wpSave.click(); // I'm feeling lucky!
});
addOnloadHook(function () {
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);
}
});
// </nowiki></pre>