Common.Utils

Classes for your scripts

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/389765/1785927/CommonUtils.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         Common.Utils
// @description  Classes for your scripts
// @author       Anton Shevchuk
// @license      MIT License
// @version      0.1.0
// @match        *://*/*
// @grant        none
// @namespace    https://greasyfork.org/users/227648
// ==/UserScript==

(function () {
    'use strict';

    /**
     * Object with a wrapper with getters and setters
     */
    class Container {
        constructor() {
            this.container = {};
        }
        set(keys, value) {
            let target = this.container;
            for (let i = 0; i < keys.length - 1; i++) {
                if (typeof target[keys[i]] === 'undefined')
                    target[keys[i]] = {};
                target = target[keys[i]];
            }
            target[keys[keys.length - 1]] = value;
        }
        get(...keys) {
            if (keys.length === 0)
                return this.container;
            let target = this.container;
            for (let i = 0; i < keys.length; i++) {
                if (typeof target[keys[i]] === 'undefined')
                    return null;
                target = target[keys[i]];
            }
            return target;
        }
        has(...keys) {
            let target = this.container;
            for (let i = 0; i < keys.length; i++) {
                if (typeof target[keys[i]] === 'undefined')
                    return false;
                target = target[keys[i]];
            }
            return true;
        }
    }

    /**
     * Simple cache object with getters and setters
     */
    class SimpleCache extends Container {
        set(key, value) {
            super.set([key], value);
        }
    }

    class Tools {
        /**
         * Simple object check
         */
        static isObject(item) {
            return (item && typeof item === 'object' && !Array.isArray(item));
        }
        /**
         * Deep merge objects
         */
        static mergeDeep(target, ...sources) {
            if (!sources.length)
                return target;
            const source = sources.shift();
            if (Tools.isObject(target) && Tools.isObject(source)) {
                for (const key in source) {
                    if (!source.hasOwnProperty(key))
                        continue;
                    if (Tools.isObject(source[key])) {
                        if (!Tools.isObject(target[key]))
                            Object.assign(target, { [key]: {} });
                        Tools.mergeDeep(target[key], source[key]);
                    }
                    else {
                        Object.assign(target, { [key]: source[key] });
                    }
                }
            }
            return Tools.mergeDeep(target, ...sources);
        }
    }

    /**
     * Settings object with localStorage as storage
     */
    class Settings extends Container {
        constructor(uid, def = {}) {
            super();
            this.uid = uid;
            this.default = def;
            this.load();
        }
        load() {
            let settings = localStorage.getItem(this.uid);
            if (settings) {
                let parsed = JSON.parse(settings);
                this.container = Tools.mergeDeep({}, this.default, parsed);
            }
            else {
                this.container = Tools.mergeDeep({}, this.default);
            }
        }
        /**
         * With jQuery:
         *   $(window).on('beforeunload', () => SettingsInstance.save() );
         */
        save() {
            localStorage.setItem(this.uid, JSON.stringify(this.container));
        }
    }

    // Expose as globals (consumed via @require by other scripts)
    Object.assign(window, { Container, SimpleCache, Settings, Tools });

})();