User:Writ Keeper/Scripts/ptTalkbackLink.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Writ Keeper/Scripts/ptTalkbackLink. |
// <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§ion=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§ion=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>