User:Jarry1250/ajaxsendcomment.js

/* => FORK <= DO NOT USE THIS VERSION YOURSELF IF UNSURE Automated Ajax Talkpage Comment Sender, version [0.1.1] Originally from http://en.wikipedia.org/wiki/User:Splarka/ajaxsendcomment.js Notes:
 * Mostly experimental, but should be fully functional, with some charming quirks.
 * Blanks the textarea after success, don't send the same thing twice!
 * Lazily reuses the call to check if user exists, to also get the token.

function insertAfter(parent, node, referenceNode) { parent.insertBefore(node, referenceNode.nextSibling); }

if(wgNamespaceNumber == 3 || wgNamespaceNumber == 1) addOnloadHook(function { var body = document.getElementById("bodyContent"); var links = document.getElementsByTagName("a"); for(var i = 0; i < links.length; i++){ var href = links[i].href; var reduced = href.replace("http://en.wikipedia.org/wiki/User_talk:", ""); if(href.match("wiki/User_talk") == "wiki/User_talk" && href.match("#") != "#" && href.match(wgUserName) != wgUserName && reduced.match("/") != "/"){

if(links[i].parentNode.getAttribute("id") != null){

if(links[i].parentNode.getAttribute("id").match("ca-") == "ca-"){

continue;

}

}

var reply = document.createElement("a"); reply.href = 'javascript:ascSendComment("' + reduced.replace("_"," ") + '"); javascript:scroll(0,0);'; if(wgNamespaceNumber == 3){ reply.appendChild(document.createTextNode("quick reply")); } else { reply.appendChild(document.createTextNode("quick talkpage message")); } insertAfter(links[i].parentNode, reply, links[i]); insertAfter(links[i].parentNode, document.createTextNode(", "), links[i]); } } }) function ascSendComment(name) { if(!name) name = ""; appendCSS('#asc-form textarea {height:15em;}\n#asc-talkpage {padding:0 1em;}'); var docobj = document.getElementById('bodyContent') || document.getElementById('content') || document.getElementById('mw-content') || document.body; var fieldset = document.createElement('fieldset'); var legend = document.createElement('legend'); legend.appendChild(document.createTextNode('Send ajax comment to user talk page (new section)')); fieldset.appendChild(legend); var form = document.createElement('form'); form.setAttribute('action','javascript:void(0);'); form.setAttribute('id','asc-form'); form.appendChild(document.createTextNode('User: ')); var inp1 = document.createElement('input'); inp1.setAttribute('value',name); inp1.setAttribute('type','text'); inp1.setAttribute('id','asc-user'); addHandler(inp1,'blur',ascCheckPage); form.appendChild(inp1); var span = document.createElement('span'); span.setAttribute('id','asc-talkpage'); form.appendChild(span); form.appendChild(document.createElement('br')); form.appendChild(document.createTextNode('Subject: ')); var inp2 = document.createElement('input'); inp2.setAttribute('type','text'); inp2.setAttribute('id','asc-subject'); form.appendChild(inp2); form.appendChild(document.createElement('br')); var txt = document.createElement('textarea'); txt.setAttribute('id','asc-textbox'); form.appendChild(txt); form.appendChild(document.createElement('br')); var inp3 = document.createElement('input'); inp3.setAttribute('type','checkbox'); inp3.setAttribute('id','asc-watch'); form.appendChild(inp3); var lab3 = document.createElement('label'); lab3.setAttribute('for','asc-watch') lab3.appendChild(document.createTextNode('watch ')); form.appendChild(lab3); var inp4 = document.createElement('input'); inp4.setAttribute('type','checkbox'); inp4.setAttribute('id','asc-minor'); form.appendChild(inp4); var lab4 = document.createElement('label'); lab4.setAttribute('for','asc-minor') lab4.appendChild(document.createTextNode('minor ')); form.appendChild(lab4); form.appendChild(document.createElement('br')); var inp5 = document.createElement('input'); inp5.setAttribute('type','button'); inp5.setAttribute('id','asc-submit'); inp5.setAttribute('value','Send'); addHandler(inp5,'click',function { ascCheckPage(false,true); }); inp5.setAttribute('disabled','disabled'); form.appendChild(inp5); fieldset.appendChild(form); docobj.insertBefore(fieldset,docobj.firstChild);

if(name != ""){ ascCheckPage; } } function ascCheckPage(event,doedit) { if(!doedit) var doedit = false var inp = document.getElementById('asc-user'); if(inp.value.replace(/[\s\n\t\|<>\[\]\{\}:#]/g,) == ) return document.getElementById('asc-submit').setAttribute('disabled','disabled'); var out = document.getElementById('asc-talkpage'); if(!document.getElementById('mw-spinner-asc-cp')) injectSpinner(out,'asc-cp') inp.value = inp.value.replace(/^User(|[_ ]talk):/i,''); var url = wgScriptPath + '/api.php?action=query&format=json&indexpageids&prop=info|revisions&intoken=edit&rvprop=size&titles=User%20talk:' + encodeURIComponent(inp.value) + '&list=users&ususers=' + encodeURIComponent(inp.value); var req = sajax_init_object; req.open('GET', url, true); req.onreadystatechange = function { if(req.readyState == 4 && req.status == 200) { eval("ascCheckPageCB(" + req.responseText + ",'" + req.responseText.replace(/\'/g,"`") + "'," + doedit + ")"); } } req.send(null); } function ascCheckPageCB(obj,txt,doedit) { removeSpinner('asc-cp'); var out = document.getElementById('asc-talkpage'); while(out.firstChild) out.removeChild(out.firstChild) if(obj['error'] || !obj['query'] || !obj['query']['pageids'] || !obj['query']['pageids'][0] || !obj['query']['pages'][obj['query']['pageids'][0]]) { out.appendChild(document.createTextNode('unexpected response: ' + txt)); return; } var id = obj['query']['pageids'][0]; var page = obj['query']['pages'][id]; if(page['invalid'] == '') { out.appendChild(document.createTextNode('' + page['title'] + ' (invalid username)')); return; } var a = document.createElement('a'); a.setAttribute('href', wgScript + '?title=' + encodeURIComponent(page['title'])); if(page['missing'] == '') a.setAttribute('class','new') a.appendChild(document.createTextNode(page['title'])); out.appendChild(a); if(page['revisions'] && page['revisions'][0]['size']) out.appendChild(document.createTextNode(' (' + page['revisions'][0]['size'] + ' bytes)')) if(obj['query']['users'] && (obj['query']['users'][0]['invalid'] ==  || obj['query']['users'][0]['missing'] == )) out.appendChild(document.createTextNode(' (name is unused, invalid or IP)')) var sub = document.getElementById('asc-submit'); if(page['edittoken'] && doedit) { ascDoEdit(page['title'],page['edittoken']); injectSpinner(sub,'asc-cp-sub'); } else { sub.removeAttribute('disabled'); } } function ascDoEdit(title,token) { var text = document.getElementById('asc-textbox').value; var summary = document.getElementById('asc-subject').value; if(1 == 2){ var oldtext = ""; var url = wgScriptPath + '/api.php?action=query&format=json&indexpageids&prop=revisions&intoken=edit&rvprop=content&titles=User%20talk:' + encodeURIComponent(title.replace("User talk:","")) + '&list=users&ususers=' + encodeURIComponent(title.replace("User talk:","")); var req = sajax_init_object; req.open('GET', url, true); req.onreadystatechange = function { if(req.readyState == 4 && req.status == 200) { var obj = eval("(" + req.responseText + ")") alert(obj); alert(obj['query']); alert(obj["query"]["pages"]); alert(obj["query"]["pages"][0]); alert(obj["query"]["pages"][0]["revisions"]); alert(obj["query"]["pages"][0]["revisions"]["*"]); oldtext = obj["query"]["pages"][0]["revisions"]["*"]; alert(oldtext); if(oldtext.match("/[ ]*" + summary + "[ ]*/i").length == 0){ alert("Section not found"); } else { alert("Section probably found"); } } } req.send(null); } else { var params = 'action=edit&format=json&section=new&title=' + encodeURIComponent(title) + '&text=' + encodeURIComponent(text) + '&token=' + encodeURIComponent(token) + '&summary=' + encodeURIComponent(summary); var watch = document.getElementById('asc-watch').checked; if(watch) params += '&watch=1' var minor = document.getElementById('asc-minor').checked; if(minor) params += '&minor=1' 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("ascEdit(" + req.responseText + ",'" + req.responseText.replace(/\'/g,"`") + "')"); } } req.send(params); } } function ascEdit(obj,txt) { removeSpinner('asc-cp-sub'); document.getElementById('asc-submit').removeAttribute('disabled'); if(obj['error']) { alert('Api error: ' + obj['error']['code'] + ' - ' + obj['error']['info']); return; } else if(obj['edit'] && obj['edit']['result']) { alert('Edit returned as "' + obj['edit']['result'] + '"'); } else { alert('Unexpected response:\n' + txt); return; } document.getElementById('asc-textbox').value = '[Successfully sent]'; }