Jump to content

User:Rezonansowy/EasyTalkback.js

From Wikipedia, the free encyclopedia
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.
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 = $("<sup><a href=#" + $link[0].title.replace(pdne, '') + " style='font-size:xx-small; display:inline-block;' title='Send a talkback!' noPopup=1 class='TBsubmit' + i + '>[TB]</a></sup>");
				$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=<nowiki>~~~~~</nowiki>}}";
		sectionContent += '\n<nowiki>~~~~</nowiki>';	
	}
	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 <b>' + user + '</b> 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);