Created
April 16, 2024 08:12
-
-
Save Revadike/b099a2faa81af26741281d3fb84a591b to your computer and use it in GitHub Desktop.
Free Steam Emoticons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Free Steam Emoticons | |
// @namespace revadike | |
// @version 0.1 | |
// @description Continuously requests Steam store search results and checks if total results are divisible by 100 or if no new results are found. | |
// @author revadike | |
// @match https://steam.tools/emoticons* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=steam.tools | |
// @grant GM_xmlhttpRequest | |
// @grant GM_setValue | |
// @grant GM_getValue | |
// @run-at document-idle | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// Function to make HTTP request | |
function makeRequest(page, prev = []) { | |
return new Promise((resolve, reject) => { | |
GM_xmlhttpRequest({ | |
method: "GET", | |
url: `https://store.steampowered.com/search/results/?query&start=${page * 100}&count=100&sort_by=_ASC&maxprice=free&category1=998&category2=29&supportedlang=english&json=1`, | |
onload: function(response) { | |
const data = JSON.parse(response.responseText); | |
const results = data.items.map(({ logo }) => logo?.split("/")[5]); | |
// Check if total results are divisible by 100 or if no new results | |
if (results.length !== 100) { | |
console.log("Condition met. Exiting loop."); | |
resolve(prev.concat(results)); | |
} else { | |
// If condition not met, make another request | |
makeRequest(page + 1, prev.concat(results)).then(resolve); | |
} | |
}, | |
onerror: function(error) { | |
console.error("Error occurred:", error); | |
reject(error); | |
} | |
}); | |
}); | |
} | |
// Function to get data from cache or refresh | |
async function getData() { | |
const cachedResults = (GM_getValue('cachedResults', '')).split(','); | |
const lastRefreshDate = new Date(GM_getValue('lastRefreshDate', 0)); | |
// If cached data is not older than 1 day, return cached data | |
if (Date.now() - lastRefreshDate.getTime() <= 24 * 60 * 60 * 1000) { | |
console.log("Using cached data."); | |
return cachedResults; | |
} | |
// If data is older than 1 day, refresh | |
console.log("Data is older than 1 day. Refreshing..."); | |
const results = await makeRequest(0); | |
GM_setValue('cachedResults', results.join(',')); | |
GM_setValue('lastRefreshDate', (new Date()).getTime()); | |
return results; | |
} | |
function waitForElement(selector, callback) { | |
const element = document.querySelector(selector); | |
if (element) { | |
callback(element); | |
} else { | |
setTimeout(() => { | |
waitForElement(selector, callback); | |
}, 100); // Adjust the interval as needed (e.g., 100ms) | |
} | |
} | |
// Start by getting data | |
getData().then(data => { | |
console.log("Data retrieved:", data); | |
waitForElement('#items', () => { | |
const body = document.body, | |
html = document.documentElement; | |
const height = Math.max( body.scrollHeight, body.offsetHeight, | |
html.clientHeight, html.scrollHeight, html.offsetHeight ); | |
document.querySelector("#items").style.minHeight = height + 100 + "px"; | |
setInterval(() => { | |
for (let node of document.querySelectorAll('.box')) { | |
const appid = node.querySelector('[href^="https://steamcommunity.com/market/listings/753/"]').href.split('/')[6]?.split('-')[0] | |
if (!data.includes(appid.toString())) { | |
node.remove(); | |
} | |
} | |
}, 1500); | |
}); | |
}).catch(error => { | |
console.error("Error occurred:", error); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment