Jump to content

User:Rutilant/statusChanger.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Rutilant (talk | contribs) at 09:47, 20 December 2018 (debugging the other one). 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.
	/* IN PROGRESS */
	
	
	/* The name displayed in notification and edit summary. */
	var s_appname="Status Changer";

	function change_status(new_status, ref){
		var wgUserName=mw.config.values.wgUserName;
		if(!$(ref).parent("div#status_changer_overlay_11").length){
			return "This function is not being called from the status-changing button. Cannot continue.";
		}

		if(s_appname===undefined){
			var s_appname="Status Changer";
		}

		var edit_summary=`Changed status to ${new_status} using [[User:RainFall/Status.js||${s_appname}]].`;
		$.ajax({
			url: '/w/api.php?action=query&prop=info&intoken=edit&titles=Main_page&format=json',
			success: function(data) { token = data.query.pages[Object.keys(data.query.pages)].edittoken.replace(/\+\\$/g,'%2B%5C'); },
			dataType: 'json',
			async: false,
			error:function(e){
				console.log(e);
				mw.notify(s_appname+': Unable to get the token; check console log for more detail.');
				return 'Unable to get the token.';
			}
		});
		console.log(token)
		$.ajax({
			type: 'POST',
			url: '/w/api.php?action=edit&title=User:'+wgUserName+'/Status&summary='+edit_summary+'&minor=1&recreate=1&text='+new_status+'&token='+token,
			async: false,
			success:function(e){
				hide_options();
				mw.notify(s_appname+': Updated status to '+new_status);
			},
			error:function(e){
				console.log(e);
				mw.notify(s_appname+': Unable to perform the edit; check console log for more detail.');
				return 'Unable to perform the edit.';
			}
		});

	}


	function show_options(){
		var wgUserName=mw.config.values.wgUserName;
		$.ajax({
			type: 'GET',
			url: '/w/api.php?action=parse&prop=wikitext&format=json&page=User:'+wgUserName+'/Status',
			async: false,
			dataType: 'json',
			success:function(returndata){
				console.log(returndata);
				try{
					returndata=returndata.parse.wikitext["*"];
				}catch(e){
					returndata="Parse error."
					console.log('Parse error.')
					console.log(e)
				}
				display_menu(returndata);
			},
			error:function(e){
				display_menu('Fetch error.');
				console.log('Fetch error.')
				console.log(e)
			}
		});
	}

	function display_menu(status){
		var wgUserName=mw.config.values.wgUserName;
		var overlay="";
		var style="position: fixed; top:50%; left: 50%; background-color:#f3f3f3; padding: 10px; font-family: BlinkMacSystemFont, 'Segoe UI', Constantia, Calibri; transform: translate(-50%, -50%); width: 300px; z-index: 1; ";
		var header="User: "+wgUserName+"<br>Current status: "+status;
		var available=['online','away','around'];
		var cancelbutton=`<button onclick='hide_options()'>Cancel</button>`;

		var available_options="";
		available.forEach(function(e){
			available_options+=`<button onclick="change_status('${e}', this)">${e}</button>`;
		});
		overlay+=`
		<div style="${style}" id="status_changer_overlay_11">
		${header}<br><br>
		<div style="text-align:center">
		${available_options}
		</div><br>
		${cancelbutton}
		</div>`;
		$("body").prepend(overlay);

		/* fade-in animation because I like it... lol. */
		/* This can be removed */
		$("#status_changer_overlay_11").fadeOut(0,function(){$(this).fadeIn(300)});

		/*______________________________________________*/
	}

	function hide_options(){
		/* remove this line to disable the animation =>*/$("#status_changer_overlay_11").fadeOut(300,function(){
			$("#status_changer_overlay_11").remove();
		/* remove this line to disable the animation =>*/});
	}

	$(function() {
     	mw.util.addPortletLink("p-cactions",  "javascript:show_options()", "Change status");
	});