Saltar para o conteúdo

Usuário:Chicocvenancio/linkResposta.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)
  • Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5.
function teahouseTalkbackLink() {
	"use strict";
	if ($("#mw-content-text").length > 0) {
		var $nodeList = $('a[title*="Discussão:"][title^="Usuári"]'), pdne = ' (página inexistente)', i, $link, $newLink;
		if ($nodeList !== null) {
			for (i = 0; i < $nodeList.length; i += 1) {
				$link = $($nodeList[i]);
				$newLink = $('<a href="#' + $link[0].title.replace(pdne, "") + '" style="font-size:x-small; display:inline-block;" title="Mande um aviso de resposta!" noPopup=1 id="TBsubmit' + i + '">R</a>');
				$($link).after($newLink);
			}
		}
		$('a[title="Mande um aviso de resposta!"]').tipsy({html: true}).before("|").after("|").click(function (e) {
			talkbackSubmit($(this).attr('href').substr(1), this.id);
		});
	}
}
function talkbackSubmit(page, id) {
	"use strict";
	var questionTitle = prompt("Por favor coloque o título da seção aonde está a resposta (ou deixe em branco):", ""), sectionTitle = "{{Resposta|" + mw.config.get('wgPageName') + "|", data;
	if (questionTitle === null) {
		$("#" + id).attr("title", 'Você cancelou o envio da mensagem!').tipsy("show");
		return;
	} else if (questionTitle === "") {
		sectionTitle += "ts=~~" + "~~}}";
	} else {
		sectionTitle += questionTitle + "|ts=~~" + "~~}}";
	}
	data = {
		format : 'json',
		action : 'edit',
		minor : false,
		title : page,
		text : sectionTitle,
		section : 'new',
		summary : "Resposta: 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('wgNamespaceNumber') % 2 ===  1) && mw.config.get('wgNamespaceNumber') > 0 || mw.config.get('wgNamespaceNumber') === 4) && mw.config.get('wgAction') === 'view') {
	mw.loader.using('jquery.tipsy', function () { $(teahouseTalkbackLink); });
}