Jump to content

User:Anomie/unsignedhelper.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Anomie (talk | contribs) at 17:22, 11 December 2011 (new script, still in testing). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
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[t.getUTCMonth()]+' '+ts.getUTCFullYear()+' (UTC)';
        if(anon){
            return '<span style="font-size: smaller;" class="autosigned">— Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/'+user+'|'+user+']] ([[User talk:'+user+'|talk]]) '+ts+'</span><!-- Template:Unsigned IP -->';
        } else {
            return '<small><span class="autosigned">— Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[User:'+user+'|'+user+']] ([[User talk:'+user+'|talk]] • [[Special:Contributions/'+user+'|contribs]]) '+ts+'</span></small><!-- Template:Unsigned -->';
        }
    },

    addUnsignedTemplate:function(){
        var f=document.getElementById('editform');
        var e=$(f.elements.wpTextbox1);
        var pos=e.getCaretPosition();
        if(pos<=0) pos=e.value.length;
        var 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\)'), '');

        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
            },
            success:function(r,sts,xhr){
                if(!r.query || !r.query.pages){
                    if(typeof(window.console)=='undefined' || typeof(window.console.error)!='function')
                        throw new Error('Bad response');
                    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){
                        var t=UnsignedHelper.makeUnsignedTemplate(user,ts,anon);
                        e.value=e.value.substr(0,pos)+t+e.value.substr(pos);
                        e.setSelection({ start:pos+t.length });
                    } else {
                        user=rr.user;
                        ts=rr.timestamp;
                        anon=(typeof(rr.anon)!='undefined');
                        if(r['query-continue'] && r['query-continue'].revisions){
                            q.data.rvstartid=r['query-continue'].revisions.rvstartid;
                            $.ajax(q);
                        } else {
                            var t=UnsignedHelper.makeUnsignedTemplate(user,ts,anon);
                            e.value=e.value.substr(0,pos)+t+e.value.substr(pos);
                            e.setSelection({ start:pos+t.length });
                        }
                    }
                    return;
                }
            },
            error:function(xhr,textStatus,errorThrown){
                throw new Error('AJAX error: '+textStatus+' '+errorThrown);
            }
        };
        if(f.elements.baseRevId) q.data.rvstartid=f.elements.baseRevId.value;
        $.ajax(q);
        return false;
    },

    onLoad:function(){
        var a=document.createElement('A');
        a.href='#';
        a.appendChild(document.createTextNode('[+\x7b\x7bunsigned\x7d\x7d at cursor]'));
        $(a).click(UnsignedHelper.addUnsignedTemplate);
        $('.editCheckboxes').appendChild(document.createTextNode(' '));
        $('.editCheckboxes').appendChild(a);
    }
};
$(UnsignedHelper.onLoad);