jdswatsup / Neopets Easy Search Hack

// ==UserScript==
// @name       Neopets Easy Search Hack
// @homepage   http://www.shopneopia.com
// @match      http://www.neopets.com/halloween/witchtower*.phtml
// @match      http://www.neopets.com/island/kitchen*.phtml
// @match      http://www.neopets.com/medieval/earthfaerie.phtml*
// @match      http://www.neopets.com/faerieland/darkfaerie.phtml*
// @match      http://www.neopets.com/safetydeposit.phtml*
// @match      http://www.neopets.com/market.phtml?*type=your*
// @match      http://www.neopets.com/market_your.phtml*
// @match      http://www.neopets.com/space/coincidence.phtml
// @match      http://www.neopets.com/island/*training.phtml?*type=status*
// @match      http://www.neopets.com/pirates/academy.phtml?type=status
// @match      http://www.neopets.com/inventory.phtml*
// @match      http://www.neopets.com/halloween/esophagor*.phtml
// @match      http://www.neopets.com/faerieland/employ/employment.phtml?*job_id=*
// @match      http://www.neopets.com/closet.phtml*
// @match      http://www.neopets.com/auctions.phtml?*auction_id=*
// @match      http://www.neopets.com/winter/snowfaerie*.phtml
// @match      http://www.neopets.com/quests.phtml
// @match      http://www.neopets.com/games/kadoatery/index.phtml
// @match      http://www.neopets.com/games/kadoatery/*
// @match      http://www.neopets.com/process_cash_object.phtml
// @match      http://www.neopets.com/hospital.phtml
// @match      http://www.neopets.com/objects.phtml?type=shop*
// @match      http://www.neopets.com/market.phtml?type=wizard&string=*
// edited by |2eap to include extra link for Shopneopia.com and other modifications to design/ functionality. All credits diceroll123 for the script.
// ==/UserScript==

imgsize = 17; // for the search images

$("<style type='text/css'>.searchimg { cursor: pointer; height: " + imgsize + "px !important; width: " + imgsize + "px !important;</style>").appendTo("head");

jQuery.fn.exists = function(){return this.length>0;}

var linkmap = { // for urls and images for each search type
    ssw: {
        "img": "http://images.neopets.com/shopkeepers/super_shopwizard.gif"
    },
    sw: {
        "url": "http://www.neopets.com/market.phtml?type=wizard&string=%s",
        "img": "http://images.neopets.com/shopkeepers/shopwizard.gif"
    },
    tp: {
        "url": "http://www.neopets.com/island/tradingpost.phtml?type=browse&criteria=item_exact&search_string=%s",
        "img": "http://images.neopets.com/new_shopkeepers/29.gif"
    },
    au: {
        "url": "http://www.neopets.com/genie.phtml?type=process_genie&criteria=exact&auctiongenie=%s",
        "img": "http://images.neopets.com/shopkeepers/auctiongenie.gif"
    },
    sn: {
        
        "url": "http://www.shopneopia.com/apps/search-results#stq=%s",
        "img": "http://i61.tinypic.com/fejpg5.png"
    },
    sdb: {
        "url": "http://www.neopets.com/safetydeposit.phtml?obj_name=%s&category=0",
        "img": "http://images.neopets.com/images/emptydepositbox.gif"
    },
    closet: {
        "url": "http://www.neopets.com/closet.phtml?obj_name=%s",
        "img": "http://images.neopets.com/items/ffu_illusen_armoire.gif"
    },
    jni: {
        "url": "http://items.jellyneo.net/index.php?go=show_items&name=%s&name_type=exact&desc=&cat=0&specialcat=0&status=0&rarity=0&sortby=name&numitems=10",
        "img": "http://images.neopets.com/items/toy_plushie_negg_fish.gif"
    },
    dti: { // had to base64 the image that I have saved since the item now has the hanger on it -.-
        "img": "data:image/gif;base64,R0lGODlhUABQAPcAAIuBA9CnAP/tAOfTCJeUhVFCAMzLyrmjN6KCAJePcIqFctyxAHRdAP/9f+zs7Ly8vN3FAJZ4ALmVALLO4l1KAP/YALmhAGheOoaRhIt5AHVtTaKVV7K5yv/sP/Hx8l1RIqSjk6KNALLd9XRnM7u6tsXk9f/UAJaDOGpWAP/9L//iAMe2AH9mAMuiAFpPIv/0X7y5rfb29sWiAKKTALCNAINpAHttDHRhAK+PAJd7ALKysmhTAP/MALKywev4/9jy/4tvAMXr//r9//PCAP//APTz8E1ADHx1WVFEEK6okLvo/9nZ2fv7+2FjQIuDX9Pw/7bm/87u/5e6wNHOwKzb8N3a0PX7/1hHAHRqQPD6/0w9AKKbAFRIF+L1/66LAGhdMN3z/97e3nyPgFNNILq1oKOcgHx1YOe5AMDp/255YJ6bkcrt/+jn4MWdAHRpAP/yv8XBsIN9ZW5mSKSjnPPiAExCEGhuUOPj4+fn53hgAMbGxquqp1pYMG5mQ2FXLYOZ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8cDk74giLO8Q2pedwnUwgw+clXnpeAAAA7"
    }
};

// user has premium toolbar
premium = $("#sswmenu .imgmenu").exists();

function getQueryParams(qs) {
    qs = qs.split("+").join(" ");
    var params = {}, tokens, re = /[?&]?([^=]+)=([^&]*)/g;
    while (tokens = re.exec(qs)) {
        params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
    }
    return params;
}

function sswlink(item) {
    // the only different one because it doesn't use a URL
    return "<img item='"+ item + "' class='sswstuff searchimg' src='" + linkmap.ssw.img + "'>";
}

function sswopen(item) {
    if($(".sswdrop").hasClass("panel_hidden")) {
        $("#sswmenu .imgmenu").click();
    }

    if($("#ssw-tabs-1").hasClass("ui-tabs-hide")) {
        $('#ssw-tabs').tabs('select', 0);
    }

    $("#ssw-criteria").val("exact");

    $("#searchstr").val(item);
}

$(".sswstuff").live("click", function() {
    sswopen($(this).attr("item"));
});

function combiner(item, url, image) {
    url = url.replace("%s", item); // javascript needs sprintf.
    return  "<a target='_blank' href='" + url + "'><img src='" + image + "' class='searchimg'></a>";
}

// overall linker thing
function makelinks(item, extras) {
    // extras is an object that can only have boolean of 'cash' and 'wearable' (for now) | and a string/int number 'itemid' (only needed for wearable being true)
    links = "<br>";

    item = $.trim(item);
    if (typeof extras === "undefined") {
        extras = {cash: false, wearable: false, tradeable: true}
    }

    if (typeof extras.tradeable === "undefined") {
        extras.tradeable = true;
    }

    item = item.replace(/&/g, "%26");

    if(extras.cash == false && extras.tradeable == true) {
        if(document.URL.indexOf("quests.phtml") == -1) { // doesn't show either SW if you're on a quest
            // SSW
            if(premium) {
                links += sswlink(item);
            }

            // Regular SW
            links += combiner(item, linkmap.sw.url, linkmap.sw.img);
        }

        // TP
        links += combiner(item, linkmap.tp.url, linkmap.tp.img);

        // Auctions
        links += combiner(item, linkmap.au.url, linkmap.au.img);
    }

    // SDB
    if(document.URL.indexOf("safetydeposit") == -1) {
        links += combiner(item, linkmap.sdb.url, linkmap.sdb.img);
    }

    // Closet
    if(extras.wearable) {
        if(document.URL.indexOf("closet.phtml") == -1) {
            links += combiner(item, linkmap.closet.url, linkmap.closet.img);
        }
    }

    // JN items
    links += combiner(item, linkmap.jni.url, linkmap.jni.img);
    
    // SN items
    links += combiner(item, linkmap.sn.url, linkmap.sn.img);

    // DTI
    if(extras.wearable) {
        if(extras.itemid != -1 && typeof extras.itemid != "undefined") {
            link = "http://impress.openneo.net/items/" + extras.itemid;
        } else {
            link = "http://impress.openneo.net/items?utf8=%E2%9C%93&q=%s&commit=search";
        }
        links += combiner(item, link, linkmap.dti.img);
    }

    return links;
}

