SteamTrades - Have List Filter

Check if you own the games from someone's have list (instant Compare2Steam)

目前為 2016-11-07 提交的版本,檢視 最新版本

// ==UserScript==
// @name         SteamTrades - Have List Filter
// @namespace    Royalgamer06
// @version      1.1
// @description  Check if you own the games from someone's have list (instant Compare2Steam)
// @author       Royalgamer06
// @match        https://www.steamtrades.com/trade/*
// @grant        GM_xmlhttpRequest
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_setClipboard
// @support      https://www.steamgifts.com/discussion/fN8vR/userscript-steamgifts-trades-have-list-filter-compare2steam-alternative
// ==/UserScript==

//If you want to set your/another SteamID64 manually, state it here between brackets. (For example, to check owned games from someone else)
var manual_steamID = "";

//Set your steam sync interval in hours. (Set to 0 if you want to force a sync)
var sync_interval = 6;

//Set link to search game to SteamDB instead of the Steam Store? (SteamDB finds removed games from the steam store and gives better results overall)
var steamdb_instead = false;

//Do not touch below :)
$(document).ready(function() {
    $(".have").parent().find(".trade_heading").first().html("I have... (<a href='javascript:void(0)' id='filter' style='text-decoration:underline;color:rgb(120,169,71)' title='Check have-list for owned games. \n" +
                                                            "Warning: It may become unresponsive for a while.'>Filter</a>)");
    $("#filter").click(function() {
        $("#filter").removeAttr("style").html("<i class='fa fa-spin fa-circle-o-notch' />");
        if (manual_steamID.length == 17) {
            syncHandler(manual_steamID);
        } else if (GM_getValue("steamID") !== undefined) {
            syncHandler(GM_getValue("steamID"));
        } else {
            if ($(".nav_avatar").length > 0) {
                var steamID = $(".nav_avatar").attr("href").split("user/")[1];
                if (steamID.length == 17) {
                    GM_setValue("steamID", steamID);
                    syncHandler(steamID);
                } else {
                    alert("[Have List Filter] ERROR: Invalid SteamID. Refresh the page to try again, or set manual SteamID");
                }
            } else {
                alert("[Have List Filter] ERROR: Could not fetch SteamID automatically. Refresh the page to try again, or set manual SteamID");
            }
        }
    });
});

function syncHandler(steamID) {
    var now = new Date().valueOf();
    var last = GM_getValue("lastsync");
    if (last === undefined) {
        GM_setValue("lastsync", now);
        syncHandler(steamID);
    } else if ((now - last) / 3600000 > sync_interval || GM_getValue("ownedSteamGames") === undefined || GM_getValue("wlSteamGames") === undefined) {
        GM_xmlhttpRequest({
            method: "GET",
            url: "http://steamcommunity.com/profiles/" + steamID + "/games/?xml=1",
            onload: function(gdata) {
                var xml = $.parseXML(gdata.responseText);
                var ownedSteamGames = [];
                $("name", xml).each(function() {
                    ownedSteamGames.push($(this).text().toLowerCase().replace(/\:|\-|\–|\\|\/|\™|\®| |\'|\.|\?|\!/g, ""));
                });
                GM_setValue("ownedSteamGames", ownedSteamGames.join("-"));
                GM_xmlhttpRequest({
                    method: "GET",
                    url: "http://steamcommunity.com/profiles/" + steamID + "/wishlist",
                    onload: function(wldata) {
                        var wlSteamGames = [];
                        $("#wishlist_items h4", wldata.responseText).each(function() {
                            wlSteamGames.push($(this).text().toLowerCase().replace(/\:|\-|\–|\\|\/|\™|\®| |\'|\.|\?|\!/g, ""));
                        });
                        GM_setValue("wlSteamGames", wlSteamGames.join("-"));
                        GM_setValue("lastsync", now);
                        console.log("[Have List Filter] Synced with steam!");
                        filterHaveList(ownedSteamGames, wlSteamGames);
                    }
                });
            }
        });
    } else {
        var ownedSteamGames = GM_getValue("ownedSteamGames");
        var wlSteamGames = GM_getValue("wlSteamGames");
        filterHaveList(ownedSteamGames.split("-"), wlSteamGames.split("-"));
    }
}

function filterHaveList(ownedSteamGames, wlSteamGames) {
    var have = document.getElementsByClassName("have")[0].innerText.split("\n");
    var have_unowned = "";
    var have_unowned_raw = "";
    have.forEach(function(game) {
        if (game.length > 1 && game.length < 80) {
            // Ignoring everything between and including brackets, and also these symbols: :, -, –, \, /, ™, ®,  , ', ., ? and ! (Better suggestions? Please contact me)
            if ($.inArray(game.toLowerCase().replace(/( *\[[^)]*\] *)|( *\([^)]*\) *)|\=.*|\:|\-|\–|\\|\/|\™|\®| |\'|\.|\?|\!/g, ""), wlSteamGames) > -1) { //Game is wishlisted
                //console.log (game + ": wishlisted");
                $(".have").html($(".have").html().replace(game, "<i class='fa icon-purple fa-heart' style='color:purple;' title='You have this game wishlisted on steam'></i> " + game));
            }
            if ($.inArray(game.toLowerCase().replace(/( *\[[^)]*\] *)|( *\([^)]*\) *)|\=.*|\:|\-|\–|\\|\/|\™|\®| |\'|\.|\?|\!/g, ""), ownedSteamGames) > -1) { //Game is owned
                //console.log (game + ": owned");
                $(".have").html($(".have").html().replace(game, "<i class='fa icon-green fa-check-circle' style='color:green;' title='You own this game on steam'></i> " + game));
            } else { //Game is not owned or unknown game
                //console.log (game + ": not owned");
                $(".have").html($(".have").html().replace(game.replace(/( *\[[^)]*\] *)|( *\([^)]*\) *)|\=.*/g,""),
                                                          "<i class='fa icon-red fa-times-circle' style='color:red;' title='You do not own this game on steam, or unknown game: \nPlease check manually'></i> " +
                                                          game.replace(/( *\[[^)]*\] *)|( *\([^)]*\) *)|\=.*/g,"") + " <a href='" +
                                                          (steamdb_instead ? "https://steamdb.info/search/?a=app&q=" : "https://store.steampowered.com/search/?term=") +
                                                          encodeURIComponent(game.replace(/( *\([^)]*\) *)/g,"")) +
                                                          "' target='_blank'><i class='fa icon-grey fa-external-link' title='Search game on " +
                                                          (steamdb_instead ? "SteamDB" : "Steam") +
                                                          "'></i></a> "));
                have_unowned += game.replace(/(\"|\')|\=.*/g, '') + "&#013;";
                have_unowned_raw += game + "\n";
            }
        }
    });
    $(".trade_heading").first().html("<i id='clip' data-clipboard-text='" + have_unowned + "' class='icon_to_clipboard fa fa-fw fa-copy' style='cursor: pointer' title='Copy unowned/unknown games to clipboard'></i> I have...");
    $("#clip").click(function() {
        GM_setClipboard(have_unowned_raw);
        alert("Successfully copied unowned have-list games to clipboard!");
    });
}