Jump to content

User:Writ Keeper/Scripts/teahouseTalkbackLink.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
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();
	});
}