Last.fm - Country Flag

By kepp Last update Oct 21, 2007 — Installed 2,927 times.
// ==UserScript==
// @name          Last.fm - Country Flag
// @namespace     http://last.fm.country.flag/kepp
// @description   Adds a user's country's flag next to the country name
// @include       http://www.last.fm/user/*
// @include       http://www.last.fm/place/*
// ==/UserScript==

/*

3/28/2007 - Made the remote fetching of flag urls more accepting
10/20/2007 - Fix for Croatia as "hr"

*/

var countryCodes = {
    "Argentina":          "ar",
    "Austria":            "at",
    "Belgium":            "be",
    "Botswana":           "bw",
    "Brazil":             "br",
    "Bulgaria":           "bg",
    "Canada":             "ca",
    "Croatia":            "hr",
    "Czech Republic":     "cz",
    "Denmark":            "dk",
    "Finland":            "fi",
    "France":             "fr",
    "Germany":            "de",
    "Hungary":            "hu",
    "India":              "in",
    "Ireland":            "ie",
    "Israel":             "il",
    "Italy":              "it",
    "Japan":              "jp",
    "Latvia":             "lv",
    "Malta":              "mt",
    "Mexico":             "mx",
    "Netherlands":        "nl",
    "New Zealand":        "nz",
    "Norway":             "no",
    "Poland":             "pl",
    "Portugal":           "pt",
    "Russian Federation": "ru",
    "Serbia":             "rs",
    "Singapore":          "sg",
    "Spain":              "es",
    "Sweden":             "se",
    "Switzerland":        "ch",
    "Thailand":           "th",
    "Turkey":             "tr",
    "Ukraine":            "ua",
    "United Kingdom":     "gb",
    "United States":      "us"
};

var setItem, getItem, removeItem;
try {
    var gs = globalStorage.namedItem("last.fm.country.flag." + document.domain);
    setItem = function(key, val) {
            gs.setItem(key, val);
        };
    getItem = function(key, def) {
            var item = gs.getItem(key);
            return (item) ? item.value : def;
        };
    removeItem = function(key) {
            gs.removeItem(key);
        };
} catch (e) {
    if (!GM_getValue("prompt", false)) {
        alert("Last.fm - Country Flag: Please install an updated version of Firefox");
        GM_setValue("prompt", true);
    }
}

function $xf(query, context) {
    return document.evaluate(query, (context || document), null,
           XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}

function createFlag(href, src) {
    var a = document.createElement("a");
    a.href = href;
    a.style.background = "none";

    var img = document.createElement("img");  
    img.src = src;
    img.style.maxWidth = "25px";
    img.style.maxHeight = "25px";
    img.style.position = "relative";
    img.style.margin = "0 3px";
    img.style.top = "2px";
    img.style.border = "1px solid rgb(220,220,220)";

    a.appendChild(img);

    return a;
}

function getCountryCode(country, callback) {
    if (countryCodes[country]) {
        callback(countryCodes[country]);
    } else {
        var cc = { value: "", setVal: function(val) { this.value = val; } };
        cc.watch("value", function(id, oldVal, newVal) {
            setItem("extra-flags", getItem("extra-flags", "") + country + "\t" + newVal + "\n");
            callback(newVal);
        });
        GM_xmlhttpRequest({
            method: "GET",
            url: "http://www.last.fm/place/" + encodeURIComponent(country),
            onload: function(details) {
                cc.setVal(details.responseText.match(/depth\/flags\/(?:\d+)\/([^.]+)/)[1]);
            }
        });
    }
}

(function() {

    if (/\/user\//.test(location.href)) {
        var country = $xf("//a[@class='country-name']");
        if (!country) {
            return;
        }

        var extraFlags = getItem("extra-flags", "").split("\n");
        for each (var extra in extraFlags) {
            var parts = extra.split("\t");
            countryCodes[parts[0]] = parts[1];
        }

        getCountryCode(country.textContent, function(cc) {
            var src = "http://static.last.fm/depth/flags/150/" +
                      cc + ".gif";
            var flag = createFlag(country.href, src);
            country.parentNode.insertBefore(flag, country.nextSibling);
        });
    } else {
        var country = decodeURIComponent(location.href.match(/[^\/]+$/));
        if (!countryCodes[country]) {
            var flag = $xf("//img[starts-with(@src, 'http://static.last.fm/depth/flags/150/')]");
            var cc = flag.src.match(/([^\/]+)\.gif/)[1];
            setItem("extra-flags", getItem("extra-flags", "") + country + "\t" + cc + "\n");
        }
    }

})();