Jump to content

User:Writ Keeper/Scripts/orangeBar.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Writ Keeper (talk | contribs) at 14:08, 2 May 2013 (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 orangeBarOfDoom()
{
	var cookieResult = $.cookie("lastTalkpageView");
	if(cookieResult != null)
	{
		var escapedUsername = wgUserName.replace(/ /g, "_");
		if(wgPageName == "User_talk:"+ escapedUsername)
		{
			var d = new Date();
			$.cookie("lastTalkpageView", d.getTime(), {expires:365, path: '/'});
		}
		else
		{
			var data = {action: "query", prop:"revisions", format:"json", rvprop:"timestamp|user|parsedcomment", rvlimit:"1", rvdir:"older", rvexcludeuser: wgUserName, titles: "User talk:"+wgUserName};
			$.post("/w/api.php", data, function(results)
			{
				var index = Object.keys(results.query.pages)[0];
				var lastTimestamp = Date.parse(results.query.pages[index].revisions[0].timestamp);

				if( cookieResult < lastTimestamp )
				{
					var lastUser = results.query.pages[index].revisions[0].user
					var section = /href="(.+?)"/.exec(results.query.pages[index].revisions[0].comment);
					var talkLink = '/wiki/User_talk:'+encodeURIComponent(escapedUsername);
					if(section != null && section.length > 1)
					{
						talkLink = section[1];
					}

					var orangeBarDiv = document.createElement("div");
					orangeBarDiv.innerHTML = 'You have <a href="' + talkLink + " title="User talk:'+wgUserName+'">new messages.</a> Last edit by '+lastUser+'.';
					orangeBarDiv.className = "usermessage";
					$("#contentSub").after(orangeBarDiv);
				}
			})
		}
	}
	else
	{
		var d = new Date();
		$.cookie("lastTalkpageView", d.getTime(), {expires:365, path: '/'});
	}
}
$(document).ready(orangeBarOfDoom);