Brazen Subscriptions Loader

Helper class for loading account subscriptions

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/424516/1114774/Brazen%20Subscriptions%20Loader.js

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Brazen Subscriptions Loader
// @namespace    brazenvoid
// @version      1.0.1
// @author       brazenvoid
// @license      GPL-3.0-only
// @description  Helper class for loading account subscriptions
// ==/UserScript==

class BrazenSubscriptionsLoader
{
    /**
     * @callback SubscriptionLoaderGetPageCountCallback
     * @param {JQuery} page
     * @return {number}
     */

    /**
     * @callback SubscriptionLoaderGetPageUrlCallback
     * @param {string} baseUrl
     * @param {number} pageNo
     * @return {string}
     */

    /**
     * @callback SubscriptionLoaderProgressReportingCallback
     * @param {string} status
     */

    /**
     * @callback SubscriptionLoaderSubscriptionsGatheredCallback
     * @param {string[]} subscriptions
     */

    /**
     * @typedef {{url: string, getPageCount: SubscriptionLoaderGetPageCountCallback, getPageUrl: SubscriptionLoaderGetPageUrlCallback,
     *            subscriptionNameSelector: string, subscriptionsCountSelector: string}} SubscriptionLoaderConfig
     */

    /**
     * @param {SubscriptionLoaderProgressReportingCallback} onProgressUpdate
     * @param {SubscriptionLoaderSubscriptionsGatheredCallback} onSubscriptionsGathered
     */
    constructor (onProgressUpdate, onSubscriptionsGathered)
    {
        /**
         * @type {number}
         * @private
         */
        this._configIterator = 0

        /**
         * @type {SubscriptionLoaderConfig[]}
         * @private
         */
        this._configs = []

        /**
         * @type {SubscriptionLoaderProgressReportingCallback}
         * @private
         */
        this._onProgressUpdate = onProgressUpdate

        /**
         * @type {SubscriptionLoaderSubscriptionsGatheredCallback}
         * @private
         */
        this._onSubscriptionsGathered = onSubscriptionsGathered

        /**
         * @type {JQuery}
         * @private
         */
        this._sandbox = null

        /**
         * @type {SubscriptionLoaderConfig}
         * @private
         */
        this._selectedConfig = {}

        /**
         * @type {*[]}
         * @private
         */
        this._subscriptions = []
    }

    /**
     * Loads and records subscriptions by page
     * @param {number} maxPage
     * @param {number} pageNo
     * @private
     */
    _loadSubscriptionsPage (pageNo, maxPage)
    {
        this._sandbox.empty().load(this._selectedConfig.getPageUrl(this._selectedConfig.url, pageNo), () => {
            this._sandbox.find(this._selectedConfig.subscriptionNameSelector).each((index, element) => {
                this._subscriptions.push($(element).text().trim())
            })
            this._onProgressUpdate('Gathering subscriptions - ' + Math.ceil(((pageNo / maxPage) * 100)) + '% Complete')
            pageNo++
            if (pageNo <= maxPage) {
                this._loadSubscriptionsPage(pageNo, maxPage)
            } else {
                this._configIterator++
                if (this._configIterator < this._configs.length) {
                    this._processSubscriptions()
                } else {
                    this._sandbox.remove()
                    this._onProgressUpdate('Loaded ' + this._subscriptions.length + ' subscriptions.')
                    this._onSubscriptionsGathered(this._subscriptions)
                    this._configIterator = 0
                    this._subscriptions = []
                }
            }
        })
    }

    /**
     * @private
     */
    _processSubscriptions ()
    {
        this._selectedConfig = this._configs[this._configIterator]
        this._sandbox.empty().load(this._selectedConfig.url + ' ' + this._selectedConfig.subscriptionsCountSelector, () => {
            this._loadSubscriptionsPage(1, Math.ceil(this._selectedConfig.getPageCount(this._sandbox)))
        })
    }

    /**
     * @param {SubscriptionLoaderConfig} config
     * @returns {BrazenSubscriptionsLoader}
     */
    addConfig (config)
    {
        this._configs.push(config)
        return this
    }

    run ()
    {
        this._onProgressUpdate('Gathering subscriptions - 0% Complete')
        this._sandbox = $('<div id="brazen-subscriptions-loader-sandbox" hidden/>').appendTo('body')
        this._processSubscriptions()
    }
}