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 */ if (mw.config.get('wgCanonicalSpecialPageName') === 'WikiActivity') { window.addEventListener('unload', function {        localStorage.removeItem('WABoardNotify');    });

function WABoardNotify { if (document.hasFocus) { return; }

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

function stash (id) { localStorage.setItem('WABoardNotify', id); }

// Thanks to Larissa#4027 for a simplification function resolveThread (title) { return new mw.Api.get({               action: 'query',                list: 'recentchanges',                rcprop: 'title|ids',                rcstart: title.match(/@comment-\d+-(\d+)/)[1]            }).then(function (data) {                var page = data.query.recentchanges.find(function(revision) { return revision.title == title; }),               board = title.match(/^(?:[^:]+:)?([^/]+)/)[1] + ' board';                return ['https://community.fandom.com/wiki/Thread:' + page.pageid, board];            }); }

Notification.requestPermission.then(function (permission) {           // if we don't have permission then this will do f*** all won't it            if (permission !== 'granted') {                return;            }

var last = grab;

if (last === null) { stash(new Date.toISOString); last = grab; }

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

if (threads.length === 0) { return; }

var end = threads[0].timestamp;

if (last === null) { last = end; stash(end); }

threads.forEach(function (thread) {                   if (new Date(thread.timestamp) > new Date(last)) {                        resolveThread(thread.user, thread.title).then(function (res) { var notif = new Notification(res[1]);

notif.onclick = function { window.open(res[0]); }                       })['catch']($.noop);                    }                });

stash(end); });       });    }

window.ajaxCallAgain = window.ajaxCallAgain || []; ajaxCallAgain.push(WABoardNotify); }

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

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