User:Robyn Grayson/wikia.js

/* User files/Gallery files button */ $(function {   if (wgNamespaceNumber == 6 && !$('.categories').hasClass('ui-sortable')) {        $('#WikiaArticle').before( 'Gallery files ' );       $('#gallery-files').click(function { $('#CategorySelectAdd').click; setTimeout(function {               $('#CategorySelectInput').val('Gallery files').trigger(jQuery.Event('keydown', {                    which: 13                }));                setTimeout(function { $('#CategorySelectSave').click; }, 500);           }, 500);        });    } }); $(function {    if (wgNamespaceNumber == 6 && !$('.categories').hasClass('ui-sortable')) {        $('#WikiaArticle').before( 'User files ' );       $('#user-files').click(function { $('#CategorySelectAdd').click; setTimeout(function {               $('#CategorySelectInput').val('User files').trigger(jQuery.Event('keydown', {                    which: 13                }));                setTimeout(function { $('#CategorySelectSave').click; }, 500);           }, 500);        });    } });

$(function {   var timer;//ensure all events accumulated before processing    var ids = [];//array of cityid    var cache = {};//cache of city.id=city    var debug = $.getUrlVar('debug');    function log  {        log.a = [].slice.call(arguments);        log.a.unshift((new Date).getTime);        log.a.unshift('noti-obs');        if (debug) console.log.apply(this, log.a);    }//log    function mo_callback (records) {        //clearTimeout(timer);        log('moc', records);        //process;        var recs = [];        recs = records.filter(function(el) { var isTrue = false; if (!el.addedNodes.length) return isTrue; el.addedNodes.forEach(function(v) {               isTrue = $(v).hasClass('.notifications-for-wiki') || $(v).find('.notifications-for-wiki').length ? true : isTrue;            }); return isTrue; });       if (!recs.length) return;        timer = setTimeout(process.bind(this, false, recs), 500);    }//mo_callback    function stobserver (state) {        //start/stop observer        var $target = $('#globalNavigation');//$('#notifications #notificationsContainer > ul');        log('stobs', state, $target.length);        if (state) {            if (!$target.length) {                restoreObserver;                return;            }            observer.observe($target.get(0), {childList: true, subtree: true});        } else {            //process lost? records            var recs = observer.takeRecords;            observer.disconnect;            log('stobs.recs', recs);            if (recs.length) mo_callback(recs);        }//if state    }//stobserver    function get_data  {        log('gd.ids', ids);        if (!ids || !ids.length) return;        $.ajax({ url: '//www.wikia.com/api/v1/Wikis/Details', type: 'post', data: { ids: ids.join(',') },           success: function data_callback (data) { log('dc.data', data); if (!data || data.error || !data.items) return; var $target = $('#notifications #notificationsContainer > ul li.notifications-for-wiki'); stobserver(false); $target.each(function {                   var $this = $(this);                    var city = data.items[$this.data('wikiId')];                    log('dc', city, this);                    if (!city) return;                    anode($this, city);                    cache[city.id] = city;                });//each target stobserver(true); }//success });//post       ids = [];//clear ids    }//get_data    function anode (node, city) {        log('anode', node, city);        var $header = $(node).find('header:first');        //log('anode header', $header);        if (!$header.length) return;//something weird        //text_node = 3. otherwise node already processed        if ($header.get(0).childNodes[0].nodeType !== 3) {            log('anode nodetype', $header.get(0).childNodes[0].nodeType);            return;        }        var $a = $('', { href: city.url, target: '_blank' }).text(($header.get(0).childNodes[0].nodeValue || '').trim);       //remove text node and add         $header.get(0).childNodes[0].remove;        $header.prepend($a);    }//anode    function process (obs, recs) {        log('proc', obs, recs);        if (!recs || !recs.length) return;        if (!obs) stobserver(false);        $.each(recs, function { log('p.t1', this); $.each($(this.addedNodes).find('.notifications-for-wiki'), function {               var $this = $(this);                if (cache[$this.data('wikiId')]) {                    anode($this, cache[$this.data('wikiId')]);                    return;                }//if cached                ids.push($this.data('wikiId'));            });//each node });//each rec       if (!obs) stobserver(true);        if (!ids.length) return;        get_data;    }//process    function restoreObserver  {        //restore observer after target loss (overwrite?)        var ttimer = setInterval(function { var $target = $('#globalNavigation'); if (!$target.length) return; clearInterval(ttimer); log('target', $target.get(0)); stobserver(true); }, 100);   }//restoreobserver    log('start');    var observer = new MutationObserver(mo_callback);    restoreObserver; });