Show play stats on Steam Gifts winners pages. Adapted from kelnage's Do You Even Play, Bro? script.
目前為
// ==UserScript==
// @name Did they even play?
// @namespace https://www.steamgifts.com/user/lext
// @version 0.1
// @description Show play stats on Steam Gifts winners pages. Adapted from kelnage's Do You Even Play, Bro? script.
// @author Lex
// @match https://www.steamgifts.com/giveaway/*/winners
// @require http://code.jquery.com/jquery-3.2.1.min.js
// @connect api.steampowered.com
// @grant GM_xmlhttpRequest
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// ==/UserScript==
(function() {
'use strict';
const PLAYTIME_URL = "https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/"; // takes a steamid and API key
const ACHIEVEMENTS_URL = "https://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/"; // takes a steamid, appid and API key
const SG_USER_URL = "https://www.steamgifts.com/user/";
const GAME_ACHIEVEMENTS_URL = "http://steamcommunity.com/profiles/$1/stats/$2/achievements/";
const PLAYTIME_CACHE_KEY = "PLAYTIME_CACHE_";
const ACHIEVEMENT_CACHE_KEY = "ACHIEVEMENT_CACHE_";
const API_KEY_REGEXP = /[0-9A-Z]{32}/;
const APPID = $("div.featured__inner-wrap a.global__image-outer-wrap").attr('href').match(/\/(\d+)/)[1];
var STEAM_API_KEY = GM_getValue("STEAM_API_KEY");
if (STEAM_API_KEY == "undefined") {
var apiDiv = $(`<form><input class="steamApiKey" style="margin-left: 15px; width: 300px" type="text" value="STEAM_API_KEY_HERE"/>
<input style="float:left;" name="btnSubmit" type="button" value="Submit"></input></form>`);
apiDiv.find("input[name='btnSubmit']").click(function(){
const api_key = $(this).parents("form:first").find("input.steamApiKey").val();
if (api_key.match(API_KEY_REGEXP)) {
GM_setValue("STEAM_API_KEY", api_key);
$(this).parent().text(" - API key set");
} else {
alert("Error! Invalid API Key");
}
});
$("div.page__heading__breadcrumbs").append(apiDiv);
return;
} else {
var apiDelDiv = $(`<div style="margin-left: 15px"><a href="#">Delete API Key for Did they even play?</a></div>`);
apiDelDiv.find("a").one("click", function(){
$(this).parent().text("Deleted");
GM_deleteValue("STEAM_API_KEY");
});
$("div.page__heading__breadcrumbs").append(apiDelDiv);
main();
}
function fetchGamePlaytimes(steamID64, callback) {
GM_xmlhttpRequest({
"method": "GET",
"url": PLAYTIME_URL + "?steamid=" + steamID64 + "&key=" + STEAM_API_KEY,
"onload": function(response) {
const data = JSON.parse(response.responseText);
if (data && data.response)
callback(data.response.games);
else
callback();
}
});
}
function fetchGamePlaytime(steamID64, gameid, callback) {
fetchGamePlaytimes(steamID64, function(games) {
for(var i = 0; i < games.length; i++) {
if (games[i].appid == gameid)
return callback(games[i].playtime_forever); // playtime in minutes
}
});
}
function fetchAchievementStats(appid, steamID64, callback) {
GM_xmlhttpRequest({
"method": "GET",
"url": ACHIEVEMENTS_URL + "?appid=" + appid + "&steamid=" + steamID64 + "&key=" + STEAM_API_KEY,
"onload": function(response) {
var data;
try {
data = JSON.parse(response.responseText);
var achievements = data.playerstats.achievements;
if (achievements) {
const achieved = achievements.filter(function(achievement) { return achievement.achieved == 1; }).length;
const total = achievements.length;
callback({"achieved": achieved, "total": total});
} else {
callback({"achieved": 0, "total": 0});
}
} catch(err) {
callback({"achieved": 0, "total": 0});
}
}
});
}
function fetchSteamID (username, callback) {
GM_xmlhttpRequest({
"method": "GET",
"url": SG_USER_URL + username,
"onload": function(response) {
const steamID = $('a[data-tooltip="Visit Steam Profile"]', response.responseText).attr("href").match(/\d{17}/)[0];
callback(steamID);
}
});
}
function main() {
$("p.table__column__heading a").each(function(){
var self = $(this);
fetchSteamID(self.text(), function(steamID) {
const achUrl = GAME_ACHIEVEMENTS_URL.replace("$1", steamID).replace("$2", APPID);
const achievementLink = $(`<a href="${achUrl}"> - <span class="DTEP_ACHIEVEMENTS"/> Achievements</a><span class="DTEP_PLAYTIME"/>`);
self.parent().parent().append(achievementLink);
fetchGamePlaytime(steamID, APPID, function(playtime) {
var hours = playtime / 60;
hours = +hours.toFixed(2);
self.parent().parent().find(".DTEP_PLAYTIME").text(", " + hours + " hours total");
});
fetchAchievementStats(APPID, steamID, function(achievements) {
self.parent().parent().find(".DTEP_ACHIEVEMENTS").text(achievements.achieved + " / " + achievements.total + " ");
});
});
});
}
})();