Steam Collection Manager

Adds buttons to the collection screens related to mass removal and addition of items.

当前为 2021-07-21 提交的版本,查看 最新版本

// ==UserScript==
// @name         Steam Collection Manager
// @version      2.1.1
// @description  Adds buttons to the collection screens related to mass removal and addition of items.
// @author       pointfeev
// @copyright    2021, pointfeev (https://github.com/pointfeev)
// @license      MIT
// @match        https://steamcommunity.com/sharedfiles/*
// @match        https://steamcommunity.com/workshop/filedetails/*
// @icon         https://steamcommunity.com/favicon.ico
// @grant        none
// @namespace    https://github.com/pointfeev
// @homepageURL  https://gist.github.com/pointfeev/31618a04ab2f754158ca7d950e1dd35c
// ==/UserScript==

(function() {
    'use strict';

    if (document.querySelector("div#mainContentsCollection") == null && document.querySelector("div.manageCollectionItemsBody") == null) { return; }

    var collection_id = new URL(document.location.href).searchParams.get('id');
    var g_sessionID = window.g_sessionID;

    var btn_cancel_offset = 0;
    var collection_window = document.querySelector('div.manageCollectionItems');
    var right_contents;
    if (collection_window != null)
    {
        btn_cancel_offset = 63;
        collection_window.style['min-height'] = '220px';
    }
    else
    {
        right_contents = document.querySelector("div#rightContents");
        if (right_contents != null)
        {
            btn_cancel_offset = 69;
            right_contents.style['padding-top'] = "260px";
        }
        else
        {
            return;
        }
    }

    var working = false;
    var manage_collection_page;
    function get_manage_collection_page(btn, func)
    {
        if (manage_collection_page != null) { func(); return; }
        btn.html("Getting collection page . . .");
        if (window.location.pathname == "/sharedfiles/managecollection/")
        {
            manage_collection_page = jQuery(document.documentElement);
            func();
        }
        else
        {
            jQuery.ajax({
                type: "GET",
                url: "https://steamcommunity.com/sharedfiles/managecollection",
                data: {
                    id: collection_id
                },
                success: function (response) {
                    manage_collection_page = jQuery(jQuery.parseHTML(response));
                }
            }).done(func);
        }
    }

    function update_choice_item(childID, add)
    {
        var listItems = ["#choice_MyItems_" + childID, "#choice_MyFavoriteItems_" + childID, "#choice_MySubscribedItems_" + childID];
        for (var i = 0; i < listItems.length; ++i)
        {
            var listElem = manage_collection_page.find(listItems[i]);
            if (listElem)
            {
                if (add)
                {
                    listElem.addClass("inCollection");
                }
                else
                {
                    listElem.removeClass("inCollection");
                }
            }
        }
    }

    var btn_cancel;
    function start_working(btn)
    {
        working = true;

        var btn_dom = btn[0];
        btn_dom.style.right = (parseInt(btn_dom.style.right.replace('px', '')) + btn_cancel_offset) + 'px';

        if (btn_cancel != null)
        {
            btn_cancel.remove();
            btn_cancel = null;
        }
        btn_cancel = document.createElement('a');
        jQuery(btn_cancel).text('Cancel');
        jQuery(btn_cancel).addClass("sectionTab");
        btn_cancel.style.background = btn_dom.style.background;
        btn_cancel.style.color = 'black';
        btn_cancel.style['border-radius'] = '0px';
        btn_cancel.style.position = 'absolute';
        if (collection_window != null)
        {
            btn_cancel.style.top = btn_dom.style.top;
            btn_cancel.style.right = '5px';
            collection_window.insertBefore(btn_cancel, collection_window.firstChild);
        }
        else if (right_contents != null)
        {
            btn_cancel.style.top = btn_dom.style.top;
            btn_cancel.style.right = '0px';
            right_contents.insertBefore(btn_cancel, right_contents.firstChild);
        }
        jQuery(btn_cancel).click(function() {
            location.reload(true);
        });
    }

    function stop_working(btn)
    {
        working = false;

        var btn_dom = btn[0];
        btn_dom.style.right = (parseInt(btn_dom.style.right.replace('px', '')) - btn_cancel_offset) + 'px';

        if (btn_cancel != null)
        {
            btn_cancel.remove();
            btn_cancel = null;
        }
    }

    function remove_item(btn, choice_string)
    {
        if (working) { return; }
        var btn_text = btn.text();
        start_working(btn);
        btn.text("Working . . .");
        get_manage_collection_page(btn, function() {
            var sortable_items = manage_collection_page.find('div#sortable_items div.managedCollectionItem');
            if (sortable_items.length)
            {
                var i = 0;
                sortable_items.each(function() {
                    var item = jQuery(this);
                    var childID = item.attr('id').replace('sharedfile_', '');
                    if (choice_string != null && manage_collection_page.find("#choice_" + choice_string + "_" + childID).length) { return; }
                    i++;
                    btn.text("Removing " + i + " items . . .");
                    jQuery.ajax({
                        type: "POST",
                        url: "https://steamcommunity.com/sharedfiles/removechild",
                        data: {
                            id: collection_id,
                            sessionid: g_sessionID,
                            childid: childID
                        },
                        success: function () {
                            item.remove();
                            update_choice_item(childID, false);
                        }
                    }).done(function() {
                        i--;
                        btn.text("Removing " + i + " items . . .");
                        if (i == 0)
                        {
                            location.reload();
                        }
                    });
                });
                if (i == 0)
                {
                    stop_working(btn)
                    btn.text(btn_text);
                }
            }
            else
            {
                stop_working(btn)
                btn.text(btn_text);
            }
        });
    }

    function add_item(btn, choice_string)
    {
        if (working) { return; }
        var btn_text = btn.text();
        start_working(btn);
        btn.text("Working . . .");
        get_manage_collection_page(btn, function() {
            var sortable_items = manage_collection_page.find('div#' + choice_string + ' div.itemChoice:not(.inCollection)');
            if (sortable_items.length)
            {
                var i = 0;
                sortable_items.each(function() {
                    if (jQuery(this).find('div.itemChoiceType').text().trim() != "Item") { return; }
                    var childID = jQuery(this).attr('id').replace('choice_' + choice_string + '_', '');
                    i++;
                    btn.text("Adding " + i + " items . . .");
                    jQuery.ajax({
                        type: "POST",
                        url: "https://steamcommunity.com/sharedfiles/addchild",
                        data: {
                            id: collection_id,
                            sessionid: g_sessionID,
                            childid: childID
                        },
                        success: function () {
                            update_choice_item(childID, true);
                        }
                    }).done(function() {
                        i--;
                        btn.text("Adding " + i + " items . . .");
                        if (i == 0)
                        {
                            location.reload();
                        }
                    });
                });
                if (i == 0)
                {
                    stop_working(btn)
                    btn.text(btn_text);
                }
            }
            else
            {
                stop_working(btn)
                btn.text(btn_text);
            }
        });
    }

    var btn_rem_published = document.createElement('a');
    jQuery(btn_rem_published).text('Remove all unowned items');
    jQuery(btn_rem_published).addClass("sectionTab");
    btn_rem_published.style.background = '#FF6666';
    btn_rem_published.style.color = 'black';
    btn_rem_published.style['border-radius'] = '0px';
    btn_rem_published.style.position = 'absolute';
    jQuery(btn_rem_published).click(function() { remove_item(jQuery(this), "MyItems"); });

    var btn_add_published = document.createElement('a');
    jQuery(btn_add_published).text('Add all owned items');
    jQuery(btn_add_published).addClass("sectionTab");
    btn_add_published.style.background = '#90EE90';
    btn_add_published.style.color = 'black';
    btn_add_published.style['border-radius'] = '0px';
    btn_add_published.style.position = 'absolute';
    jQuery(btn_add_published).click(function() { add_item(jQuery(this), "MyItems"); });

    var btn_rem_favorited = document.createElement('a');
    jQuery(btn_rem_favorited).text('Remove all unfavorited items');
    jQuery(btn_rem_favorited).addClass("sectionTab");
    btn_rem_favorited.style.background = '#FF6666';
    btn_rem_favorited.style.color = 'black';
    btn_rem_favorited.style['border-radius'] = '0px';
    btn_rem_favorited.style.position = 'absolute';
    jQuery(btn_rem_favorited).click(function() { remove_item(jQuery(this), "MyFavoriteItems"); });

    var btn_add_favorited = document.createElement('a');
    jQuery(btn_add_favorited).text('Add all favorited items');
    jQuery(btn_add_favorited).addClass("sectionTab");
    btn_add_favorited.style.background = '#90EE90';
    btn_add_favorited.style.color = 'black';
    btn_add_favorited.style['border-radius'] = '0px';
    btn_add_favorited.style.position = 'absolute';
    jQuery(btn_add_favorited).click(function() { add_item(jQuery(this), "MyFavoriteItems"); });

    var btn_rem_subscribed = document.createElement('a');
    jQuery(btn_rem_subscribed).text('Remove all unsubscribed items');
    jQuery(btn_rem_subscribed).addClass("sectionTab");
    btn_rem_subscribed.style.background = '#FF6666';
    btn_rem_subscribed.style.color = 'black';
    btn_rem_subscribed.style['border-radius'] = '0px';
    btn_rem_subscribed.style.position = 'absolute';
    jQuery(btn_rem_favorited).click(function() { remove_item(jQuery(this), "MySubscribedItems"); });

    var btn_add_subscribed = document.createElement('a');
    jQuery(btn_add_subscribed).text('Add all subscribed items');
    jQuery(btn_add_subscribed).addClass("sectionTab");
    btn_add_subscribed.style.background = '#90EE90';
    btn_add_subscribed.style.color = 'black';
    btn_add_subscribed.style['border-radius'] = '0px';
    btn_add_subscribed.style.position = 'absolute';
    jQuery(btn_add_subscribed).click(function() { add_item(jQuery(this), "MySubscribedItems"); });

    var btn_rem_all = document.createElement('a');
    jQuery(btn_rem_all).text('Remove all items');
    jQuery(btn_rem_all).addClass("sectionTab");
    btn_rem_all.style.background = '#FF6666';
    btn_rem_all.style.color = 'black';
    btn_rem_all.style['border-radius'] = '0px';
    btn_rem_all.style.position = 'absolute';
    jQuery(btn_rem_all).click(function() { remove_item(jQuery(this)); });

    if (collection_window != null)
    {
        btn_rem_published.style.top = '415px';
        btn_rem_published.style.right = '5px';
        collection_window.insertBefore(btn_rem_published, collection_window.firstChild);
        btn_add_published.style.top = '445px';
        btn_add_published.style.right = '5px';
        collection_window.insertBefore(btn_add_published, collection_window.firstChild);
        btn_rem_favorited.style.top = '485px';
        btn_rem_favorited.style.right = '5px';
        collection_window.insertBefore(btn_rem_favorited, collection_window.firstChild);
        btn_add_favorited.style.top = '515px';
        btn_add_favorited.style.right = '5px';
        collection_window.insertBefore(btn_add_favorited, collection_window.firstChild);
        btn_rem_subscribed.style.top = '555px';
        btn_rem_subscribed.style.right = '5px';
        collection_window.insertBefore(btn_rem_subscribed, collection_window.firstChild);
        btn_add_subscribed.style.top = '585px';
        btn_add_subscribed.style.right = '5px';
        collection_window.insertBefore(btn_add_subscribed, collection_window.firstChild);
        btn_rem_all.style.top = '625px';
        btn_rem_all.style.right = '5px';
        collection_window.insertBefore(btn_rem_all, collection_window.firstChild);
    }
    else if (right_contents != null)
    {
        btn_rem_published.style.top = '0px';
        btn_rem_published.style.right = '0px';
        right_contents.insertBefore(btn_rem_published, right_contents.firstChild);
        btn_add_published.style.top = '30px';
        btn_add_published.style.right = '0px';
        right_contents.insertBefore(btn_add_published, right_contents.firstChild);
        btn_rem_favorited.style.top = '70px';
        btn_rem_favorited.style.right = '0px';
        right_contents.insertBefore(btn_rem_favorited, right_contents.firstChild);
        btn_add_favorited.style.top = '100px';
        btn_add_favorited.style.right = '0px';
        right_contents.insertBefore(btn_add_favorited, right_contents.firstChild);
        btn_rem_subscribed.style.top = '140px';
        btn_rem_subscribed.style.right = '0px';
        right_contents.insertBefore(btn_rem_subscribed, right_contents.firstChild);
        btn_add_subscribed.style.top = '170px';
        btn_add_subscribed.style.right = '0px';
        right_contents.insertBefore(btn_add_subscribed, right_contents.firstChild);
        btn_rem_all.style.top = '210px';
        btn_rem_all.style.right = '0px';
        right_contents.insertBefore(btn_rem_all, right_contents.firstChild);
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址