User:Hypirae/wikia.js

/** * Wikia.js - Community Central specific scripts */

/** * WABoardNotify - Creates notifications to new board replies for CC * * WARNING: This is a plugin for AjaxRC and requires this to be installed first. * * Note: I tried this with DOM querying but the WikiActivity doesn't have * incremental item numbering and I cannot be a**ed to sit here and implement * it myself so now I have choses a more robust method that uses API calls. * * Written by Hypirae * * Version 2 electric boogaloo */ window.ajaxCallAgain = window.ajaxCallAgain || [];

var AjaxRCBoardNotify = function { this.notificationAllowed = this.getNotificationPermission;

return this.init; }

AjaxRCBoardNotify.stash = function (value) { localStorage.setItem('AjaxRCBoardNotify', value); };

AjaxRCBoardNotify.grab = function { return localStorage.getItem('AjaxRCBoardNotify'); };

AjaxRCBoardNotify.wipe = function { localStorage.removeItem('AjaxRCBoardNotify'); };

AjaxRCBoardNotify.prototype.getNotificationPermission = function { return Notification.permission === 'granted' ? true : false; };

AjaxRCBoardNotify.prototype.setNotificationPermission = function (allowed) { this.notificationAllowed = allowed; };

AjaxRCBoardNotify.prototype.requestNotificationPermission = function { Notification.requestPermission.then(function (permission) {       if (permission === 'granted') {            this.setNotificationPermission(true);            return true;        } else {            this.setNotificationPermission(false);            return false;        }    }.bind(this)); };

AjaxRCBoardNotify.createNotification = function (threadId, boardTitle) { var notification = new Notification('Thread update!', {       body: 'New post on the "' + boardTitle + '" board.'    });

notification.onclick = function { window.open('https://community.fandom.com/wiki/Thread:' + threadId); }; };

AjaxRCBoardNotify.prototype.init = function { if (!this.notificationAllowed) { return this.requestNotificationPermission.then(function (allowed) {           if (!allowed) {                return Promise.reject('Why use a notification script if you won\'t allow notifications?');            }        }); }

if (mw.config.get('wgCanonicalSpecialPageName') !== 'WikiActivity') { return Promise.reject('Needs to be run on Special:WikiActivity'); }

window.onunload = function { AjaxRCBoardNotify.wipe; };

return Promise.resolve(this); };

AjaxRCBoardNotify.resolveThread = function (title) { var parts = title.split('/'), parent = parts.slice(0, -1).join('/');

return new mw.Api.get({       action: 'query',        prop: 'pageprops',        titles: parent    }).then(function (res) {        var id = Object.keys(res.query.pages)[0],            board = res.query.pages[id].title.split(':')[1].split('/')[0];

return [id, board]; }); };

AjaxRCBoardNotify.prototype.fire = function { if (document.hasFocus) { return; }

// grab last timestamp var last = AjaxRCBoardNotify.grab;

// if there is no last timestamp create one and stash it   if (last === null) { last = new Date.toISOString; AjaxRCBoardNotify.stash(last); }

new mw.Api.get({       action: 'query',        list: 'recentchanges',        rcnamespace: 2001,        rcprop: 'title|ids|timestamp',        rcend: last    }).then(function (res) {        var threads = res.query.recentchanges,            end;

// if there are no results then bail if (!threads.length) { return; }

// grab the last sequential entry (newest to oldest) end = threads[0].timestamp;

// iterate through entries to find newest threads threads.forEach(function (thread) {           var last = AjaxRCBoardNotify.grab;

// compare timestamps if (new Date(thread.timestamp) > new Date(last)) {

// find parent thread id and board AjaxRCBoardNotify.resolveThread(thread.title).then(function (res) {                   AjaxRCBoardNotify.createNotification(res[0], res[1]);                }); }       });

// stash the last timestamp AjaxRCBoardNotify.stash(end); }); };

new AjaxRCBoardNotify.then(function (plugin) {   window.ajaxCallAgain.push(plugin.fire); });

window.ajaxSpecialPages = ["RecentChanges", "WikiActivity"];

importArticles({   type: 'script',    articles: [        'u:dev:MediaWiki:AjaxRC/code.js',    ] });