User:Writ Keeper/Scripts/teahouseTalkbackLink.js: Difference between revisions

Content deleted Content added
m oops
copying User:Chicocvenancio's ultracool rewrite (from User:Chicocvenancio/teahouseTalkbackLink.js) using jquery into production code
Line 1:
addOnloadHook$(document).ready(teahouseTalkbackLink);
 
function teahouseTalkbackLink() {
if($("#mw-content-text").length > 0) {
{
var $nodeList = $('a[title^="User talk:"]'), pdne = ' (page does not exist)';
if(wgPageName == "Wikipedia:Teahouse/Questions")
if($nodeList !== null) {
{
var i;
var contentBody = document.getElementById("mw-content-text");
for( i = 0; i < $nodeList.length; i++) {
if(contentBody != null)
var $link = $($nodeList[i]);
{
$newLink = $('<a href="#' + $link[0].title.replace(pdne, "") + '" style="font-size:x-small; display:inline-block;" title="Send a talkback!" noPopup=1 id="TBsubmit' + i + '">TB</a>');
var nodeList = contentBody.getElementsByTagName("a");
$newLink.click(function(e) {
talkbackSubmit($(this).attr('href').substr(1), this.id)
if(nodeList != null)
{ });
$($link).after($newLink);
var i;
for(i = 0; i < nodeList.length; i++)
{
var link = nodeList[i];
var targetNameStart = link.href.search("User_talk");
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 = 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++;
}
}
}
$('a[title="Send a talkback!"]').tipsy({html: true}).before("|").after("|");
}
}
function talkbackSubmit(page, id) {
 
var questionTitle = prompt("Please enter the title of the question you're replying to (or just leave it blank):", ""), sectionTitle = "{{Wikipedia:Teahouse/Teahouse talkback|WP:Teahouse/Questions|";
function talkbackSubmit(i)
if (questionTitle === null) {
{
$("#"+id).attr("title", 'You canceled the talkback!').tipsy("show");
submitForm = document.getElementById("TBSubmit" + i);
return;
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;
}
else if (questionTitle === "") {
sectionTitle += "ts=~~" + "~~}}";
{
}
sectionTitle += "ts=~~"+"~~}}";
else {
sectionTitle += questionTitle + "|ts=~~" + "~~}}";
}
var data = {
else
format : 'json',
{
action : 'edit',
sectionTitle += questionTitle +"|ts=~~"+"~~}}";
minor : false,
}
title : page,
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( 'editToken' ) + '">\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!">';
text : sectionTitle,
submitForm.submit();
section : 'new',
}
summary : "Teahouse talkback: you've got messages!",
 
token : mw.user.tokens.get('editToken')
function addAfter(newNode, oldNode)
};
{
$.ajax({
if(oldNode.nextSibling != null)
url : mw.util.wikiScript('api'),
{
type : 'POST',
oldNode.parentNode.insertBefore(newNode, oldNode.nextSibling);
dataType : 'json',
}
data : data,
else
success : function(data) {
{
if(data && data.edit && data.edit.result && data.edit.result === 'Success') {
oldNode.parentNode.appendChild(newNode);
window.___location = mw.util.wikiGetlink(page);
}
} else {
}
$("#"+id).attr("title", 'There was an error requesting the page edit. Code: ' + data.error.code + '": ' + data.error.info).tipsy("show");
 
}
function getMWTimestamp()
},
{
error : function() {
var date = new Date();
$("#"+id).attr("title", 'There was an error using AJAX to edit the page.').tipsy("show");
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;
}
if(mw.config.get('wgPageName') === "Wikipedia:Teahouse/Questions") {
mw.loader.using( 'jquery.tipsy', function () { $(teahouseTalkbackLink); } );}