Jump to content

User:BrandonXLF/UpdateNotifications.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by BrandonXLF (talk | contribs) at 01:01, 9 December 2019 (UpdateNotifications.js). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
/*** Update Notification Count ***/

// Updates the alert and notification counts every few seconds
// Documentation at [[User:BrandonXLF/UpdateNotifications]]
// By [[User:BrandonXLF]]

$(function updateNotificationCount () {
    $.get(mw.config.get('wgScriptPath') + '/api.php', {
        action: 'query',
        meta: 'notifications',
        notgroupbysection: 1,
        notprop: 'list|count|seenTime',
        format: 'json',
        notcrosswikisummary: mw.user.options.get('echo-cross-wiki-notifications')
    }).done(function(r){
        r = r.query.notifications;
        var newcount = +r.alert.count;
        var oldcount = +$('#pt-notifications-alert a').attr('data-counter-num');
        var unread = false, i = 0;
        for (i = 0; i < r.alert.list.length; i++) {
            if ((new Date(r.alert.seenTime)).getTime() < (new Date(r.alert.list[i].timestamp.utciso8601)).getTime()) {
                unread = true;
                break;
            }
        }
        if (unread && newcount > oldcount) mw.notify('New alert recieved!');
        $('#pt-notifications-alert a').toggleClass('mw-echo-unseen-notifications',unread).toggleClass('mw-echo-notifications-badge-all-read',newcount === 0).attr('data-counter-num',newcount).attr('data-counter-text',newcount).text('Alerts (' + newcount + ')');
        newcount = +r.message.count;
        oldcount = +$('#pt-notifications-notice a').attr('data-counter-num');
        unread = false;
        for (i = 0; i < r.message.list.length; i++) {
            if ((new Date(r.message.seenTime)).getTime() < (new Date(r.message.list[i].timestamp.utciso8601)).getTime()) {
                unread = true;
                break;
            }
        }
        if (unread && newcount > oldcount) mw.notify('New notice recieved!');
        $('#pt-notifications-notice a').toggleClass('mw-echo-unseen-notifications',unread).toggleClass('mw-echo-notifications-badge-all-read',newcount === 0).attr('data-counter-num',newcount).attr('data-counter-text',newcount).text('Alerts (' + newcount + ')');
    });
    setTimeout(updateNotificationCount,5000);
});