Jump to content

User:CanonNi/Scripts/StatusSetter.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.
$(() => {
    const statusList = [
        'somewhere',
        'in',
        'editing',
        'online',
        'offline',
        'busy',
        'sleeping',
        'wikibreak',
        'away',
        'vandal',
        'holiday',
        'school',
        'working',
        'eating',
        'huggle',
        'twinkle',
    ];

    function makeListener(newStat) {
        return (event) => {
            event.preventDefault();
            new mw.Api()
                .edit(`User:${mw.config.get('wgUserName')}/Status`, () => {
                    return {
                        text: newStat,
                        summary: `Set status to ${newStat}) ([[User:CanonNi/Scripts/StatusSetter|SS]]`,
                    };
                })
                .then(() => {
                    mw.notify('Done setting status!');
                    location.reload();
                });
        };
    }

    if (mw.config.get('wgPageName').includes(mw.config.get('wgUserName'))) {
        mw.util.addPortlet('p-status', 'Status', '#p-cactions');
        statusList.forEach((stat) => {
            mw.util
                .addPortletLink(
                    'p-status',
                    '#',
                    stat.charAt(0).toUpperCase() + stat.slice(1),
                    `status-${stat}`,
                    `Set status to ${stat}`
                )
                .addEventListener('click', makeListener(stat));
        });
    }
});