User:Rezonansowy/EasyTalkback.js

function TalkbackLink() {

if($("#mw-content-text").length > 0) {

var $nodeList = $('#bodyContent a[href^="/wiki/User:"]'), pdne = ' (page does not exist)';

if($nodeList !== null) {

for( var i = 0; i < $nodeList.length; i++ ) {

var $link = $($nodeList[i]);

$newLink = $("[TB]");

$newLink.click(function(e) {talkbackSubmit($(this).attr('href').substr(1), $link[0].title.replace(pdne, "").tostring)});

$($link).after($newLink);

}

}

}

}

function talkbackSubmit(page, user) {

var questionTitle = confirm('Send a talkback?');

var sectionContent = '{{talkback|' + mw.config.get('wgPageName') + '|';

if (questionTitle) {

sectionContent += "ts=~~~~~}}";

sectionContent += '\n~~~~';

}

else {

$("#"+id).attr("title", 'You canceled the talkback!').tipsy("show");

return;

};

var data = {

format : 'json',

action : 'edit',

minor : false,

title : page,

text : sectionContent,

section : 'new',

summary : 'Talkback',

token : mw.user.tokens.get('editToken')

};

$.ajax({

url : mw.util.wikiScript('api'),

type : 'POST',

dataType : 'json',

data : data,

success : function(data) {

if(data && data.edit && data.edit.result && data.edit.result === 'Success') {

mw.util.jsMessage( 'Talkback on ' + user + ' posted!' );

} else {

alert('There was an error requesting the page edit. Code: ' + data.error.code + '": ' + data.error.info);

}

},

error : function() {

alert('There was an error using AJAX to edit the page.');

}

});

}

addOnloadHook(TalkbackLink);