Jump to content

User:CanonNi/Scripts/StatusSetter.js

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by CanonNi (talk | contribs) at 09:17, 18 June 2024 (Undid revision 1229711299 by CanonNi (talk) ...and of course that didnt work). 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.
// Fork of [[User:Enterprisey/StatusChanger.js]] and a rip-off of [[User:SD0001/BDCS.js]]
// Work in progress, report bugs to [[User talk:CanonNi]]

$.when(
    $.ready,
    mw.loader.using( "mediawiki.api" )
).then(function () {
	
if (!mw.config.get('wgUserName')) {
	return;
}
    
/* var Set = {};
* window.Set = Set;
*
* Set.advert = ' ([[User:CanonNi/StatusSetter|StatusSetter]])' */

// check for config options, and set them to default values if undefined

if (typeof(statusSetterConfig) == 'undefined') { // create variable to store configuration
	statusSetterConfig = {};
}

if (typeof(statusSetterConfig.statusList) == 'undefined') {
    statusSetterConfig.statusList = [ // all available statuses
		"online",
    	"offline",
    	"sleeping",
    	"busy",
    ];
}

if (typeof(statusSetterConfig.statusPage) == 'undefined') {
    statusSetterConfig.statusPage = 'User:' + mw.config.get('wgUserName') + '/Status';
}

function makeListener(newStatus) {
    return function ( evt ) {
        evt.preventDefault();
        var api = new mw.Api({
            ajax: { headers: { 'Api-User-Agent': '[[w:User:CanonNi/StatusSetter.js]]' } }
        });

        api.postWithEditToken({
            action: 'edit',
            title: statusSetterConfig.statusPage,
            text: newStatus,
            summary: "Set status to " + newStatus + " using [[w:User:CanonNi/StatusSetter.js|StatusSetter]]"
        }).then(function(){
            api.post( { action: "purge", titles: 'User:' + mw.config.get('wgUserName') } );
            mw.notify('Done setting status!');
        });
        return false;
    };
}

// Add the links
for (var i=0; i<statusSetterConfig.statusList.length; i++) {
    var stat = statusSetterConfig.statusList[i];
    var message = link = stat;
    mw.util.addPortletLink(
        "p-tb",
        "#",
        stat.charAt(0).toUpperCase() + stat.slice(1), // link text, capitalized
        "pt-status-" + stat, // id of new button
        "I'm " + message + "!", // hover text
        "", // access key - no need to define one
        document.getElementById("pt-logout")) //add before logout button
    .addEventListener('click',makeListener(stat));
};

});