Skip to content

Instantly share code, notes, and snippets.

@RatserX
Last active December 29, 2017 04:37
Show Gist options
  • Save RatserX/d56f3e592f13a4c7c309d20da7aa59e5 to your computer and use it in GitHub Desktop.
Save RatserX/d56f3e592f13a4c7c309d20da7aa59e5 to your computer and use it in GitHub Desktop.
steam-discovery-queue-explorer
// ==UserScript==
// @description Automatically explore your Steam Discovery Queue to get the daily Trading Cards during sales
// @icon https://store.steampowered.com/favicon.ico
// @match http://store.steampowered.com/explore*
// @match https://store.steampowered.com/explore*
// @name Steam Discovery Queue Explorer
// @namespace https://github.com/RatserX/steam-discovery-queue-explorer
// @version 1.0
// @updateURL https://raw.githubusercontent.com/RatserX/steam-discovery-queue-explorer/master/sdqe.js
// ==/UserScript==
var maxQueueNumber = 3;
var perf = {
objectToQueryString: function(object) {
var url = [];
for (var property in object) {
if (object.hasOwnProperty(property)) {
var value = object[property];
if (toString(value) == "[object Object]") {
url.push(this.objectToQueryString(value));
continue;
}
}
url.push(property + "=" + value);
}
return url.join("&");
},
xhr: function(settings) {
settings.async = settings.async || true;
settings.user = settings.user || null;
settings.password = settings.password || null;
settings.mediatype = settings.mediatype || "";
settings.charset = settings.charset || "";
settings.boundary = settings.boundary || "";
settings.responseType = settings.responseType || "text";
settings.timeout = settings.timeout || 0;
settings.withCredentials = settings.withCredentials || false;
settings.body = settings.body || null;
settings.promise = (typeof Promise != "undefined" ? (settings.promise || false) : false);
var executor = function(resolve, reject) {
var contentType = "";
contentType += settings.mediatype;
contentType += (contentType != "" ? ";" : "");
contentType += (settings.charset != "" ? "charset=" + settings.charset : "");
contentType += (contentType != "" ? ";" : "");
contentType += (settings.boundary != "" ? "boundary=" + settings.boundary : "");
var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open(settings.method, settings.url, settings.async, settings.user, settings.password);
xmlHttpRequest.setRequestHeader("Content-Type", contentType);
xmlHttpRequest.responseType = settings.responseType;
xmlHttpRequest.timeout = settings.timeout;
xmlHttpRequest.withCredentials = settings.withCredentials;
xmlHttpRequest.onreadystatechange = function() {
var data = {
xmlHttpRequest: this,
event: event
}
if (this.readyState == 4) {
if (this.status == 200) {
if (settings.promise) {
resolve(data);
}
if (settings.hasOwnProperty("onsuccess")) {
if (typeof settings.onsuccess == "function") {
settings.onsuccess(data);
}
}
} else {
if (settings.promise) {
reject(data);
}
if (settings.hasOwnProperty("onerror")) {
if (typeof settings.onerror == "function") {
settings.onerror(data);
}
}
}
}
}
xmlHttpRequest.ontimeout = function() {
var data = {
xmlHttpRequest: this,
event: event
}
if (Object.prototype.hasOwnProperty.call(settings, "ontimeout")) {
if (typeof settings.ontimeout == "function") {
if (settings.promise) {
reject(data);
}
settings.ontimeout(data);
}
}
}
xmlHttpRequest.send(settings.body);
return true;
}
if (settings.promise) {
return new Promise(executor);
} else {
return executor(null, null);
}
}
}
function generateQueue(queueNumber) {
var blockingWaitDialog = ShowBlockingWaitDialog("Steam Discovery Queue Explorer - Request", "Generating new discovery queue: " + (++queueNumber) + " / " + maxQueueNumber);
var settings = {
mediatype: "application/x-www-form-urlencoded",
charset: "utf-8",
method: "POST",
url: "http://store.steampowered.com/explore/generatenewdiscoveryqueue",
body: {
sessionid: g_sessionID,
queuetype: 0
},
promise: false,
onsuccess: function(data) {
var jsonResponseText = JSON.parse(data.xmlHttpRequest.responseText);
var queue = jsonResponseText.queue;
var queueLength = queue.length;
var queueItem = 0;
var settings = {
mediatype: "application/x-www-form-urlencoded",
charset: "utf-8",
method: "POST",
url: "http://store.steampowered.com/app/10",
body: {
appid_to_clear_from_queue: 0,
sessionid: g_sessionID
},
promise: true
};
var body = settings.body;
var xhrApps = [];
for (var i = 0; i < queueLength; i++) {
body.appid_to_clear_from_queue = queue[i];
settings.body = perf.objectToQueryString(body);
var xhrApp = perf.xhr(settings);
xhrApp.then(function(data) {
blockingWaitDialog.Dismiss();
blockingWaitDialog = ShowBlockingWaitDialog("Steam Discovery Queue Explorer - Request", "Exploring queue item: " + (++queueItem) + " / " + queueLength);
});
xhrApp.catch(function(data) {
blockingWaitDialog.Dismiss();
blockingWaitDialog = ShowConfirmDialog("Steam Discovery Queue Explorer - Error", "Failed to clear queue item: " + queueItem + " / " + queueLength, "Try again");
blockingWaitDialog.done(function(doneCallbacks) {
if (doneCallbacks == "OK") {
generateQueue(queueNumber - 1);
}
});
})
xhrApps.push(xhrApp);
}
var promiseAll = Promise.all(xhrApps);
promiseAll.then(function(data) {
blockingWaitDialog.Dismiss();
if (queueNumber < maxQueueNumber) {
generateQueue(queueNumber);
} else {
blockingWaitDialog = ShowConfirmDialog("Steam Discovery Queue Explorer - Finish", "Queue explored: " + queueNumber + " / " + maxQueueNumber, "Reload");
blockingWaitDialog.done(function(doneCallbacks) {
if (doneCallbacks == "OK") {
blockingWaitDialog.Dismiss();
blockingWaitDialog = ShowBlockingWaitDialog("Steam Discovery Queue Explorer - Finish", "Reloading . . .");
window.location.reload();
}
});
}
});
},
onerror: function(data) {
blockingWaitDialog.Dismiss();
blockingWaitDialog = ShowConfirmDialog("Steam Discovery Queue Explorer - Error", "Failed to clear queue item: " + queueNumber + " / " + maxQueueNumber, "Try again");
blockingWaitDialog.done(function(doneCallbacks) {
if (doneCallbacks == "OK") {
generateQueue(queueNumber - 1);
}
});
}
};
settings.body = perf.objectToQueryString(settings.body);
var xhrQueue = perf.xhr(settings);
}
generateQueue(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment