User:Ilmari Karonen/fixuserjslinks.js

// FIX LINKS TO FAKE USER JS PAGES:

var fakeUserRegex = "user[ _]?name|your[ _]?name|example";

if (mw.config.get('wgAction') == "edit" && /[?&]fixuserjslinks=/.test(window.location.search)) { addOnloadHook(function {        var editForm = document.forms.editform;        if (!editForm) return;        var fixRegex = new RegExp ("(your\\s+)?\\[\\\\+\\.(?:js|css))[ _]*(?:\\|\\s*\\2\\s*)?\\]\\](\\s+(?:page|file))?", "ig");        var oldText = editForm.wpTextbox1.value;        var newText = oldText.replace(fixRegex, "$1$2$3");        editForm.wpTextbox1.value = newText;        editForm.wpSummary.value = "simplify instructions using Special:Mypage";        editForm.wpMinoredit.checked = true;        if (oldText != newText) editForm.wpDiff.click;    }); }

if (mw.config.get('wgNamespaceNumber') == -1 && mw.config.get('wgCanonicalSpecialPageName').toLowerCase == "whatlinkshere") { addOnloadHook(function {        var inputElem = document.getElementById("mw-whatlinkshere-target");        var re = new RegExp("^User:(?:" + fakeUserRegex + ")/\\w+\\.(?:js|css)", "i");        if (!inputElem || !re.test(inputElem.value)) return;

var items = document.getElementById('content').getElementsByTagName('li'); for (var i = 0; i < items.length; i++) { var link = items[i].getElementsByTagName('a')[0]; if (!link) continue;

var fixLink = document.createElement('a'); fixLink.href = mw.config.get('wgScript') + "?title=" + encodeURIComponent(link.title) + "&action=edit&fixuserjslinks=1"; fixLink.appendChild(document.createTextNode("fix"));

link.parentNode.insertBefore(fixLink, link.nextSibling); link.parentNode.insertBefore(document.createTextNode(" ("), fixLink);           link.parentNode.insertBefore(document.createTextNode(") "), fixLink.nextSibling); }   }); }