User:Natercrawford/wikia.js

/* * Authors: T3CHNOCIDE (http://community.wikia.com/wiki/User:T3CHNOCIDE) * Website: Destiny Wiki (http://destiny.wikia.com) * License: CC-BY-SA 3.0 * Version: v1.0 (4 September 2016) * Function: *     Fetches item data on hover over of ns-0 article links through an onsite JSON database - see Project:HoverTool *     Displays basic item information in tool tip which follows cursor. *     To reduce load time, item database is split in alphabetical order. * * History: *     4 September 2016 - 1.0 Enters beta phase. *     28 September 2016 - 1.01 Minor fix and linted. * * To Do: *     1) Correct tool tip HTML so description is in italics and titles appear without padding. *      2) Include Ambiguous items into database as currently no tool tip will generate. *     3) Include quests, bounties & characters into database. * * Additional Details: *      This JavaScript extension requires additional CSS to function as expected. *      Below is a list of the CSS classes used. *      - hovertool *      - hovertool p *      - hovertool-exotic *      - hovertool-legendary *      - hovertool-rare *      - hovertool-uncommon *      - hovertool-common *      - hovertool-opacity-exotic *      - hovertool-opacity-legendary *      - hovertool-opacity-rare *      - hovertool-opacity-uncommon *      - hovertool-opacity-common */ //Inserts hovertool div to document body $("body").append(" "); //Sets hovertool functin on link hover $("a").mouseover(function (kmouse) { "use strict"; //Gets first character of a link & full page name var pageName = $(this).attr("href").split("/wiki/")[1]; //Exit if not an article url. if (pageName === undefined) { return; }   var dbIndex = $(this).attr("href").split("/wiki/")[1][0].toLowerCase; //Checks if page is non-article namespace through prefix. Exits if non-article. if (pageName.split(":").length > 1) { if ($.inArray(pageName.split(":")[0].toLowerCase.replace(/_/g, " "), ["media", "special", "main", "talk", "user", "user talk", "project", "project talk", "file", "file talk", "mediawiki", "mediawiki talk", "template", "template talk", "help", "help talk", "category", "category talk", "forum", "forum talk"]) > -1) { return; }   }    //If first character is not a letter, assign to db 0, otherwise get matching database var dbTitle = "Project:HoverTool/0"; if (new RegExp("[a-z]").test(dbIndex)) { dbTitle = "Project:HoverTool/" + dbIndex; }   //Download JSON database //Performs synchronous get request for page contents of database var dbFetch = JSON.parse(		$.ajax({ url: mw.util.wikiScript("api"), data: { action: "query", format: "json", prop: "revisions", titles: dbTitle, rvprop: "timestamp|content" }, async: false })		.responseText	); //Parses database from get request and converts to JSON var dbJSON = JSON.parse(dbFetch["query"]["pages"][Object.keys(dbFetch.query.pages)[0]]["revisions"][0]["*"]); //Checks if item exists in database if (dbJSON[pageName.replace(/_/g, " ")]) { //Remove title attribute to stop title tooltip $(this).removeAttr("title"); //Generates hovertool HTML mark up       var hoverToolContents = "     "; } else { //Adds stat name & stat bar to stat section magazineStat = " " + statname + " " + statvalue + "  "; }               }            });            //Adds end of stats table             hoverToolContents += "   ";        }            hoverToolContents += "   ";        }        hoverToolContents += "   ";        //Sets hover tool X and Y distance from mouse        var toolX = 15;        var toolY = 15;        //If hovertool exceeds window resets Y distance to fit in screen        if ((kmouse.clientY + $("#hovertool").height + 15) > $(window).height) {            toolY = kmouse.pageY - ((kmouse.pageY + ($(window).height - kmouse.clientY)) - $("#hovertool").height);            var bottomBarTrim = $("#WikiaBarWrapper").height;            if ($("#WikiaBarWrapper").hasClass("hidden")) {                bottomBarTrim = 0;            }            toolY = toolY + bottomBarTrim;            toolY = -toolY;        }        //If hovertool exceeds window resets X value to fit in screen if ((kmouse.pageX + $("#hovertool").width + 15) > $(window).width) { toolX = kmouse.pageX - ($(window).width - $("#hovertool").width); toolX = -toolX; }       //Adds hovertool to document and distances from mouse $("#hovertool").html(hoverToolContents).css({ left: toolX, top: toolY }).fadeIn(400); } }).mousemove(function (kmouse) { "use strict"; //Sets hover tool X and Y distance from mouse var toolX = 15; var toolY = 15; //If hovertool exceeds window resets Y distance to fit in screen if ((kmouse.clientY + $("#hovertool").height + 15) > $(window).height) { toolY = kmouse.pageY - ((kmouse.pageY + ($(window).height - kmouse.clientY)) - $("#hovertool").height); var bottomBarTrim = $("#WikiaBarWrapper").height; if ($("#WikiaBarWrapper").hasClass("hidden")) { bottomBarTrim = 0; }       toolY = toolY + bottomBarTrim; toolY = -toolY; }   //If hovertool exceeds window resets X value to fit in screen if ((kmouse.pageX + $("#hovertool").width + 15) > $(window).width) { toolX = kmouse.pageX - ($(window).width - $("#hovertool").width); toolX = -toolX; }   //Adds hovertool to document and distances from mouse $("#hovertool").css({ left: kmouse.pageX + toolX, top: kmouse.pageY + toolY }); }).mouseout(function { "use strict"; $("#hovertool").stop.fadeOut(400); });