jQuery.fn.justtext = function() {
    return $(this).clone().children().remove().end().text();
};

//////////////////////////////////////////////////////////////////////////////////////////

/*
 Adds the search icons under things in:
 Inventory
 SDB
 Closet
 Auction Bidding Page
 Your Shop Price Page
 Coincidence
 MI Training School
 KI Training School
 Snow Faerie
 Esophagor
 Edna Quest
 Kitchen Quest
 Illusen/Jhudora
 Employment Agency
 Faerie Quest Page
*/

br = "<br>";
hr = "<hr>";

// Main Shops
if(document.URL.indexOf("objects.phtml?type=shop") != -1) {
    $("img[src*='/items/']").parent().parent().find("b").each(function(k,v) {
        $(v).after(makelinks($(v).text()) + br);
    });
}

// Hospital
if(document.URL.indexOf("/hospital.phtml") != -1) {
    $("img[src*='/items/']").parent().prev().find("b").each(function(k,v) {
        $(v).after(makelinks($(v).text()) + br).before(br);
        $(v).parent().width(150);
    });
}

// Redeeming Cash
if(document.URL.indexOf("process_cash_object") != -1) {
    extras = {cash: true, wearable: true};
    $("img[src*='/items/']").parent().find("b").each(function(k,v) {
        $(v).before(br).after(makelinks($(v).text(), extras) + br);
    });
}

// Auctions
if(document.URL.indexOf("auction_id") != -1) {
    nameb = $("b:contains('owned by')");
    fixname = nameb.html();
    fixname = fixname.substr(0, fixname.indexOf(" (own")); // remove "owned by..."
    nameb.parent().find("img").after(makelinks(fixname));
}

// Inventory
if(document.URL.indexOf("inventory") != -1) {
    $("img[src*='/items/']").each(function(k,v) {
        $nametd = $(v).parent().parent();

        extras = {cash: $(v).hasClass("otherItem"), wearable: $nametd.hasClass("wearable"), itemid: -1};

        if ($nametd.find("hr").exists()) {

            extras.tradeable = !$nametd.find("span:contains('(no trade)')").exists();
            $nametd.find("hr").before(makelinks($nametd.justtext(), extras));
        } else {
            $nametd.append(makelinks($nametd.justtext(), extras));
        }
    });
}

// SDB & Closet
// only downside is not knowing if something is NC if it's in the closet. Oh well, no way to know.
if(document.URL.indexOf("safetydeposit") != -1 || document.URL.indexOf("closet") != -1) {
    $("img[src*='/items/']").each(function(k,v) {
        id = $(v).parent().parent().find("td").eq(5).find("input").attr("name").match(/\d+/g)[0];

        iswearable = $(v).parent().parent().find("td").eq(1).text().indexOf("(wearable)") != -1;
        if(document.URL.indexOf("closet") != -1) { // because it'll always be wearable if it's in the closet...
            iswearable = true;
        }
        category = $(v).parent().parent().find("td").eq(3);
        extras = {cash: (category.text().trim() == "Neocash"), wearable: iswearable, itemid: id};
        nametd = $(v).parent().parent().find("td").eq(1);
        nametd.find("b").eq(0).after(makelinks(nametd.find("b").eq(0).justtext(), extras));
    });
}

