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! */ var mwApiCounter = setInterval(function {   if(typeof mw !== 'undefined' && typeof mw.Api !== 'undefined')    {        clearInterval(mwApiCounter);        /**         * Required utilities         * Array.prototype.equals: Compares two arrays, implementation from 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}); /**        * Main object */       var ImageNotification = { api: new mw.Api, config: $.extend({               refreshImage: "http://slot1.images.wikia.nocookie.net/__cb1464990246/common/skins/common/images/ajax.gif",                refreshInterval: 10000,                enableOn: ["Special:BlankPage/Images", "Special:RecentChanges"],                enableAction: ["view"],                announceError: false,                pingSrc: "http://images.wikia.com/monchbox/images/0/01/Beep-sound.ogg",            }, window.ImageNotificationConfig), ping: new Audio, files: [], init: function {               if(                    !this.config.enableOn.includes(mw.config.get("wgPageName")) ||                    !this.config.enableAction.includes(mw.config.get("wgAction"))                ) return; if(!localStorage.getItem("ImageNotificationList")) localStorage.setItem("ImageNotificationList", JSON.stringify([])); this.insertUI; this.refreshList; setInterval($.proxy(this.refreshList, this), this.config.refreshInterval); },           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 && this.config.announceError) 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 && this.config.announceError) 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($.proxy(function{ if(this.config.announceError) BannerNotification("An error occurred while fetching data about unused images", 'error').show; }, this)); }               }, this)).fail($.proxy(function{ if(this.config.announceError) new BannerNotification("Error while null editing http://community.wikia.com/wiki/User:KockaAdmiralac/Maintenance", 'error').show; }, this)); },           getUses: function(page) {               this.api.get({                    action: "parse",                    text: " imageused=" + page + " "                }).done($.proxy(function(d) {                   if(d.error && this.config.announceError) 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 {                   if(this.config.announceError) 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 = this.config.pingSrc; $("#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)); } });