User:Mooziq/global.js

//making Wikia FANDOM better. One script at a time. console.log("Running global.js version 1.31"); (function ($, mw) {   if (mw.config.get("wgCanonicalNamespace") == "Thread") {        //put thread manipulations here        var threadname = mw.config.get("wgPageName");        if (!threadname) threadname = "NotFound";        addThreadNames;    } else if (mw.config.get("wgCanonicalNamespace") == "Special") {        //put special page manipulations here        if (mw.config.get("wgPageName") == "Special:Insights/popularpages") {            listPopularPages;        } else if (mw.config.get("wgPageName") == "Special:Analytics") {            listAnalytics;        }    }    //do other stuff later    //add thread names     function addThreadNames {        //cancel if we can't find thread messages        var tmsgs = $(".speech-bubble-message > .MiniEditorWrapper");        if (!tmsgs || tmsgs.length < 1) {            console.log("global.js - Could not find thread messages!");            return; }       //do stuff for each message tmsgs.each(function {           var msg = $(this);            //get message name (href link)            var tnlink = msg.find(".timestamp > a.permalink").attr("href");            var tmname = "NotFound";            if (tnlink) {                tmname = tnlink.substr(tnlink.lastIndexOf('/')+1);            }            //build thread name element            var tnel = $.parseHTML(" " + tmname + " ");            //get message author element or continue to next message            var tmauth = msg.find(".edited-by");            if (tmauth.length < 1) return true;            //add thread name            tmauth.append(tnel);        }); }   function listPopularPages { //cancel if we cannot find insights table var popic = $(".insights-content"); if (!popic || popic.length != 1) { console.log("global.js - Could not find insights content table!"); return; }       //build popular pages list text area var poptah = $.parseHTML("Building Popular Pages List... "); popic.prepend(poptah); //cancel if we cannot find table rows var popili = popic.find(".insights-list-item"); if (!popili || popili.length < 1) { console.log("global.js - Could not find insights content table rows!"); return; }       //record each row var poplist = ""; var poptotal = 0; popili.each(function {           var pli = $(this);            //get page title            var plti = pli.find(".insights-list-item-title").attr("title");            //get page views            var plvi = pli.find(".insights-list-item-pageviews").text;            plvi = parseInt(plvi.trim.replace(/[^0-9]/g,''));            poptotal = poptotal + plvi;            poplist = poplist + "\n" + plti + "," + plvi;        }); //cancel if we cannot find popular pages text area var popta = $("textarea.jqpoplist"); if (!popta || popta.length != 1) { console.log("global.js - Could not find popular pages text area!"); return; }       popta.text("Total," + poptotal + poplist); }   function listAnalytics { //cancel if we cannot find analytics wrapper var anwrap = $("#analytics_wrapper"); if (!anwrap || anwrap.length != 1) { console.log("global.js - Could not find analytics wrapper!"); return; }       //build analytics list text area var antah = $.parseHTML("Building Analytics List... "); anwrap.prepend(antah); //cancel if we cannot find analytics data if (!sectionsData || !sectionsData.edits_per_day || !sectionsData.number_of_pageviews           || !sectionsData.number_of_pageviews.per_day) { console.log("global.js - Could not find required analytics data!"); return; }       //format rows of data var anlist = "YYYY-MM-DD,views,edits"; var anviews = sectionsData.number_of_pageviews.per_day; var anedits = sectionsData.edits_per_day; for (var anday in anviews) { //get views and edits var anview = anviews[anday]; var anedit = anedits[anday]; if (!anedit) anedit = ""; //format date var anymd = new Date(anday*1000).toISOString.substr(0,10); anlist = anlist + "\n" + anymd + "," + anview + "," + anedit; }       //cancel if we cannot find analytics list text area var anta = $("textarea.jqanlist"); if (!anta || anta.length != 1) { console.log("global.js - Could not find analytics list text area!"); return; }       anta.text(anlist); } }(jQuery, mediaWiki));