User:AdorableDarling/wikia.js

/* Image notification */ /** * Name:       ImageNotification * Version:    v1.0 * Author:     KockaAdmiralac <1405223@gmail.com> * Description: Shows a button with the number of images that are used but not categorized *             WARNING: Only works on Community Central, don't import this elsewhere! * Note:       This script is for personal use only! *             Importing it wiki-widely would most probably be a Terms of Use violation! */ // http://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript Array.prototype.equals = function (array) {   if (!array) return false; if (this.length != array.length) return false; for (var i = 0, l=this.length; i < l; i++) {       if (this[i] instanceof Array && array[i] instanceof Array) {           if (!this[i].equals(array[i])) return false; }       else if (this[i] != array[i]) return false; }   return true; }; Object.defineProperty(Array.prototype, "equals", {enumerable: false}); var mwApiCounter = setInterval(function {   if(typeof mw !== 'undefined' && typeof mw.Api !== 'undefined')    {        clearInterval(mwApiCounter);        var ImageNotification = {            api: new mw.Api,            ping: new Audio,            files: [],            init: function            {                if(!localStorage.getItem("ImageNotificationList")) localStorage.setItem("ImageNotificationList", JSON.stringify([]));                this.insertUI;                this.refreshList;                setInterval($.proxy(this.refreshList, this), 10000);                // Moar stuff            },            refreshList: function            {                $("#ImageNotificationButton").html('');               this.api.post({ action: "edit", title: "User:KockaAdmiralac/Maintenance", prependtext: "", token: mw.user.tokens.get('editToken') }).done($.proxy(function(data)               {                    if(data.error) new BannerNotification("Error while null editing http://community.wikia.com/wiki/User:KockaAdmiralac/Maintenance: " + data.error.code, 'error').show;                    else                    {                        this.api.get({ action: "query", prop: "links", titles: "User:KockaAdmiralac/Maintenance", pllimit: 500 }).done($.proxy(function(d)                       {                            if(d.error) new BannerNotification("An error occurred while fetching data about unused images: " + d.error.code, 'error').show;                            else                            {                                this.files = [];                                var array = d.query.pages[1021677].links.map(function(el){ return el.title; });                                this.tempCount = array.length;                                array.forEach($.proxy(this.getUses, this));                            }                        }, this)).fail(function{ BannerNotification("An error occurred while fetching data about unused images", 'error').show; });                    }                }, this)).fail(function{ new BannerNotification("Error while null editing http://community.wikia.com/wiki/User:KockaAdmiralac/Maintenance", 'error').show; }); },           getUses: function(page) {               this.api.get({                    action: "parse",                    text: " imageused=" + page + " "                }).done($.proxy(function(d) {                   if(d.error) new BannerNotification("Error while fetching backlinks for " + page + ": " + d.error.code, 'error').show; else if(d.parse.links.length > 0) this.files.push(page); if(--this.tempCount === 0) this.onFetchEnd; }, this)).fail($.proxy(function {                   new BannerNotification("Error while fetching backlinks for " + page, 'error').show; if(--this.tempCount === 0) this.onFetchEnd; }, this)); },           onFetchEnd: function {               if(this.files.equals(localStorage.getItem("ImageNotificationList"))) {                   this.ping.play; $("#ImageNotificationButton").addClass("alert"); }               $("#ImageNotificationButton").html(this.files.length); },           insertUI: function {               mw.util.addCSS("#ImageNotificationButton{width:30px;height:30px;background-color:#73AD21;border-radius:5px;border:3px solid green;transition:background 1s ease-in-out;padding:10px;text-align:center;font-weight:bold;font-size:24px}#ImageNotificationButton:hover{background-color:yellow}#ImageNotificationButton.alert{background-color:red}"); $("#WikiHeader .buttons").append(' '); this.ping.src = "http://images.wikia.com/monchbox/images/0/01/Beep-sound.ogg"; $("#ImageNotificationButton").click($.proxy(this.showModal, this)); },           showModal: function {               $.showCustomModal("Uncategorized images", " ", {                    id: "ImageNotificationModal",                    buttons: [{                        id: "ImageNotificationCloseButton",                        defaultButton: true,                        message: "Close",                        handler: function{ $("#ImageNotificationModal").closeModal; }                    }]                }); this.files.forEach(function(el){ $("#ImageNotificationModalMain").append("" + el + " "); }); localStorage.setItem("ImageNotificationList", JSON.stringify(this.files)); $("#ImageNotificationButton").removeClass("alert"); }       };        $($.proxy(ImageNotification.init, ImageNotification)); } });