Wowhead - Armory Import

By Emoroan Last update Oct 22, 2009 — Installed 145 times. Daily Installs: 3, 1, 2, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 2, 0, 3, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0

There are 5 previous versions of this script.

// ==UserScript==
// @name           Wowhead - Armory Import
// @namespace      http://userscripts.org/users/88914
// @description    Highlights items in Wowhead item lists based on multicharacter armory imports
// @include        http://*.wowhead.com/*
// ==/UserScript==
 
// http://www.wowhead.com/?items&filter=sl=16:18:5:8:11:10:1:23:7:21:2:22:15:4:3:25:12:17:6:9;ub=9;gm=3;gb=1;wt=123:119:103:96:24:23:94:79;wtv=1.55:0.97:0.97:0.8:0.78:0.32:0:-100

// * Fixed issue with EU armory

/// <summary>
/// WoW Characters to import
/// </summary>
var characters = [
    { name: "Character1", realm: "Realm", zone: "US", color: "#3B434F" },
    { name: "Character2", realm: "Realm", zone: "US", color: "#52415F" },
    { name: "Character3", realm: "Realm", zone: "US", color: "#AA5555" }
];

/// <summary>
/// Inject the JQuery Library into the current page so it can be used.
/// Once it is loaded run the callback so the rest of the script is run
/// </summary>
/// <param name="callback">function to call after JQuery has been loaded</param>
JQuery = function(callback) {
    // Check if jQuery's loaded
    var wait = function() {        
        if (typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(function() { wait(); }, 100); }
        else { $ = unsafeWindow.jQuery; callback(); }
    };

    if (typeof unsafeWindow.jQuery == 'undefined') {
        var script = document.createElement('script');
        script.src = 'http://code.jquery.com/jquery-latest.js';
        script.type = 'text/javascript';
        document.getElementsByTagName('head')[0].appendChild(script);
    }
    wait();
}

Color = {
    /// <summary>
    /// Take a Hex color and multiply it by a number
    /// </summary>
    Shade: function(a, b) {
        var v = Color.RGB(a);
        for (var i = 0; i < 3; i++) {
            v[i] = Math.round(v[i] * b)
            if (v[i] > 255) v[i] = 255
            if (v[i] < 0) v[i] = 0
        }
        return Color.Hex(v);
    },

    /// <summary>
    /// Convert Hex to RGB
    /// </summary>
    RGB: function(a) {
        var o = a.toLowerCase();
        if (o.charAt(0) == '#')
            o = o.substring(1);

        return [parseInt(o.slice(0, 2), 16), parseInt(o.slice(2, 4), 16), parseInt(o.slice(4), 16)]
    },

    /// <summary>
    /// Convert RGB to Hex
    /// </summary>
    Hex: function(a) { return "#" + Color._hex(a[0]) + Color._hex(a[1]) + Color._hex(a[2]) },
    _hex: function(a) { return ('0' + a.toString(16)).slice(-2) }
}



Network = {
    Request: function(url, callback) {
        GM_xmlhttpRequest({
            method: 'GET',
            url: url,
            onload: function(response) {
                var contents = '';
                if (response.status == 200) {
                    contents = response.responseText;
                    GM_setValue(url, contents);
                } else {
                    contents = GM_getValue(url);
                }
                callback(contents);
            }
        });
    }
};

/// <summary>
/// Function Library for the Wowhead
/// </summary>
Wowhead = {
    ItemList: {

        /// <summary> 
        /// Highlight the table row of any item passed in
        /// </summary>
        HighlightItem: function(item) {
            var itemLink = $("a[href='/?item=" + item.id + "']");
            var row = itemLink.parents('tr');

			
            if (row.parents().hasClass('grid') || row.parents().hasClass('icontab')) {
                itemLink.parents('th').css("background", item.color);
                itemLink.parents('td').css("background", item.color);            
            } else if (!row.parents().hasClass('tooltip')) {
                row.css("background", item.color);                            
            }
            
            //Add player names as tooltips for the rows
            if(!row.attr("title") || row.attr("title").indexOf(item.title) == -1)
				row.attr("title", row.attr("title") + " " + item.title);

            var icon = itemLink.parents("div.iconsmall")
            icon.css("border", "solid 1px " + Color.Shade(item.color, 1.5));

            var iconcontainer = icon.parent();
            iconcontainer.width(iconcontainer.width() + 2);
        },

        /// <summary> 
        /// Raise an alert when a link is clicked
        /// </summary>
        onUpdate: function(callback) {
            $("a").click(function() { callback(); });
            callback();
        }
    }
};


/// <summary>
/// Function Library for the WoWArmory
/// </summary>
WoWArmory = {

    GetArmoryDomain: function(zone) {
        if (zone.toLowerCase() == "eu")
            return "eu.wowarmory.com";
        return "www.wowarmory.com";
    },

    GetArmoryUrl: function(name, realm, zone) {
        return "http://" + WoWArmory.GetArmoryDomain(zone) + "/character-sheet.xml?r=" + realm + "&n=" + name;
    },

    Character: function(character, callback) {             
        var createCharacter = function(xml) {
			var ret = [];
            $(xml).find('item').each(function() { ret.push($(this).attr('id')); });
            
            var t = {
				color: character.color,
				class: $(xml).find('character').attr('class'),
				name: $(xml).find('character').attr('name'),
				items: ret
            };
            
            return t;
        }

        var url = WoWArmory.GetArmoryUrl(character.name, character.realm, character.zone);
        Network.Request(url, function(response) { callback(createCharacter(response)) });       
    }

};


/// <summary> 
/// This is the actual script that does the work
/// </summary>
Script = function(armory, website) {
    var _items = [];
    var _armory = armory;
    var _website = website;
    self = this;

    this.Init = function() {
        //Whenever the itemlist is updated then apply highlight
        _website.ItemList.onUpdate(self.HighlightItems);
        self.LoadCharacters();
    }


    /// <summary> 
    /// Load all defined characters
    /// <s/ummary>
    this.LoadCharacters = function() {
        for (var character = 0; character < characters.length; character++) {
            _armory.Character(characters[character], self.LoadItems);
        }
    }

    /// <summary> 
    /// Load items for a specific character
    /// </summary>
    this.LoadItems = function(character) {
        //Store a local cached version of the items
        var items = character.items;        
        for (var i = 0; i < items.length; i++) {
            _items[_items.length] = { id: items[i], color: character.color, title: character.name };
        }
        self.HighlightItems();
    }

    /// <summary> 
    /// Highlight all known items
    /// </summary>
    this.HighlightItems = function() { 
        $.each(_items, function(i, item) {
            _website.ItemList.HighlightItem(item);
        });
    }

    JQuery(self.Init);
} (WoWArmory, Wowhead);