User:Abelmoschus Esculentus/Welcome+THinvite.js

// $(document).ready(teahouseMenu); function teahouseMenu {	if(mw.config.get("wgCanonicalNamespace") == "User" || mw.config.get("wgCanonicalNamespace") == "User_talk") {		var inviteLink = mw.util.addPortletLink("p-cactions", "#", "W+THI", "th-invite", "Add a Welcome and Teahouse invite template", null, "#ca-move"); $(inviteLink).click(postmalone); } } function getURL {	var url; var slashLocation = mw.config.get("wgTitle").indexOf("/"); if(slashLocation > 0) {		url = "User_talk:" + mw.config.get("wgTitle").substring(0, mw.config.get("wgTitle").indexOf("/")); }	else {		url = "User_talk:" + mw.config.get("wgTitle"); }	return url; } function postmalone {	invitationBody = ""; invitationSectionTitle = "Check out the Teahouse!"; welcomeBody = ""; var linkArray = document.getElementById("mw-content-text").getElementsByTagName("a"); var i;	for(i = 0; i < linkArray.length; i++) {		if(linkArray[i].href.search("Wikipedia:Teahouse") > -1) {			if(confirm("Someone may have already invited this person to the Teahouse!\n\nAre you sure you want to continue?")) {				break; }			else return; }	}

var data = { format : 'json', action : 'edit', minor : false, title : getURL, text : welcomeBody + '~\n==' + invitationSectionTitle + '==\n' + invitationBody, section : 'new', sectiontitle : 'Welcome!', watchlist : 'nochange', token : mw.user.tokens.get('editToken'), summary : 'Welcome!' };	$.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.href = "https://en.wikipedia.org/wiki/User_talk:" + mw.config.get("wgTitle");			} else {				$("#"+id).attr("title", 'There was an error requesting the page edit. Code: ' + data.error.code + '": ' + data.error.info).tipsy("show");			}		},		error : function {			$("#"+id).attr("title", 'There was an error using AJAX to edit the page.').tipsy("show");		}	}); } //