// Shop
if(document.URL.indexOf("type=your") != -1 || document.URL.indexOf("market_your") != -1) {
    $("img[src*='/items/']").each(function(k,v) {
        nametd = $(v).parent().parent().find("td").eq(0);
        itemname = nametd.text();
        itemname = itemname.replace(nametd.find(".medText").text(), "");

        nametd.find("b").eq(0).after(makelinks(itemname));
    });
}

// Coincidence
if(document.URL.indexOf("coincidence") != -1) {
    $("img[src*='/items/']").each(function(k,v) {
        nametd = $(v).parent();
        nametd.find("b").eq(0).after(makelinks(nametd.justtext()));
    });
}

// MI Training
if(document.URL.indexOf("/island/training.phtml?type=status") != -1) {
    $("img[src*='/items/']").each(function(k,v) {
        $(v).after(makelinks($(v).prev().text()) + "<hr>");
    });
}

// Secret Training
if(document.URL.indexOf("/island/fight_training.phtml?type=status") != -1) {
    $("img[src*='/items/']").each(function(k,v) {
        $(v).after(makelinks($(v).prev().text()) + "<hr>");
    });
}

// KI Training
if(document.URL.indexOf("/pirates/academy.phtml?type=status") != -1) {
    $("img[src*='/items/']").each(function(k,v) {
        nametd = $(v).parent();
        itemname = nametd.parent().find("td > b").eq(0).text();
        nametd.parent().find("td > b").eq(0).after(makelinks(itemname));
    });
}

// Snow Faerie
// essentially same as kitchen. woo, lazy!
if(document.URL.indexOf("snowfaerie") != -1) {
    addhr = (document.URL.indexOf("snowfaerie2") == -1);
    hr = addhr ? "<hr>" : "";
    $("img[src*='/items/']").parent().find("b").each(function(k,v) {
        $(v).after(makelinks($(v).text()) + hr);
    });
}

// Esophagor
if(document.URL.indexOf("esophagor") != -1) {
    $("img[src*='/items/']").each(function(k,v) {
        itemname = $(v).parent().find("b");
        itemname.after(makelinks(itemname.text()));
    });
}

// Edna
if(document.URL.indexOf("witchtower") != -1) {
    $("img[src*='/items/']").each(function(k,v) {
        itemname = $(v).parent().find("b");
        itemname.after(makelinks(itemname.text()));
    });
}

// Kitchen
if(document.URL.indexOf("kitchen") != -1) {
    addhr = (document.URL.indexOf("kitchen2") == -1);
    hr = addhr ? "<hr>" : "";
    $("img[src*='/items/']").parent().find("b").each(function(k,v) {
        $(v).after(makelinks($(v).text()) + hr);
    });
}

// illusen & jhudora
if($("img[src*='ef_2.gif']").exists() || $("img[src*='darkfaeriequest2.gif']").exists()) {
    itemname = $("center:contains('Where is my') > b").text();
    $("center:contains('Where is my')").parent().find("img[src*='/items/']").after(makelinks(itemname));
}

// employment agency
if(document.URL.indexOf("employment") != -1) {
    $("b:contains('Find')").eq(0).after(makelinks($("b:contains('Find')").eq(0).justtext()));
}

// Faerie Quests
if(document.URL.indexOf("quests") != -1) {
    $("img[src*='/items/']").each(function(k,v) {
        itemname = $(v).parent().find("b");
        itemname.after(makelinks(itemname.text()));
    });
}

// Kadoatery
if(document.URL.indexOf("kadoatery") != -1) {
    $("td:contains('You should give it'):not(:contains('Thanks,'))").each(function(k,v) {
        itemname = $(v).find("strong").last();
        itemname.after(makelinks(itemname.text()));
    });
}

// Shop Wiz Auto-Exact
if(document.URL.indexOf("type=wizard&string=") != -1) {
    $("[name='shopwizard']").val(getQueryParams(document.location.search).string);
    $("[name='criteria']").val("exact");
}