Jump to content

User:Ilmari Karonen/userboxes.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ilmari Karonen (talk | contribs) at 22:38, 11 May 2006 (skip redundant if= parameter for Babel-X). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
/* 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-(\\d+|X)\\s*\\|(([^{}|]*\\|)*\\s*(" + userBoxesToSubst + ")\\s*(\\|[^{}|]*)*)\\}\\}");
    while (true) {
        var babelMatch = babel_re.exec(newText);
        if (!babelMatch || babelMatch.length < 3) break;
        var babelOrig = babelMatch[0];
        var babelSubst = babelMatch[3];
        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>