User:Rutilant/statusChanger.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Rutilant/statusChanger. |
/*
<section begin=last_update/>February 7, 2019<section end=last_update/>
*/
if (s_appname === undefined) {
var s_appname = "statusChanger";
}
$(function() {
var get_token = $.get("/w/api.php?action=query&meta=tokens&format=json").then(response => {
try {
return response.query.tokens.csrftoken;
} catch (e) {
mw.notify(s_appname + ': unable to parse the token; check console log for more detail.');
console.log(e);
return false;
}
});
function change_status(new_status) {
var wgUserName = mw.config.values.wgUserName;
var edit_summary = `Status: ${new_status} ([[User:RainFall/statusChanger|${s_appname}]])`;
get_token.then(token => {
if (token) {
$(".s-btns").attr("disabled", "disabled");
$.ajax({
type: 'POST',
url: '/w/api.php',
data: {
title: 'User:' + wgUserName + '/Status',
action: 'edit',
token: token,
summary: edit_summary,
minor: 1,
recreate: 1,
text: new_status,
format: 'json'
},
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.';
},
complete: () => {
$(".s-btns").prop("disabled", false);
}
});
}
});
}
function show_options() {
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: 350px;
z-index: 1;
`;
var header = "User: " + wgUserName + "<br>Current status: <span id='s-current-status'>unknown</span>";
var available = ['online', 'away', 'around', 'busy', 'offline', 'sleeping'];
var cancelbutton = `<button id='cancel_status_change'>Cancel</button>`;
var available_options = "";
available.forEach(function(e, i) {
available_options += `<button id="s-btn-${i}" class="s-btns" data-status="${e}">${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);
$(".s-btns").click(function(elem) {
change_status(elem.currentTarget.dataset.status);
});
$("#cancel_status_change").click(function() {
hide_options();
});
/* get the current status */
$.get('/w/api.php?action=parse&prop=wikitext&format=json&page=User:' + wgUserName + '/Status')
.then(returndata => {
try {
returndata = returndata.parse.wikitext["*"];
} catch (e) {
returndata = "parse error";
console.log('Parse error.');
console.log(e);
}
$("#s-current-status").html(returndata);
});
}
function hide_options() {
$("#status_changer_overlay_11").animate({
opacity: 0,
top: 0
}, 500, function() {
$("#status_changer_overlay_11").remove();
});
}
mw.util.addPortletLink("p-cactions", "#", "Change status", "sts_change");
$("#sts_change").click(function(ev) {
ev.preventDefault();
show_options();
});
});