User:Lyrithya/usp.js

From Uncyclopedia, the content-free encyclopedia
Jump to navigation Jump to search

Note: After saving, you have to bypass your browser's cache to see the changes.

  • Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button, or press Ctrl+F5.
  • Firefox: hold down the Shift key while clicking Reload; alternatively press Ctrl+F5 or Ctrl-Shift-R.
  • Opera, Konqueror and Safari users can just click the Reload button.
  • Chrome: press Ctrl+F5 or Shift+F5
//<nowiki> 

usp = {}

usp.deliver = function() {
	var userIsBot = false;
	var userIsSysop = false;
	if ( wgUserGroups ) {
		for ( var g = 0; g < wgUserGroups.length; ++g ) {
			if ( wgUserGroups[g] == "bot" ) {
				userIsBot = true
			} else if ( wgUserGroups[g] == "sysop" )
				userIsSysop = true
		}
	}
	if (userIsBot) {
		if (document.URL.indexOf("Uncyclopedia:UnSignpost/signup") > 0) 
			addPortletLink('p-cactions', 'javascript:usp.send()', 'Deliver', 'ca-delivery', 'Open a slew of tabs to deliver what may or may not be the current UnSignpost');
		if (document.URL.indexOf("usp=yes") > 0) {
			document.editform.wpSummary.value = 'UnSignpost - {{SUBST:CURRENTDAY}} {{SUBST:CURRENTMONTHNAME}} {{SUBST:CURRENTYEAR}}';
			document.editform.wpMinoredit.checked = false;
			document.editform.wpWatchthis.checked = false;
			document.editform.wpTextbox1.value = '{{Uncyclopedia:UnSignpost/{{SUBST:CURRENTYEAR}}{{SUBST:CURRENTMONTH}}{{SUBST:CURRENTDAY2}}}}\~~~';
			document.editform.wpSave.click();
		}
	} else if (userIsSysop)
		if (document.URL.indexOf("Uncyclopedia:UnSignpost/20") > 0) 
			addPortletLink('p-cactions', 'javascript:usp.go()', 'Deliver', 'ca-delivery', 'Mark this UnSignpost as ready for delivery');
}

YAHOO.util.Event.onContentReady('p-cactions', usp.deliver);

usp.send = function() {
	var arrDocLinks=document.getElementsByTagName("a");
	for (varLink in arrDocLinks) {
		if(arrDocLinks[varLink].href.search("=new") > 0) {
			window.open(arrDocLinks[varLink].href + "&usp=yes");
		}
	}
}

usp.debugOutput = function(str) {
	usp.output += str;
	usp.panel.setBody(usp.output);
}
usp.debugStatus = function(i) {
	var s = (i) ? 'green;"> [OK]' : 'red;"> [FAILED]';
	usp.debugOutput('<span style="font-weight: bold;color:' + s + '</span>');
}
 
usp.go = function() {
	addPortletLink('p-cactions', 'javascript:usp.go()', 'Deliver', 'ca-deliveryu', 'Mark this UnSignpost ready for delivery');
	usp.header = ''; //prompt( 'Header:', '' );
	usp.issue = wgPageName;
	usp.panel = new YAHOO.widget.Panel("usp", { width:"240px", fixedcenter:true, draggable:true, zindex:10, modal:false, visible:false } ); 
	usp.panel.setHeader('Updating, please wait...');
	usp.panel.setFooter('<br /><hr>This script is powered by 100% pure tacos.');
	usp.output = '';
	usp.debugOutput("<br />Loading...");
	usp.panel.render(document.body); 
	usp.panel.show();
	YAHOO.util.Connect.asyncRequest('GET', '/api.php?action=query&titles=Uncyclopedia:UnSignpost/current&prop=revisions|info|links&pllimit=500&rvprop=content&rvlimit=1&format=xml&intoken=edit', usp.update1, null);
}
 
usp.update1 = {
	success: function(o) {
		usp.debugStatus(1);
		usp.debugOutput('<br />Updating <a href=\"/wiki/Uncyclopedia:UnSignpost/current\">current</a>...');
		try {
			//var sectionText = o.responseXML.getElementsByTagName('rev')[0].firstChild.nodeValue;
			sectionText = '#REDIRECT: [[' + usp.issue +  ']]';
		} catch(e) {
			usp.debugStatus(0);
			usp.debugOutput('<br />Could not add text to the page!<br /><a href="javascript:usp.panel.hide()">Close</a>');
			return;
		}
		usp.debugStatus(1);
		usp.debugOutput('<br />Saving page...');
 
		var token = o.responseXML.getElementsByTagName('page')[0].getAttribute('edittoken');
		var post = 'title=Uncyclopedia:UnSignpost/current&token=' + encodeURIComponent(token) + '&text=' + encodeURIComponent(unescape(sectionText));
		YAHOO.util.Connect.asyncRequest('POST', '/api.php?action=edit&format=xml', usp.update2, post);
	},
	failure: function() {
		usp.debugStatus(0);
		usp.debugOutput('<br />Couldn\'t connect to page!<br /><a href="javascript:usp.panel.hide()">Close</a>');
	}
}
 
usp.update2 = {
	success: function(o) {
		try {
			result = (o.responseXML.getElementsByTagName('edit')[0].getAttribute('result') == 'Success') ? 1 : 0;
		} catch(e) {
			result = 0;
		}
		if (result) {
			usp.debugStatus(1);
			usp.debugOutput('<br /><b>Saved!</b>');
			YAHOO.util.Connect.asyncRequest('GET', '/api.php?action=query&titles=User:Fnoodle/vars/unsignpost/header&prop=revisions|info|links&pllimit=500&rvprop=content&rvlimit=1&format=xml&intoken=edit', usp.update3, null);
		} else {
			with(o.responseXML.getElementsByTagName('error')[0]) {
				usp.debugStatus(0);
				usp.debugOutput('<br /><b>Saving failed :(</b><br />Error: ' + getAttribute('code') + ' - ' + getAttribute('info') + '<br /><a href="/wiki/Uncyclopedia:UnSignpost/current">Add manually</a> or <br /><a href="javascript:usp.panel.hide()">close</a>');
			}
		}
	},
	failure: function(o) {
		usp.debugStatus(0);
		usp.debugOutput('<br /><b>Connection failed :(</b><br /><a href="/wiki/Uncyclopedia:UnSignpost/current">Go to page</a> or <br /><a href="javascript:usp.panel.hide()">close</a>');
	}
}

usp.update3 = {
	success: function(o) {
		usp.debugStatus(1);
		if ( usp.header != null ) {
			usp.debugOutput('<br />Updating <a href="/wiki/User:Fnoodle/vars/unsignpost/header">header</a>...');
			try {
				//var sectionText = o.responseXML.getElementsByTagName('rev')[0].firstChild.nodeValue;
				sectionText = usp.header;
			} catch(e) {
				usp.debugStatus(0);
				usp.debugOutput('<br />Could not add text to the page.<br /><a href="javascript:usp.panel.hide()">Close</a>');
				return;
			}
			usp.debugStatus(1);
			usp.debugOutput('<br />Saving page...');
	 
			var token = o.responseXML.getElementsByTagName('page')[0].getAttribute('edittoken');
			var post = 'title=User:Fnoodle/vars/unsignpost/header&token=' + encodeURIComponent(token) + '&summary=Updating&text=' + encodeURIComponent(unescape(sectionText));
			YAHOO.util.Connect.asyncRequest('POST', '/api.php?action=edit&format=xml', usp.update4, post);
		} //else YAHOO.util.Connect.asyncRequest('GET', '/api.php?action=query&titles=User:Fnoodle/vars/unsignpost/deliver&prop=revisions|info|links&pllimit=500&rvprop=content&rvlimit=1&format=xml&intoken=edit', usp.update5, null);
	},
	failure: function() {
		tags.debugStatus(0);
		tags.debugOutput('<br />Couldn\'t connect to page.<br /><a href="javascript:tags.panel.hide()">Close</a>');
	}
}

usp.update4 = {
	success: function(o) {
		try {
			result = (o.responseXML.getElementsByTagName('edit')[0].getAttribute('result') == 'Success') ? 1 : 0;
		} catch(e) {
			result = 0;
		}
		if (result) {
			usp.debugStatus(1);
			usp.debugOutput('<br /><b>Saved!</b>');
			YAHOO.util.Connect.asyncRequest('GET', '/api.php?action=query&titles=User:Fnoodle/vars/unsignpost/deliver&prop=revisions|info|links&pllimit=500&rvprop=content&rvlimit=1&format=xml&intoken=edit', usp.update5, null);
		} else {
			with(o.responseXML.getElementsByTagName('error')[0]) {
				usp.debugStatus(0);
				usp.debugOutput('<br /><b>Saving failed :(</b><br />Error: ' + getAttribute('code') + ' - ' + getAttribute('info') + '<br /><a href="/wiki/Uncyclopedia:UnSignpost/header">Add manually</a> or <br /><a href="javascript:usp.panel.hide()">close</a>');
			}
		}
	},
	failure: function(o) {
		usp.debugStatus(0);
		usp.debugOutput('<br /><b>Connection failed :(</b><br /><a href="/wiki/Uncyclopedia:UnSignpost/header">Go to page</a> or <br /><a href="javascript:usp.panel.hide()">close</a>');
	}
}
usp.update5 = {
	success: function(o) {
		usp.debugStatus(1);
		usp.debugOutput('<br />Marking <a href="/wiki/User:Fnoodle/vars/unsignpost/deliver">to go</a>...');
		try {
			//var sectionText = o.responseXML.getElementsByTagName('rev')[0].firstChild.nodeValue;
			sectionText = '1';
		} catch(e) {
			usp.debugStatus(0);
			usp.debugOutput('<br />Could not add text to the page.<br /><a href="javascript:usp.panel.hide()">Close</a>');
			return;
		}
		usp.debugStatus(1);
		usp.debugOutput('<br />Saving page...');
	 
		var token = o.responseXML.getElementsByTagName('page')[0].getAttribute('edittoken');
		var post = 'title=User:Fnoodle/vars/unsignpost/deliver&token=' + encodeURIComponent(token) + '&summary=Set&text=' + encodeURIComponent(unescape(sectionText));
		YAHOO.util.Connect.asyncRequest('POST', '/api.php?action=edit&format=xml', usp.update6, post);
	},
	failure: function() {
		tags.debugStatus(0);
		tags.debugOutput('<br />Couldn\'t connect to page.<br /><a href="javascript:tags.panel.hide()">Close</a>');
	}
}
usp.update6 = {
	success: function(o) {
		try {
			result = (o.responseXML.getElementsByTagName('edit')[0].getAttribute('result') == 'Success') ? 1 : 0;
		} catch(e) {
			result = 0;
		}
		if (result) {
			usp.debugStatus(1);
			usp.debugOutput('<br /><b>Saved!</b> <a href="javascript:usp.panel.hide()">close</a>');
		} else {
			with(o.responseXML.getElementsByTagName('error')[0]) {
				usp.debugStatus(0);
				usp.debugOutput('<br /><b>Saving failed :(</b><br />Error: ' + getAttribute('code') + ' - ' + getAttribute('info') + '<br /><a href="/wiki/User:Fnoodle/vars/unsignpost/deliver?action=edit">Add manually</a> or <br /><a href="javascript:usp.panel.hide()">close</a>');
			}
		}
	},
	failure: function(o) {
		usp.debugStatus(0);
		usp.debugOutput('<br /><b>Connection failed. <a href="javascript:tags.panel.hide()">close</a>');
	}
}


//</nowiki>