Jump to content

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(){
	var api = new mw.Api();
	var version = [0, 1, 0]; // to get version number, join numbers with periods. For example, "0, 0, 1" is version 0.0.1
	var whatsnew = `Thanks for using TheTechie's User Tools.
To see the changelog for this version (v${version[0]}.${version[1]}.${version[2]}), please go to:
https://en.wikipedia.org/wiki/User:TheTechie/UserTools/changelog
`;
	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.warninggeneric = `<br><!-- the above warning was generated by TTUT and is completely the responsibility of the user above -->
''This warning was generated by [[User:TheTechie/UserTools|TheTechie's UserTools]], and is not an official warning, but rather was crafted by the user listed above. They assume full responsibility for this warning.''<br>`;
				this.warning1 = `<br>[[File:Information.svg|25px|alt=Information icon]] %WARNING% ~~~~`;
				this.warning2 = `<br>[[File:Information orange.svg|25px|alt=Information icon]] %WARNING% ~~~~`;
				this.warning3 = `<br>[[File:Nuvola apps important.svg|25px|alt=Warning icon]] %WARNING% ~~~~`;
				this.warning4 = `<br>[[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]]";
			}
		}

		var colorsList = {
		    white: "#ffffff",
		    black: "#000000",
		    orangeDark: "#3d3114",
		    greenDark: "#1c3d14",
		    tealDark: "#14393d",
		    blueDark: "#16143d",
		    purpleDark: "#2b143d",
		    pinkDark: "#3d142f",
		    orangeLight: "#eda455",
		    greenLight: "#55ed6c",
		    tealLight: "#55edb3",
		    blueLight: "#5562ed",
		    purpleLight: "#a655ed",
		    pinkLight: "#ed55de"
		};

		class TTUI {
			constructor(){
				this.heading = "Foobar";
				this.msg = "Foo bar foobar?";
				this.buttonCount = 1;
				this.buttonLabels = ["Yes"];
				this.cancelButtonText = "Cancel";
			}

			initialize(){
				this.self = document.createElement("div");
				this.self.style.display = "none";
				this.self.id = "ttui";
				$("#bodyContent").prepend(this.self);
				this.self.style.borderStyle = "solid";
				this.self.style.borderStyle = "";
				this.self.style.fontFamily = "sans-serif";
				this.selfHeader = document.createElement("h2");
				this.selfHeader.style.fontWeight = "bold";
				this.selfHeader.id = "ttui-header";
				this.selfHeader.innerHTML = this.heading;
				this.selfHeader.style.fontFamily = "sans-serif";
				this.self.appendChild(this.selfHeader);
				this.selfMessage = document.createElement("p");
				this.selfMessage.id = "ttui-message";
				this.selfMessage.innerHTML = this.msg;
				this.self.appendChild(this.selfMessage);
				this.cancelButton = document.createElement("button");
				this.cancelButton.innerHTML = this.cancelButtonText;
				this.cancelButton.onclick = "document.getElementById('ttui');";
				
			}
		}
		
		blocks = new textBlocks();
		
		es = new editSummaries();

		var colors = colorsList;

		ui = new TTUI();
		ui.initialize();
		
		function wait(secs) {
			let msecs = secs * 1000;
			setTimeout(function () {
        		location.reload();
    		}, msecs);
		}
		
		function replacePage( info ) {
			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 ) {
			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') {
				if (confirm("Publicly send a smile to this user?")) {
					appendToPage({
						title: mw.config.get('wgPageName'),
						text: blocks.smile,
						summary: es.smile
					});
					wait(3);
				}
			}
			
			else {
				alert("TTUT: This option can only be used on user talk pages.");
			}
		}
		
		function changeLog(){
			alert(whatsnew);
		}
		
		function inviteToTeahouse(){
			if ( mw.config.get( 'wgCanonicalNamespace' ) === 'User_talk') {
				if (confirm("Publicly send a Teahouse invitation to this user?")) {
					appendToPage({
						title: mw.config.get('wgPageName'),
						text: blocks.teahouse,
						summary: es.teahouse
					});
				}
				wait(3);
			}
			
			else {
				alert("TTUT: This option can only be used on user talk pages.");
			}
		}
		
		function userEditCount(){
			let username = mw.config.get("wgPageName");
			let type;
			username = username.split(":")[0];
			username = username.split("/")[0];
			let contribPage = `Special:Contributions/${username}`;
		}
		
		function customWarning(){
			warning = window.prompt("Please enter the message of the custom warning:");
			console.log("Warning contents: " + warning);
			let levels = ["1", "2", "3", "4"];
			let level_msg = `Enter the level of the warning:
1: Good faith assumed
2: No faith assumed
3: Bad faith assumed, warning
4: Bad faith assumed, final warning`;
			let level = window.prompt(level_msg);
			while (!levels.includes(level)){
				alert("TTUT: Invalid level specified");
				level = window.prompt(level_msg);
			}
			let block;
			if (level == "1"){
				block = blocks.warning1;
			} else if (level == "2"){
				block = blocks.warning2;
			} else if (level == "3"){
				block = blocks.warning3;
			} else if (level == "4"){
				block = blocks.warning4;
			}
			
			block = block.replace("%WARNING%", warning);
			
			block = block + blocks.warninggeneric;
			console.log("Wikitext blurb for warning: " + block);
			if (confirm("Send this custom warning? YOU ARE RESPONSIBLE FOR FOLLOWING ALL RULES USING THIS FUNCTION.")){
				appendToPage({
					title: mw.config.get("wgPageName"),	
					text: block,
					summary: es.custom
				});
				wait(3);
			}
		}
		
		mw.util.addPortlet('p-ttut', 'TTUT', '#p-cactions');
		if ( mw.config.get( 'wgCanonicalNamespace' ) === 'User_talk') {
			smile_button = mw.util.addPortletLink(portletId='p-ttut', href='#', text='Send a smile', tooltip="Send this user a smile");
			$(smile_button).on("click", function(e) {
				smileAtUser();
				e.preventDefault();
			});
			teahouse = mw.util.addPortletLink(portletId='p-ttut', href='#', text='Invite to Teahouse', tooltip="Invite this user to the Teahouse");
			$(teahouse).on("click", function(e) {
				inviteToTeahouse();
				e.preventDefault();
			});
			custom = mw.util.addPortletLink(portletId='p-ttut', href="#", text="Custom warning", tooltip="Deploy a custom warning");
			$(custom).on("click", function(e) {
				customWarning();
				e.preventDefault();
			});
		} else if (mw.config.get("wgCanonicalNamespace") === ""){}
		whatsnew_button = mw.util.addPortletLink(portletId="p-ttut", href="#", text="What's new?", tooltip="See what's new!");
		$(whatsnew_button).on("click", function(e) {
			changeLog();
			e.preventDefault();
		});
	});
});
//</nowiki>