Steam 免费游戏永久入库脚本

steamdb.info屏蔽周末临时免费 Steam 免费游戏永久入库脚本

当前为 2020-04-21 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Steam 免费游戏永久入库脚本
// @namespace    https://greasyfork.org/zh-CN/scripts/401595
// @version      0.1
// @description  steamdb.info屏蔽周末临时免费   Steam 免费游戏永久入库脚本
// @author       Boys
// @match        https://steamdb.info/upcoming/free/
// @match        https://store.steampowered.com/account/licenses/
// @icon         https://steamdb.info/static/logos/512px.png
// @note         2020年4月21日 修复部分使用的问题
// @grant        GM_getValue
// @grant        GM_setValue
// @run-at       document-end
// ==/UserScript==
(function () {
    'use strict';
    Array.prototype.notempty = function () {
        let arr = [];
        this.map(function (val, index) {
            if (val !== "" && val != undefined) {
                arr.push(val);
            }
        });
        return arr;
    }
    function load_licenses(res) {
        if (typeof res == "undefined" || res == null || res.replace(/\s*/g, "") == "" || !res) return;
        if (location.hostname !== 'store.steampowered.com') {
            alert('Run this code on theSteamStore!');
            return;
        } else if (typeof jQuery !== 'function') {
            ShowAlertDialog('Fail', 'This page has no jQuery, try homepage.');
            return;
        } else if (document.getElementById('header_notification_area') === null) {
            ShowAlertDialog('Fail', 'You have to be logged in.');
            return;
        }
        let freePackages = res.replace(/\s*/g, "").split(",").notempty();
        let ownedPackages = {};
        jQuery('.account_table a').each(function (i, el) {
            const match = el.href.match(/javascript:RemoveFreeLicense\( ([0-9]+), '/);
            if (match !== null) {
                ownedPackages[+match[1]] = true;
            }
        });
        let loaded = 0,
            packages = 0,
            total = freePackages.length,
            modal = ShowBlockingWaitDialog('努力执行中...', '请耐心等待,如果有错误请无视,请耐心等待脚本加载完毕');
        for (let i = 0; i < total; i++) {
            packages = freePackages[i];
            if (ownedPackages[packages]) {
                loaded++;
                continue;
            }
            jQuery.post('//store.steampowered.com/checkout/addfreelicense', {
                action: 'add_to_cart',
                sessionid: g_sessionID,
                subid: packages
            }).always(function () {
                loaded++;
                modal.Dismiss();
                if (loaded >= total) {
                    GM_setValue("steam_db_free_info_store_ok", true);
                    ShowAlertDialog('任务执行完成', '请刷新网页查看....');
                    // location.reload();
                } else {
                    modal = ShowBlockingWaitDialog('执行中...', '加载至 <b>' + loaded + '</b>/' + total + '.');
                }
            });//always-end
        }//for-end
    }//load_licenses-end
    function flashing_prompt(count, time) {
        let timeout
        function clear() {
            --count
            if (count === 0) {
                clearInterval(timeout)
                timeout = null;
            }
        }
        function exec() {
            $("#dark-mode-toggle .dark-mode-slider").click()
            clear();
        }
        timeout = setInterval(() => {
            exec();
        }, time)
    }//flashing_prompt-end
    let local = window.location.href;
    if (local.indexOf("steamdb.info/upcoming/free") != -1) {
        $("td").each(function () {
            if ($(this).text() == "Weekend") $(this).parent().remove();
        })
        let v = "";
        $(".timeago:even").each(function (index, value, array) {
            if ($(this).text().indexOf("ago") == -1)
                return false;
            else
                v += $(this).parent().attr("data-subid") + ",";
        })
        if (GM_getValue("steam_db_free_info_store_ok") != true) flashing_prompt(10, 500);
        let oldValue = GM_getValue("steam_db_free_info_store")
        if (oldValue === v) return;
        let freePackages = v.replace(/\s*/g, "").split(",").notempty();
        if (freePackages.forEach(function (value, index, array) {
            if (oldValue === "undefined" || oldValue == null) return false;
            if (value === "undefined" || value == null) return false;
            if (oldValue.indexOf(value) === -1) return false;
        })) return;
        GM_setValue("steam_db_free_info_store", v);
        GM_setValue("steam_db_free_info_store_ok", false);
        console.log("set steam_db_free_info_store=" + v);
        console.log("set steam_db_free_info_store_ok=" + false);
        return;
    }
    if (local.indexOf("store.steampowered.com/account/licenses") != -1) {
        let v = GM_getValue("steam_db_free_info_store");
        console.log("get steam_db_free_info_store=" + v);
        setTimeout(function () {
            if (GM_getValue("steam_db_free_info_store_ok") == true) return;
            load_licenses(v);
        }, 1000);
        return;
    }
})();