Common.Utils

Classes for your scripts

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/389765/1090053/CommonUtils.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Common.Utils
// @description  Classes for your scripts
// @author       Anton Shevchuk
// @license      MIT License
// @version      0.0.7
// @match        *://*/*
// @grant        none
// @namespace    https://greasyfork.org/users/227648
// ==/UserScript==

/**
 * Object with wrapper with getters and setters
 */
class Container {
  constructor() {
    this.container = {};
  }
  /**
   * @param {String[]} keys
   * @param {String}   value
   */
  set(keys, value) {
    this._set(this.container, keys, value);
  }
  _set(elements, keys, value) {
    let key = keys.shift();
    if (typeof elements[key] === 'undefined') {
      elements[key] = {};
    }
    if (keys.length === 0) {
      elements[key] = value;
    } else {
      this._set(elements[key], keys, value);
    }
  }
  /**
   * @param {String} keys
   * @return {null|*}
   */
  get(...keys) {
    if (keys.length === 0) {
      return this.container
    }
    if (this.has(...keys)) {
      return this._get(this.container, ...keys);
    } else {
      return null;
    }
  }
  _get(elements, ...keys) {
    let key = keys.shift();
    if (typeof elements[key] === 'undefined') {
      return null;
    }
    if (keys.length === 0) {
      return elements[key];
    } else {
      return this._get(elements[key], ...keys);
    }
  }
  /**
   * @param {String} keys
   * @return {boolean}
   */
  has(...keys) {
    return this._has(this.container, ...keys);
  }
  _has(elements, ...keys) {
    let key = keys.shift();
    if (typeof elements[key] === 'undefined') {
      return false;
    }
    if (keys.length === 0) {
      return true;
    } else {
      return this._has(elements[key], ...keys);
    }
  }
}

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

/**
 * 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) {
      settings = JSON.parse(settings);
      this.container = Tools.mergeDeep({}, this.default, settings);
    } else {
      this.container = this.default;
    }
  }
  /**
   * With jQuery:
   *   $(window).on('beforeunload', () => SettingsInstance.save() );
   */
  save() {
    localStorage.setItem(this.uid, JSON.stringify(this.container));
  }
}

/**
 * Static functions
 */
class Tools {
  /**
   * Simple object check
   * @param {Object} item
   * @returns {Boolean}
   */
  static isObject(item) {
    return (item && typeof item === 'object' && !Array.isArray(item));
  }

  /**
   * Deep merge objects
   * @param {Object} target
   * @param {Array} sources
   */
  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 (Tools.isObject(source[key])) {
          if (!target[key]) Object.assign(target, { [key]: {} });
          Tools.mergeDeep(target[key], source[key]);
        } else {
          Object.assign(target, { [key]: source[key] });
        }
      }
    }
    return Tools.mergeDeep(target, ...sources);
  }
}