User:Mooziq/global.js

//making Wikia FANDOM better. One script at a time. console.log("Running global.js version 1.2"); (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;        }    }    //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.trim; poptotal = poptotal + parseInt(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);    } }(jQuery, mediaWiki));