Jump to content

User:Writ Keeper/Scripts/teahouseTalkbackLink.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Writ Keeper (talk | contribs) at 16:26, 11 October 2019 (test). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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 = "<span class='reply-link-wrapper'>|";
				var newLinkHref = $link[0].title.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 href="#' + newLinkHref + '" style="font-size:x-small; display:inline-block;" title="Send a talkback!" noPopup=1 id="' + newLinkId + '">TB</a>|</span>';
				$($link).after(newLinkString)
				$("#" + newLinkId).click(function (e)
				{
					talkbackSubmit($(this).attr('href').substr(1), this.id)
				});
			}
		}
	}
}
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|";
	if (questionTitle === null)
	{
		return;
	}
	else if (questionTitle === "")
	{
		sectionTitle += "ts=~~" + "~~}}";
	}
	else
	{
		sectionTitle += questionTitle + "|ts=~~" + "~~}}";
	}
	var data =
	{
		format: 'json',
		action: 'edit',
		minor: false,
		title: page,
		text: sectionTitle,
		section: 'new',
		summary: "Teahouse talkback: you've got messages!",
		token: mw.user.tokens.get('editToken')
	};
	$.ajax(
	{
		url: mw.util.wikiScript('api'),
		type: 'POST',
		dataType: 'json',
		data: data,
		success: function (data)
		{
			if (data && data.edit && data.edit.result && data.edit.result === 'Success')
			{
				//window.location = mw.util.getUrl(page);
				alert("Talkback posted!");
			}
			else
			{
				alert('There was an error requesting the page edit. Code: ' + data.error.code + '": ' + data.error.info);
			}
		},
		error: function ()
		{
			alert('There was an error using AJAX to edit the page.');
		}
	}
	);
}
if (mw.config.get('wgPageName') === "Wikipedia:Teahouse")
{
	teahouseTalkbackLink();
}