User:Writ Keeper/Scripts/teahouseTalkbackLink.js
Appearance
< User:Writ Keeper | Scripts
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/teahouseTalkbackLink. |
function teahouseTalkbackLink()
{
if ($("#mw-content-text").length > 0)
{
var $nodeList = $('a[title^="User talk:"]'),
pdne = ' (page does not exist)';
if ($nodeList !== null)
{
var i;
for (i = 0; i < $nodeList.length; i++)
{
var $link = $($nodeList[i]);
var newLinkString = "|";
var redlink = false;
var newLinkHref = $link[0].title
if(newLinkHref.includes(pdne))
{
redlink = true;
newLinkHref = newLinkHref.replace(pdne, "");
}
var newLinkId = "TBsubmit" + i;
newLinkString += '<a href=' + (($link[0].href).replace("User_talk:", "Special:Contributions/")) + ' style="font-size:x-small; display:inline-block;">C</a>|'
newLinkString += '<a style="font-size:x-small; display:inline-block;" title="Send a talkback!" noPopup=1 id="' + newLinkId + '">TB</a>|';
$($link).after(newLinkString)
$("#" + newLinkId).click({"page":newLinkHref, "redlink":redlink}, talkbackSubmit);
}
}
}
}
function talkbackSubmit(event)
{
var page = event.data.page;
var redlink = event.data.redlink;
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|";
if (questionTitle === null)
{
return;
}
else if (questionTitle === "")
{
sectionTitle += "ts=~~" + "~~}}";
}
else
{
sectionTitle += questionTitle + "|ts=~~" + "~~}}";
}
if(redlink)
{
sectionTitle = "== Teahouse talkback: you've got messages! ==\n" + sectionTitle;
var requestData =
{
summary: "Teahouse talkback: you've got messages!"
}
new mw.Api().create(page, requestData, sectionTitle).done(function(data)
{
if (data && data.result && data.result === 'Success')
{
alert("Talkback posted!");
}
else
{
alert('There was an error requesting the page create. Code: ' + data);
}
}).fail(function(data) {
alert('There was an error using AJAX to create the page. Error: ' + data);
});
}
else
{
var requestData =
{
minor: false,
text: sectionTitle,
section: 'new',
summary: "Teahouse talkback: you've got messages!"
};
new mw.Api().edit(page, function() {
return requestData;
}).done(function(data) {
if (data && data.result && data.result === 'Success')
{
alert("Talkback posted!");
}
else
{
alert('There was an error requesting the page edit. Code: ' + data);
}
}).fail(function(data) {
alert('There was an error using AJAX to edit the page. Error: ' + data);
});
}
}
if (mw.config.get('wgPageName') === "Wikipedia:Teahouse")
{
mw.loader.using("mediawiki.api", function()
{
teahouseTalkbackLink();
});
}