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. |
/* 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");
});