User:Splarka/ajaxsendcomment.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Splarka/ajaxsendcomment. |
/* 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.
*/
if(wgNamespaceNumber > 0) addOnloadHook(function() {
addPortletLink('p-cactions','javascript:ascSendComment()','Send comment','ca-asc','Send someone a comment.');
})
function ascSendComment() {
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('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);
}
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;
var params = 'action=edit&format=json§ion=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]';
}