User:HighInBC/unsignedhelper.js

var UnsignedHelper={ months:['January','February','March','April','May','June','July','August','September','October','November','December'],

makeUnsignedTemplate:function(user,ts,anon){ ts=new Date(ts); var h=ts.getUTCHours; if(h<10) h='0'+h; var m=ts.getUTCMinutes; if(m<10) m='0'+m; ts=h+':'+m+', '+ts.getUTCDate+' '+UnsignedHelper.months[ts.getUTCMonth]+' '+ts.getUTCFullYear+' (UTC)'; if(anon){ return '\x7b\x7bsubst:UnsignedIP|'+user+'|'+ts+'\x7d\x7d'; } else { return '\x7b\x7bsubst:Unsigned|'+user+'|'+ts+'\x7d\x7d'; }   },

addUnsignedTemplate:function(evt){ mw.loader.using(['mediawiki.util','jquery.ui'], function{           var f=document.getElementById('editform');            var e=f.elements.wpTextbox1;            var pos=$(e).textSelection('getCaretPosition', {startAndEnd:true});            var txt;            if(pos[0]!=pos[1]){                txt=e.value.substring(pos[0],pos[1]);                pos=pos[1];            } else {                pos=pos[1];                if(pos<=0) pos=e.value.length;                txt=e.value.substr(0,pos);                txt=txt.replace(new RegExp('[\\s\\S]*\\d\\d:\\d\\d, \\d+ ('+UnsignedHelper.months.join('|')+') \\d\\d\\d\\d \\(UTC\\)'), );                txt=txt.replace(/[\s\S]*\n=+.*=+\s*\n/, );            }            txt=txt.replace(/^\s+|\s+$/g, '');

var rvct=1; var dialog=$(' Examining revision 1... ').dialog({               buttons:{ Cancel:function{ dialog.dialog('close'); } },                modal:true,                title:'Adding \x7b\x7bunsigned\x7d\x7d'            });

var user, ts, anon; var q={ url:mw.util.wikiScript('api'), dataType:'json', type:'POST', data:{ format:'json', action:'query', titles:mw.config.get('wgPageName'), prop:'revisions', rvprop:'timestamp|user|content', rvlimit:1, rawcontinue:1 },               success:function(r,sts,xhr){ if(!dialog.dialog('isOpen')) return;

if(!r.query || !r.query.pages){ dialog.html(' Error: Bad response from API '); if(window.console && typeof(window.console.error)=='function') window.console.error("Bad response", r); return; }

for(var k in r.query.pages){ var rr=r.query.pages[k].revisions[0]; if(typeof(rr['*'])!='undefined' && rr['*'].indexOf(txt)<0){ if(!user){ dialog.html(' Error: Text was not found in the starting revision! Did you edit it? '); return; }                           var t=UnsignedHelper.makeUnsignedTemplate(user,ts,anon); var tt=e.value.substr(0,pos).replace(/\s*$/,' ')+t; e.value=tt+e.value.substr(pos); $(e).textSelection('setSelection', { start:tt.length }); dialog.dialog('close'); } else { user=rr.user; ts=rr.timestamp; anon=(typeof(rr.anon)!='undefined'); if(r['query-continue'] && r['query-continue'].revisions){ dialog.html('Evaluating revision '+(++rvct)+'...'); q.data.rvcontinue=r['query-continue'].revisions.rvcontinue; $.ajax(q); } else { var t=UnsignedHelper.makeUnsignedTemplate(user,ts,anon); var tt=e.value.substr(0,pos).replace(/\s*$/,' ')+t; e.value=tt+e.value.substr(pos); $(e).textSelection('setSelection', { start:tt.length }); dialog.dialog('close'); }                       }                        return; }                   dialog.html(' Error: No revisions found in the page! '); },               error:function(xhr,textStatus,errorThrown){ if(!dialog.dialog('isOpen')) return; dialog.html(' Error: '+textStatus+' '+errorThrown+' '); }           };            if(f.elements.baseRevId) q.data.rvstartid=f.elements.baseRevId.value; $.ajax(q); });       evt.preventDefault;        evt.stopPropagation;        return false;    } };

if(!window.charinsertCustom) window.charinsertCustom={}; if(!window.charinsertCustom['Insert']) window.charinsertCustom['Insert']=''; window.charinsertCustom['Insert']+=' \x7b\x7bunsigned\x7d\x7d\x10UnsignedHelper.addUnsignedTemplate'; if(!window.charinsertCustom['Wiki markup']) window.charinsertCustom['Wiki markup']=''; window.charinsertCustom['Wiki markup']+=' \x7b\x7bunsigned\x7d\x7d\x10UnsignedHelper.addUnsignedTemplate'; if(window.updateEditTools) window.updateEditTools;