Reset Steam Workshop Subscriptions

Unsubscribe to all Steam Workshop Addons with the ease of a single click.

当前为 2015-05-10 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Reset Steam Workshop Subscriptions
// @namespace    https://timmy.ws
// @version      1.0.0
// @description  Unsubscribe to all Steam Workshop Addons with the ease of a single click.
// @author       Timmy
// @match        http://steamcommunity.com/id/*/myworkshopfiles/*mysubscriptions*
// @match        https://steamcommunity.com/id/*/myworkshopfiles/*mysubscriptions*
// @grant        GM_addStyle
// @run-at       document-end
// ==/UserScript==

/*jslint browser: true*/

(function () {
    'use strict';

    var addons = [], addonTotal, pageTotal, sessionId;

    // Returns the number of addons the user is subscribed to
    function getAddonTotal() {
        var regex, workshopInfo;
        regex = /([0-9]+)\ entries/g;
        workshopInfo = document.getElementsByClassName('workshopBrowsePagingInfo')[0].innerHTML;
        return parseInt(workshopInfo.match(regex)[0], 10);
    }

    // Return the number of pages we have to analyse
    function getPageTotal(addonTotal) {
        return Math.ceil(addonTotal / 30);
    }

    // Returns true if we are on a secured page
    function usingSSL() {
        return (document.location.protocol === 'https:') ? true : false;
    }

    // Returns the users' profile id
    function getProfileId() {
        return document.URL.match(/[0-9]+/)[0];
    }

    // Returns the users' session id
    function getSessionId() {
        return document.getElementById('PublishedFileUnsubscribe').getElementsByTagName('input')[2].value;
    }

    // Display feedback to user
    function feedback(id, message) {
        var element = document.getElementById(id);

        if (!element) {
            element = document.createElement('div');
            element.id = id;
            document.getElementById('unsub-progress').appendChild(element);
        } else if (element.firstChild) {
            while (element.firstChild) {
                element.removeChild(element.firstChild);
            }
        }

        element.appendChild(document.createTextNode(message));
    }

    // Unsubscribe from all addons
    function processAllAddons(xmlhttp) {
        var i, handleStateChange, url;

        if (addons.length < addonTotal) {
            return false;
        }

        feedback('ANALYSE_PROG', 'Analysing addon subscriptions... Done!');

        handleStateChange = function (index) {
            return function () {
                if (xmlhttp[index].readyState === 4) {
                    feedback('UNSUB_PROGRESS', 'Unsubscribing from addons... (' + Math.round(index / addonTotal * 100) + '%)');

                    if (index >= addonTotal - 1) {
                        feedback('UNSUB_PROGRESS', 'Unsubscribing from addons... Done!');
                        feedback('END', 'Reloading page...');
                        setTimeout(function () {
                            window.location.reload();
                        }, 1000);
                    }
                }
            };
        };

        xmlhttp = [];

        url = (usingSSL()) ? 'https' : 'http';
        url += '://steamcommunity.com/sharedfiles/unsubscribe';

        for (i = 0; i < addons.length; i = i + 1) {
            xmlhttp[i] = new XMLHttpRequest();
            xmlhttp[i].open('POST', url);
            xmlhttp[i].setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            xmlhttp[i].onreadystatechange = handleStateChange(i);
            xmlhttp[i].send('id=' + addons[i][0] + '&appid=' + addons[i][1] + '&sessionid=' + sessionId);
        }
    }

    // Get subscribed addons for each page and add them to the addons array
    function processSinglePage(xmlhttp, index) {
        if (xmlhttp[index].status === 200) {
            var container, unsubscribeItem, i, itemDetails;

            container = document.implementation.createHTMLDocument().documentElement;
            container.innerHTML = xmlhttp[index].responseText;
            unsubscribeItem = container.querySelectorAll('a[id^="UnsubscribeItemBtn"]');

            for (i = 0; i < unsubscribeItem.length; i = i + 1) {
                itemDetails = unsubscribeItem[i].href.match(/[0-9]+/g); // Extract WorkshopID and AppID
                addons.push([itemDetails[0], itemDetails[1]]);
            }

            processAllAddons(xmlhttp);
        }
    }

    function getAddons() {
        var url, xmlhttp, handleStateChange, i;

        url = (usingSSL()) ? 'https' : 'http';
        url += '://steamcommunity.com/id/' + getProfileId() + '/myworkshopfiles/?browsefilter=mysubscriptions&numperpage=30&p=';
        xmlhttp = [];

        handleStateChange = function (index) {
            return function () {
                if (xmlhttp[index].readyState === 4) {
                    feedback('ANALYSE_PROG', 'Analysing addon subscriptions... (' + index + '/' + pageTotal + ')');
                    processSinglePage(xmlhttp, index);
                }
            };
        };

        for (i = 1; i <= pageTotal; i = i + 1) {
            xmlhttp[i] = new XMLHttpRequest();
            xmlhttp[i].onreadystatechange = handleStateChange(i);
            xmlhttp[i].open('GET', url + i);
            xmlhttp[i].send();
        }
    }

    // Injects our element in the DOM
    function injectUnsubscriber() {
        if (document.getElementById('no_items')) {
            return false;
        }

        addonTotal = getAddonTotal();
        pageTotal = getPageTotal(addonTotal);
        sessionId = getSessionId();

        var reference, parent, wrap, title, button, clear, leftContents, progress;

        reference = document.getElementById('tabs_basebg_workshop');
        parent = reference.parentNode;

        wrap = document.createElement('div');
        wrap.id = 'unsubscriber';
        parent.insertBefore(wrap, reference);

        title = document.createElement('div');
        title.className = 'title';
        title.appendChild(document.createTextNode('Reset Steam Workshop'));
        wrap.appendChild(title);

        button = document.createElement('div');
        button.className = 'button';
        button.innerHTML = 'Unsubscribe All';
        button.addEventListener('click', function () {
            button.style.pointerEvents = 'none';

            leftContents = document.getElementById('leftContents');
            while (leftContents.firstChild) {
                leftContents.removeChild(leftContents.firstChild);
            }

            progress = document.createElement('div');
            progress.id = 'unsub-progress';
            progress.className = 'generic-block';
            leftContents.appendChild(progress);

            feedback('START', 'About to reset your Workshop subscriptions! Close this page RIGHT NOW if you don\'t want to continue...');
            
            setTimeout(getAddons, 3000);
        });
        wrap.appendChild(button);

        clear = document.createElement('div');
        clear.className = 'clear';
        wrap.appendChild(clear);

        GM_addStyle('#unsubscriber {font-size: 13px; width: 100%; margin-top: 12px; background: rgba(0, 0, 0, 0.4); color: rgb(143, 152, 160); padding: 10px; box-sizing: border-box;}');
        GM_addStyle('#unsubscriber .title {color: #67c1f5; font-size: 16px; font-family: \'Motiva Sans Light\', Arial, Tahoma; float: left;}');
        GM_addStyle('#unsubscriber .button {color: rgba(255, 255, 255, 0.8);background: rgba(231, 76, 60, 0.4); font-size: 13px; padding: 3px 9px; float: right; border-radius: 2px; cursor: pointer;}');
        GM_addStyle('#unsubscriber .button:hover {color: #fff; background: rgba(231, 76, 60, 0.75);}');
        GM_addStyle('#unsubscriber .clear {clear: both;}');
        GM_addStyle('.generic-block {margin-top: 10px; padding: 10px; color: rgb(143, 152, 160); background: rgba(0, 0, 0, 0.4); width: 100%; font-size: 13px; box-sizing: border-box; line-height: 1.5em;}');
        GM_addStyle('.disabled {pointer-events: none; background: rgba(0, 0, 0, 0.7);}');
    }

    injectUnsubscriber();
}());