User:Writ Keeper/Scripts/teahouseUtility-SarahFlavor.js
Appearance
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/teahouseUtility-SarahFlavor. |
/* <nowiki>
This script adds entries for posting Teahouse talkbacks and invites into the page action menu (and adds the page action menu itself, if it's not present)
*/
//standard document.ready
$(document).ready(teahouseMenu);
//add menu if it's not there; populate it with new buttons
function teahouseMenu()
{
if(mw.config.get("wgCanonicalNamespace") == "User" || mw.config.get("wgCanonicalNamespace") == "User_talk")//Only if we're on a user or user talk page
{
var inviteLink = mw.util.addPortletLink("p-cactions", "#", "THInvite", "th-invite", "Add a Teahouse invite template", null, "#ca-move");
var afcLink = mw.util.addPortletLink("p-cactions", "#", "THInvite (AfC)", "th-afc", "Add a Teahouse invite template", null, "#ca-move");
var talkbackLink = mw.util.addPortletLink("p-cactions", "#", "THTalkback", "th-talkback", "Add a Teahouse talkback template", null, "#ca-move");
$(inviteLink).click(postTHInvite);
$(afcLink).click(postTHafc);
$(talkbackLink).click(postTHTalkback);
}
}
function populateListEntry(listEntry)
{
var form = document.createElement("form");
var slashLocation = mw.config.get("wgTitle").indexOf("/");
form.style.display = "inline-block";
form.method = "POST";
if(slashLocation > 0)
{
form.action = "http://en.wikipedia.org/wiki/User_talk:" + mw.config.get("wgTitle").substring(0, mw.config.get("wgTitle").indexOf("/")) + "?action=submit§ion=new";
}
else
{
form.action = "http://en.wikipedia.org/wiki/User_talk:" + mw.config.get("wgTitle") + "?action=submit§ion=new";
}
listEntry.appendChild(form);
return form;
}
function postTHInvite()
{
invitationBody = "{{Wikipedia:Teahouse/Invitation}} I, and the rest of the hosts, would be more than happy to answer any questions you have! ~~~~";
//If you want to change what the invitation section is titled, edit the words below within the doublequote (but don't take out the doublequote!)
invitationSectionTitle = "Welcome to Wikipedia: check out the Teahouse!"
var timestamp = getMWTimestamp();
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;
}
}
$.post(getSubmitURL(true), {wpTextbox1 : invitationBody, wpSection : "new", wpEditToken : mw.user.tokens.get('csrfToken'), wpStarttime : timestamp, wpEdittime : timestamp, wpSummary : invitationSectionTitle}, function(){ alert("Invitation posted!"); window.location = getSubmitURL(false);});
}
function postTHafc()
{
invitationBody = "{{subst:#ifexist: User:" + mw.config.get("wgUserName") + "/Teahouse AfC invite | {{subst:User:" + mw.config.get("wgUserName") + "/Teahouse AfC invite}} | {{Wikipedia:Teahouse/AfC Invitation|sign=~~~~}} }}";
//If you want to change what the invitation section is titled, edit the words below within the doublequote (but don't take out the doublequote!)
invitationSectionTitle = "Welcome to Wikipedia! Need a hand?"
var timestamp = getMWTimestamp();
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;
}
}
$.post(getSubmitURL(true), {wpTextbox1 : invitationBody, wpSection : "new", wpEditToken : mw.user.tokens.get('csrfToken'), wpStarttime : timestamp, wpEdittime : timestamp, wpSummary : invitationSectionTitle}, function(){ alert("Invitation posted!"); window.location = getSubmitURL(false);});
}
function postTHTalkback()
{
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=~~"+"~~}}";
}
else
{
sectionTitle += questionTitle +"|ts=~~"+"~~}}";
}
var timestamp = getMWTimestamp();
$.post(getSubmitURL(true), {wpTextbox1 : "{{Wikipedia:Teahouse/Teahouse talkback|WP:Teahouse/Questions|"+ questionTitle +"|ts=~~~~}}", wpSection : "new", wpEditToken : mw.user.tokens.get('csrfToken'), wpStarttime : timestamp, wpEdittime : timestamp, wpSummary : "Teahouse talkback: you\'ve got messages!"}, function(){ alert("Talkback posted!"); window.location = getSubmitURL(false);});
}
function getMWTimestamp()
{
var date = new Date();
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;
}
function getSubmitURL(moreArgs)
{
var submitURL = mw.config.get("wgServer") + "/wiki/User talk:";
if(mw.config.get("wgTitle").indexOf("/") > 0)
{
submitURL += mw.config.get("wgTitle").substring(0, mw.config.get("wgTitle").indexOf("/"));
}
else
{
submitURL += mw.config.get("wgTitle");
}
if(moreArgs)
{
submitURL += "?action=submit§ion=new";
}
return submitURL;
}
//</nowiki>