User:Anrie Nord/monobook.js

/* * monobook.js * Anrie Nord, 2006-01-12 * * Based on datetime.js, autoreplace.js and advanced_sig.js scripts * by Quarl . */

//

autoreplace_on = true; autoreplace_replacements = Array;

function hookEventObj(obj, hookName, hookFunct) { if (!obj) return;

if (obj.addEventListener) obj.addEventListener(hookName, hookFunct, false); else if (obj.attachEvent) obj.attachEvent("on" + hookName, hookFunct); }

function autoreplaceEntry { hookEventObj(document.editform, 'submit', autoreplace_execute); }

function autoreplace_execute { if (!autoreplace_on) return;

textbox = document.getElementById("wpTextbox1"); if (!textbox) return; textbox.value = autoreplace_replace_strings(textbox.value);

// Since we're submitting a form, we shouldn't get here again. Disable in case of recurring bugs. autoreplace_on = false; }

function autoreplace_replace_strings(text) { if (!text) return text; for (i in autoreplace_replacements) { r = autoreplace_replacements[i]; text = autoreplace_replace_string_nonowiki(text, r[0], autoreplace_f_to_str(r[1])); }   return text; }

function autoreplace_replace_string_nonowiki(text, str, replacement) { var rtext = ''; while ( text.match(/ (?:.|\n)*?<\/nowiki>/) ) { // save these before they get clobbered! var left = RegExp.leftContext; var match = RegExp.lastMatch; var right = RegExp.rightContext;

rtext += autoreplace_replace_string(left, str, replacement) + match; text = right; }   rtext += autoreplace_replace_string(text, str, replacement) return rtext; }

function autoreplace_replace_string(text, str, replacement) { return text.replace(new RegExp(str,'g'), replacement); }

function autoreplace_f_to_str(t) { if (typeof t == 'function') t = t; return ""+t; }

function autoreplace_add(str, replacement) { autoreplace_replacements.push([str,replacement]); }

addOnloadHook(autoreplaceEntry);

/////////////////

function LZ(x) { return (x>=10||x<0?"":"0") + x }

function datestampUTCISO { var d=new Date; return ""+d.getUTCFullYear+"-"+LZ(d.getUTCMonth+1)+"-"+LZ(d.getUTCDate); }

function timestampUTCISO { var d=new Date; return LZ(d.getUTCHours)+":"+LZ(d.getUTCMinutes); }

/////////////////

function replaceSignatureOnSubmit { autoreplace_add("~", makeSignature); }

function getUsername { // read username from pt-userpage link. // Quarl return document.getElementById('pt-userpage').getElementsByTagName('a')[0].text; }

function makeSignature { var user = getUsername; return " " + user + " (talk • contribs) "+datestampUTCISO+" "+timestampUTCISO+"Z "; }

addOnloadHook(replaceSignatureOnSubmit);

//