Usuário:Chicocvenancio/linksdeAvisodeMensagemnoCafedosNovatos.js

Origem: Wikipédia, a enciclopédia livre.

Nota: Depois de publicar, poderá ter de contornar a cache do seu navegador para ver as alterações.

  • Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
  • Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
  • Internet Explorer / Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5
  • Opera: Pressione Ctrl-F5.
function teahouseTalkbackLink() {
	if($("#mw-content-text").length > 0) {
		var $nodeList = $('a[title*="Discussão:"][title^="Usuári"]');
		if($nodeList !== null) {
			var i;
			for( i = 0; i < $nodeList.length; i++) {
				var $link = $($nodeList[i]);
					$newLink = $('<a href="#' + $link[0].title + '" style="font-size:x-small; display:inline-block;" title="Mande um aviso de mensagem!" noPopup=1 id="TBsubmit' + i + '">R</a>');
				$newLink.click(function(e) {
					talkbackSubmit($(this).attr('href').substr(1), this.id)
				});
				$($link).after($newLink);
			}
		}
	$('a[title="Mande um aviso de mensagem!"]').tipsy({html: true}).before("|").after("|");
	}
}
function talkbackSubmit(page, id) {
	var questionTitle = prompt("Por favor coloque o título da seção aonde está a resposta (ou deixe em branco):", ""), sectionTitle = "{{Resposta|WP:Café dos novatos|";
	if (questionTitle === null) {
		 $("#"+id).attr("title", 'Você cancelou o envio da mensagem!').tipsy("show");
		 return;
	}
	else if (questionTitle === "") {
		sectionTitle += "ts=~~" + "~~}}";
	} 
	else {
		sectionTitle += questionTitle + "|ts=~~" + "~~}}";
	}
	var data = {
		format : 'json',
		action : 'edit',
		minor : false,
		title : page,
		text : sectionTitle,
		section : 'new',
		summary : "Aviso de mensagem no Café dos novatos: você tem uma mensagem!",
		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') {
				window.location = mw.util.getUrl(page);
			} else {
				$("#"+id).attr("title", 'Houve um erro ao requisitar a edição da página. Código: ' + data.error.code + '": ' + data.error.info).tipsy("show");
			}
		},
		error : function() {
			$("#"+id).attr("title", 'Houve um erro ao usar AJAX para editar a página.').tipsy("show");
		}
	});
}
if(mw.config.get('wgPageName') === "Wikipédia:Café_dos_novatos") {
	mw.loader.using( 'jquery.tipsy', function () { $(teahouseTalkbackLink); } );}