// ==UserScript== // @name Steam亲子鉴定(游戏库比较) // @license MIT // @namespace http://tampermonkey.net/ // @version 0.1.0 // @description 比较TA与自己的游戏库,测测TA是否有资格做自己的义父 // @author Rawwiin // @match https://steamcommunity.com/id/*/games/* // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== // @grant GM_xmlhttpRequest // @downloadURL none // ==/UserScript== const url_my_wishlist = "https://steamcommunity.com/my/wishlist/"; // const wishlistUrl = "https://store.steampowered.com/wishlist" const url_my_games = "https://steamcommunity.com/my/games?tab=all"; const color_own = "#54662f"; const color_wish = "#4c90b6"; var isMarkOwn = true; var isMarkWish = true; var isHideOwn = false; var shownCount = 0; var myAppidList; var myWishAppidList; var hisAppidList; var hisGameMap; (function () { "use strict"; console.log("steam..."); const promise1 = new Promise((resolve, reject) => { load(url_my_games, (res) => { myAppidList = getAppids(res); resolve(); }); }); const promise2 = new Promise((resolve, reject) => { load(url_my_wishlist, (res) => { myWishAppidList = getAppids(res); resolve(); }); }); const promise3 = loadHisGameList(1000); Promise.all([promise1, promise2, promise3]) .then(() => { console.log(myAppidList, myWishAppidList); console.log("请求都已完成"); initHisAppidMap(); markMyOwn(isMarkOwn); markMyWish(isMarkWish); addStatusElement(); }) .catch((error) => { // console.error("至少一个请求失败:", error); }); })(); function loadHisGameList(interval) { return new Promise((resolve) => { let count = 0; const intervalId = setInterval(() => { if (++count > 10 || (hisGameMap && hisAppidList)) { // 结束定时器 clearInterval(intervalId); resolve(); return; } let gameslist_config = document.getElementById("gameslist_config"); if (gameslist_config) { let data_profile_gameslist = gameslist_config.getAttribute( "data-profile-gameslist" ); hisAppidList = getAppids(data_profile_gameslist); clearInterval(intervalId); resolve(); let appidRegex = /app\/(\d+)/; var targetNode = document.getElementsByClassName( "_3tY9vKLCmyG2H2Q4rUJpkr" )[0]; // 创建一个观察者对象 var observer = new MutationObserver(function (mutations) { mutations.forEach(function (mutation) { if (mutation.type === "childList") { if (hisGameMap == null) { initHisAppidMap(); } else { mutation.addedNodes.forEach((addedNode) => { let appid = getAppidFromElement(addedNode); hisGameMap.set(appid, addedNode); handleNewGameDiv(appid, addedNode); }); } } }); }); // 传入目标节点和观察选项 observer.observe(targetNode, { // attributes: true, childList: true, // subtree: true, }); // observer.disconnect(); } initHisAppidMap(); }, interval); }); } function initHisAppidMap() { if (hisGameMap) { return; } var gameListElement = document.getElementsByClassName( "_29H3o3m-GUmx6UfXhQaDAm" ); if (gameListElement) { hisGameMap = new Map(); shownCount = 0; for (var i = 0; i < gameListElement.length; ++i) { let appid = getAppidFromElement(gameListElement[i]); hisGameMap.set(appid, gameListElement[i]); } } } function addStatusElement() { let element = document.getElementsByClassName("_2_BHLBIwWKIyKmoabfDFH-"); if (!element || !element.length) { return; } let notHave = 0; let inWish = 0; hisAppidList.forEach(function (appid) { if (!myAppidList.includes(appid)) { notHave++; } if (myWishAppidList.includes(appid)) { inWish++; } }); element[0].innerHTML += // "
" + "" + "" + ""; // +"
" var checkbox = document.getElementById("checkbox_hideMine"); if (checkbox) { checkbox.addEventListener("change", function () { isHideOwn = checkbox.checked; hideGameList(isHideOwn); }); } // let span_notHave = document.getElementById("notHave"); // if (span_notHave) span_notHave.textContent = notHave; // let span_inWish = document.getElementById("inWish"); // if (span_inWish) span_inWish.textContent = inWish; } function hideGameList(hide) { shownCount = 0; hisGameMap.forEach(function (gameDiv, appid) { hideGame(hide, appid, gameDiv); }); } function hideGame(hide, appid, gameDiv) { if (hide && myAppidList.includes(appid)) { gameDiv.style.display = "none"; gameDiv.style.top = "0px"; } else { gameDiv.style.display = "block"; gameDiv.style.top = shownCount++ * 150 + "px"; } } function handleNewGameDiv(appid, gameDiv) { hideGame(isHideOwn, appid, gameDiv); markGame(appid, gameDiv); } function markMyOwn(mark) { markHisGameList(myAppidList, mark, color_own); } function markMyWish(mark) { markHisGameList(myWishAppidList, mark, color_wish); } function markHisGameList(appidList, mark, color) { hisGameMap.forEach(function (gameDiv, appid) { if (appidList.includes(appid)) { gameDiv.style.backgroundColor = mark ? color : ""; } }); } function markGame(appid, gameDiv) { let color = ""; if (isMarkOwn && myAppidList.includes(appid)) { color =color_own; } else if (isMarkWish && myWishAppidList.includes(appid)) { color = color_wish; } gameDiv.style.backgroundColor = color; } function load(url, resolve) { try { return GM_xmlhttpRequest({ method: "GET", url: url, onload: function (xhr) { // console.log(xhr); resolve(xhr.responseText); }, }); } catch (e) { // location.href = 'https://keylol.com'; } } function getAppids(res) { let appidRegex = /appid("|\\"|"):(\d+)/g; let appidList = []; let appid = ""; while ((appid = appidRegex.exec(res))) { // console.log(appid[1]); appidList.push(appid[2]); } return appidList; } function getAppidFromElement(element) { let appidRegex = /app\/(\d+)/; let href = element .getElementsByClassName("_22awlPiAoaZjQMqxJhp-KP")[0] .getAttribute("href"); let appid = appidRegex.exec(href); return appid[1]; }