User:TheTechie/tut.js

From Wikipedia, the free encyclopedia
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.
//<nowiki>
$(function(){
	version = [0, 0, 1]; // to get version number, join numbers with periods. For example, "0, 0, 1" is version 0.0.1
	mw.loader.using(['mediawiki.util', "mediawiki.api"], function () {
		class textBlocks {
			constructor(){
				this.smile = `
<br>
==You've been smiled upon!==
{{subst:smile}} ~~~~
				`;
				this.teahouse = `
<br>
==You're invited!==
{{subst:Teahouse invitation}} ~~~~
				`;
				this.warning1 = `[[File:Information.svg|25px|alt=Information icon]] %warning% ~~~~`;
				this.warning2 = `[[File:Information orange.svg|25px|alt=Information icon]] %warning% ~~~~`;
				this.warning3 = `[[File:Nuvola apps important.svg|25px|alt=Warning icon]] %warning% ~~~~`;
				this.warning4 = "[[[[File:Stop hand nuvola.svg|30px|alt=Stop icon]] %warning% ~~~~";
			}
		}
		
		class editSummaries {
			constructor(){
				this.smile = "Sending this user a smile using [[User:TheTechie/tut|TheTechie's User Tools]]";
				this.archive = "Archiving this page using [[User:TheTechie/tut|TheTechie's User Tools]]";
				this.custom = "Using a custom warning created using [[User:TheTechie/tut|TheTechie's User Tools]]";
				this.teahouse = "Inviting this user to the Teahouse using [[User:TheTechie/tut|TheTechie's User Tools]]";
			}
		}
		
		blocks = new textBlocks();
		
		es = new editSummaries();
		
		function wait(secs) {
			let msecs = secs * 1000;
			setTimeout(function () {
        		location.reload();
    		}, msecs);
		}
		
		function replacePage( info ) {
			var api = new mw.Api();
			api.postWithToken("csrf", {
				action: 'edit',
				title: info.title,
				text: info.text, // will replace entire page content
				summary: info.summary
			} ).done(function( data ) {
				alert("Done! This page will reload in a few seconds.");
			} ).fail( function(code, data) {
				alert(api.getErrorMessage(data).text());
			} );
		}
		
		function appendToPage( info ) {
			var api = new mw.Api();
			api.postWithToken("csrf", {
				action: 'edit',
				title: info.title,
				appendtext: info.text, // will not replace entire page content
				summary: info.summary
			} ).done(function( data ) {
				alert("Done! This page will reload in a few seconds.");
			} ).fail( function(code, data) {
				alert(api.getErrorMessage(data).text());
			} );
		}
		
		function smileAtUser(){
			if ( mw.config.get( 'wgCanonicalNamespace' ) === 'User_talk') {
				appendToPage({
					title: mw.config.get('wgPageName'),
					text: blocks.smile,
					summary: es.smile
				});
				wait(3);
				
			}
			
			else {
				alert("TUT: This option can only be used on user talk pages.");
			}
		}
		
		function inviteToTeahouse(){
			if ( mw.config.get( 'wgCanonicalNamespace' ) === 'User_talk') {
				appendToPage({
					title: mw.config.get('wgPageName'),
					text: blocks.teahouse,
					summary: es.teahouse
				});
				wait(3);
			}
			
			else {
				alert("TUT: This option can only be used on user talk pages.");
			}
		}
		
		mw.util.addPortlet('p-ttut', 'TUT', '#p-cactions');
		smile_button = mw.util.addPortletLink(portletId='p-ttut', href='#', text='Smile', tooltip="Send this user a smile");
		$(smile_button).on("click", function(e) {
			if (confirm("Publicly send a smile to this user?")) {
				smileAtUser();
			}
			e.preventDefault;
		});
		teahouse = mw.util.addPortletLink(portletId='p-ttut', href='#', text='TH', tooltip="Invite this user to the Teahouse");
		$(teahouse).on("click", function(e) {
			if (confirm("Publicly send a Teahouse invitation to this user?")) {
				inviteToTeahouse();
			}
			e.preventDefault;
		});
	});
});
//</nowiki>