Jump to content

User:Writ Keeper/Scripts/ptTalkbackLink.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.
// <nowiki>
$(document).ready(teahouseTalkbackLink);

function teahouseTalkbackLink()
{
	if(mw.config.get("wgPageName") == "Wikipédia:Café_dos_novatos")
	{
		var contentBody = document.getElementById("mw-content-text");
		if(contentBody != null)
		{
			var nodeList = contentBody.getElementsByTagName("a");
			
			if(nodeList != null)
			{
				var i;
				for(i = 0; i < nodeList.length; i++)
				{
					var link = nodeList[i];
					var targetNameStart = link.href.search(/Usu%C3%A1ri(o_|a_|o\(a\)_)Discuss%C3%A3o:/);
					var targetNameEnd = link.href.search(/[#&]/);
					var targetName;
					if(targetNameStart < 0)
					{
						continue;
					}
					if(targetNameEnd > 0)
					{
						targetName = link.href.substring(targetNameStart, targetNameEnd);
					}
					else
					{
						targetName = link.href.substring(targetNameStart);
					}
					if(targetName.search("/") > 0)
					{
						continue;
					}
						var newLink = document.createElement("form");
						newLink.style.display = "inline-block";
						newLink.method = "post";
						newLink.action = mw.config.get("wgServer") + "/wiki/" + targetName + "?action=submit&section=new";
						newLink.id = "TBSubmit" + i;
						newLink.innerHTML = '|<a href="#" onclick="talkbackSubmit(' + i + '); return;" style="font-size:x-small;" title=""Mande um aviso de mensagem!">TB</a>|';
						addAfter(newLink, link);
						i++;
					}
				}
			}
		}
	}
}
function talkbackSubmit(i)
{
	submitForm = document.getElementById("TBSubmit" + i);
	var timestamp = getMWTimestamp();
	var questionTitle = prompt("Por favor coloque o título da seção que aonde está a resposta (ou deixe em branco):","");
	var sectionTitle = "{{Resposta|WP:Café dos novatos|";
	if(questionTitle == null)
	{
		return;
	}
	if(questionTitle == "")
	{
		sectionTitle +=	"ts=~~"+"~~}}";
	}
	else
	{
		sectionTitle += questionTitle +"|ts=~~"+"~~}}";
	}
	submitForm.innerHTML = '<input type="hidden" name="wpTextbox1" value="'+sectionTitle+'">\n<input type="hidden" name="wpSection" value="new">\n<input type="hidden" name="wpEditToken" value="' + mw.user.tokens.get( 'csrfToken' ) + '">\n<input type="hidden" name="wpStarttime" value="' + timestamp + '">\n<input type="hidden" name="wpEdittime" value="' + timestamp + '">\n<input type="hidden" name="wpSummary" value="Aviso de mensagem no Café dos novatos: você tem uma mensagem!">';
	submitForm.submit();
}

function addAfter(newNode, oldNode)
{
	if(oldNode.nextSibling != null)
	{
		oldNode.parentNode.insertBefore(newNode, oldNode.nextSibling);
	}
	else
	{
		oldNode.parentNode.appendChild(newNode);
	}
}

function getMWTimestamp()
{
	var date = new Date();
	var month = "" + (date.getUTCMonth() + 1);
	var day = "" + date.getUTCDate();
	var hours = "" + date.getUTCHours();
	var minutes = "" + date.getUTCMinutes();
	var seconds = "" + date.getUTCSeconds();
	
	if(month.length != 2)
	{
		month = "0" + month;
	}
	if(day.length != 2)
	{
		day = "0" + day;
	}
	if(hours.length != 2)
	{
		hours = "0" + hours;
	}
	if(minutes.length != 2)
	{
		minutes = "0" + minutes;
	}
	if(seconds.length != 2)
	{
		seconds = "0" + seconds;
	}
	return "" + date.getUTCFullYear() + month + day + hours + minutes + seconds;
}

					{
						targetName = link.href.substring(targetNameStart, targetNameEnd);
					}
					else
					{
						targetName = link.href.substring(targetNameStart);
					}
					if(targetName.search("/") > 0)
					{
						continue;
					}
						var newLink = document.createElement("form");
						newLink.style.display = "inline-block";
						newLink.method = "post";
						newLink.action = mw.config.get("wgServer") + "/wiki/" + targetName + "?action=submit&section=new";
						newLink.id = "TBSubmit" + i;
						newLink.innerHTML = '|<a href="#" onclick="talkbackSubmit(' + i + '); return;" style="font-size:x-small;" title="Send a talkback!">TB</a>|';
						addAfter(newLink, link);
						i++;
					}
				}
			}
		}
	}
}

function talkbackSubmit(i)
{
	submitForm = document.getElementById("TBSubmit" + i);
	var timestamp = getMWTimestamp();
	var questionTitle = prompt("Please enter the title of the question you're replying to (or just leave it blank):","");
	var sectionTitle = "{{Wikipedia:Teahouse/Teahouse talkback|WP:Teahouse/Questions|";
	if(questionTitle == null)
	{
		return;
	}
	if(questionTitle == "")
	{
		sectionTitle +=	"ts=~~"+"~~}}";
	}
	else
	{
		sectionTitle += questionTitle +"|ts=~~"+"~~}}";
	}
	submitForm.innerHTML = '<input type="hidden" name="wpTextbox1" value="'+sectionTitle+'">\n<input type="hidden" name="wpSection" value="new">\n<input type="hidden" name="wpEditToken" value="' + mw.user.tokens.get( 'csrfToken' ) + '">\n<input type="hidden" name="wpStarttime" value="' + timestamp + '">\n<input type="hidden" name="wpEdittime" value="' + timestamp + '">\n<input type="hidden" name="wpSummary" value="Teahouse talkback: you\'ve got messages!">';
	submitForm.submit();
}

function addAfter(newNode, oldNode)
{
	if(oldNode.nextSibling != null)
	{
		oldNode.parentNode.insertBefore(newNode, oldNode.nextSibling);
	}
	else
	{
		oldNode.parentNode.appendChild(newNode);
	}
}

function getMWTimestamp()
{
	var date = new Date();
	var month = "" + (date.getUTCMonth() + 1);
	var day = "" + date.getUTCDate();
	var hours = "" + date.getUTCHours();
	var minutes = "" + date.getUTCMinutes();
	var seconds = "" + date.getUTCSeconds();
	
	if(month.length != 2)
	{
		month = "0" + month;
	}
	if(day.length != 2)
	{
		day = "0" + day;
	}
	if(hours.length != 2)
	{
		hours = "0" + hours;
	}
	if(minutes.length != 2)
	{
		minutes = "0" + minutes;
	}
	if(seconds.length != 2)
	{
		seconds = "0" + seconds;
	}
	return "" + date.getUTCFullYear() + month + day + hours + minutes + seconds;
}
// </nowiki>