CommonUtils

通用工具。

2020-03-16 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

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/398010/781187/CommonUtils.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         CommonUtils
// @name:zh      CommonUtils
// @name:zh-CN   CommonUtils
// @name:en      CommonUtils
// @description  通用工具。
// @description:zh  通用工具。
// @description:zh-CN  通用工具。
// @description:en  Common Utils.
// @namespace    https://greasyfork.org/zh-CN/users/331591
// @version      1.0.0
// @author       Hale Shaw
// @homepage     https://greasyfork.org/zh-CN/scripts/398009
// @supportURL   https://greasyfork.org/zh-CN/scripts/398009/feedback
// @icon         https://greasyfork.org/assets/blacklogo16-bc64b9f7afdc9be4cbfa58bdd5fc2e5c098ad4bca3ad513a27b15602083fd5bc.png
// @match        https://greasyfork.org/*
// @license      AGPL-3.0-or-later
// @compatible	 Chrome
// @run-at       document-idle
// @grant        none
// ==/UserScript==

/**
 * check whether the element is valid by id.
 * @param {String} id element id.
 */
function isValidById(id) {
  if (document.getElementById(id)) {
    return true;
  } else {
    return false;
  }
}

/**
 * check whether the element is valid by class name.
 * @param {String} className element class name.
 */
function isValidByClassName(className) {
  if (document.getElementsByClassName(className) && document.getElementsByClassName(className)[0] !== undefined) {
    return true;
  } else {
    return false;
  }
}

/**
 * 将Date转化为指定格式的String.
 * 年(y)可以用 1-4 个占位符,
 * 月(M)、日(d)、小时(H)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
 * 毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
 * 例子:
 * format("yyyy-MM-dd hh:mm:ss.S", new Date()) ==> 2006-07-02 08:09:04.423
 * format("yyyy-M-d h:m:s.S", new Date())      ==> 2006-7-2 8:9:4.182
 * @param {String} fmt fotmat 格式字符串.
 * @param {Date} date Date object.
 */
function format (fmt, date) {
  var ret;
  var opt = {
    "y+": date.getFullYear().toString(),
    "M+": (date.getMonth() + 1).toString(),
    "d+": date.getDate().toString(),
    "H+": date.getHours().toString(),
    "m+": date.getMinutes().toString(),
    "s+": date.getSeconds().toString(), //秒
    "q+": (Math.floor((date.getMonth() + 3) / 3)).toString(), //季度
    "S": date.getMilliseconds().toString() //毫秒
    // 有其他格式化字符需求可以继续添加,必须转化成字符串
  };
  for (var k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
    }
  }
  return fmt;
}