User:Bluemoose/monobook.js/catkey.js

From Wikipedia, the free encyclopedia
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.
//<pre><nowiki>

$( function() { if(!document.forms.editform) { return; }
name = document.getElementsByTagName("h1")[0].innerHTML.replace("Editing ", "").replace(/ \(.*?\)/, "");

intLast = name.lastIndexOf(" ");
lname = name.substr(intLast + 1);

fname = name.substr(0, intLast);
name = lname + (fname ? ", " + fname : "");

txt = document.editform.wpTextbox1.value;
introTxt = txt.match(/\(([^\)]*?[0-9]{4}[^\)]*?)\)/);
if (introTxt) { years = introTxt[1].match(/[0-9]{4}/g); } else { years = []; }

newTxt = "[[Category:|" + name + "]]" + (years[0] ? "\n[[Category:" + years[0] + " births|" + name + "]]" : "") + (years[1] ? "\n[[Category:" + years[1] + " deaths|" + name + "]]" : "");

mwCustomEditButtons[mwCustomEditButtons.length] = {"imageFile": 'http://upload.wikimedia.org/wikipedia/en/b/b1/Button_key.PNG', "speedTip": 'Insert category key', "tagOpen": '', "tagClose": newTxt, "sampleText": ''}; } );

//</nowiki></pre>