User:Splarka/quicktag.js

/* Ajax quick page tagger, version [0.0.1c] Originally from http://en.wikipedia.org/wiki/User:Splarka/quicktag.js

Notes: var insertTagButtons = [ { 'label': 'Speedy', 'open' : '{\{speedy|', 'close': '}}', 'def' : 'crap page' },   { 'label': 'Nom', 'open' : '{\{eat}}' } ];
 * Needs to be defined in your user js like so:
 * Closing tag and default text are optional (if omitted it is treated as a parameterless tag).

if(wgNamespaceNumber >= 0 && wgArticleId > 0) addOnloadHook(function { if(!window.insertTagButtons) {    //example    mw.util.addPortletLink('p-cactions','javascript:insertTagOnPage("{\{example|","}}","example");','example tag');  } else {    for(var i=0;i<insertTagButtons.length;i++) {      var inb = insertTagButtons[i];      if(inb.close && inb.close != '') {        mw.util.addPortletLink('p-cactions','javascript:insertTagOnPage("' + inb['open'] + '","' + inb['close'] + '","' + inb['def'] + '");',inb['label']);      } else {        mw.util.addPortletLink('p-cactions','javascript:insertTagOnPage("' + inb['open'] + '");',inb['label']);      }    }  } });

function insertTagOnPage(tagstart,tagend,tagtxtdef) { if(tagend) { var tagtxt = prompt('Inserting: ' + tagstart + '' + tagend,tagtxtdef); if(tagtxt == null) return var tag = tagstart + tagtxt + tagend; } else { var tag = tagstart; var dowant = confirm('Inserting: ' + tag); if(dowant == false) return }

var url = wgScriptPath + '/api.php?action=query&prop=info|revisions&rvprop=timestamp&indexpageids=1&intoken=edit|move&format=json&titles=' + encodeURIComponent(mw.config.get('wgPageName')); var req = sajax_init_object; req.open('GET', url, true); req.onreadystatechange = function { if(req.readyState == 4 && req.status == 200) { eval("insertTagOnPageEdit(" + req.responseText + ",'" + req.responseText.replace(/\'/g,"`") + "','" + tag + "')"); } }  req.send(null); }

function insertTagOnPageEdit(obj,txt,tag) { if(obj['error']) { alert('Error: ' + obj['error']['code'] + ' - ' + obj['error']['info']); return; } if(!obj['query'] || !obj['query']['pageids'] || !obj['query']['pages'][obj['query']['pageids'][0]]) { alert('Unexpected response: ' + txt); return; } var page = obj['query']['pages'][obj['query']['pageids'][0]]; if(!page['edittoken'] || !page['revisions'] || !page['revisions'][0] || !page['revisions'][0]['timestamp']) { alert('Unexpected response: ' + txt); return; } var edittoken = page['edittoken']; var timestamp = page['revisions'][0]['timestamp']; var startstamp = page['starttimestamp'];

var params = 'action=edit&format=json&title=' + encodeURIComponent(mw.config.get('wgPageName')) + '&prependtext=' + encodeURIComponent(tag + '\n') + '&token=' + encodeURIComponent(edittoken) + '&summary=' + encodeURIComponent('Tagging with ' + tag) + '&minor=1&basetimestamp=' + timestamp.replace(/[^\d]/g,''); if(startstamp) params += '&starttimestamp=' + startstamp.replace(/[^\d]/g,'') var url = wgScriptPath + '/api.php'; var req = sajax_init_object; req.open('POST', url, true); req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); req.setRequestHeader('Content-length', params.length); req.setRequestHeader('Connection', 'close'); req.onreadystatechange = function { if(req.readyState == 4 && req.status == 200) { eval("insertTagOnPageEditHandler(" + req.responseText + ",'" + req.responseText.replace(/\'/g,"`") + "')"); } }  req.send(params); }

function insertTagOnPageEditHandler(obj,txt) { if(obj['error']) { alert('Error: ' + obj['error']['code'] + ' - ' + obj['error']['info']); return; } else if(obj['edit'] && obj['edit']['result']) { alert('Edit returned as "' + obj['edit']['result'] + '"'); document.location.href = wgScript + '?title=' + encodeURIComponent(mw.config.get('wgPageName')) + '&action=history'; } else { alert('Unexpected response: ' + txt); return; } }