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 ***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); }

function resolveThread (user, commentString) { return new Promise(function (res, rej) {               $.get('https://community.fandom.com/wiki/Special:Contributions/' + user).then(function (page) { var $contribs = $('#mw-content-text ul li', $(page));

$contribs.each(function (i, $contrib) {                       try {                            var $links = $('a', $($contrib));                            var comment = $links[0].href.split('?')[0].split('/wiki/')[1].replace(/_/g, ' ');                            var offset = 0;

if ($links.length === 5) { offset = 1; }

if (comment === commentString) { res([$links[offset + 2].href.split('?')[0], $links[offset + 3].innerText]); }

} catch (e) { $.noop; }                   }.bind(this));

// Something is wrong!! rej('oof'); });           });        }

Notification.requestPermission.then(function (permission) {           // if we don't have permission then this will do **** 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',    ] });