Greasy Fork 支持简体中文。

CommonUtils

通用工具。

目前為 2020-03-16 提交的版本,檢視 最新版本

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

  1. // ==UserScript==
  2. // @name CommonUtils
  3. // @name:zh CommonUtils
  4. // @name:zh-CN CommonUtils
  5. // @name:en CommonUtils
  6. // @description 通用工具。
  7. // @description:zh 通用工具。
  8. // @description:zh-CN 通用工具。
  9. // @description:en Common Utils.
  10. // @namespace https://greasyfork.org/zh-CN/users/331591
  11. // @version 1.0.0
  12. // @author Hale Shaw
  13. // @homepage https://greasyfork.org/zh-CN/scripts/398009
  14. // @supportURL https://greasyfork.org/zh-CN/scripts/398009/feedback
  15. // @icon https://greasyfork.org/assets/blacklogo16-bc64b9f7afdc9be4cbfa58bdd5fc2e5c098ad4bca3ad513a27b15602083fd5bc.png
  16. // @match https://greasyfork.org/*
  17. // @license AGPL-3.0-or-later
  18. // @compatible Chrome
  19. // @run-at document-idle
  20. // @grant none
  21. // ==/UserScript==
  22.  
  23. /**
  24. * check whether the element is valid by id.
  25. * @param {String} id element id.
  26. */
  27. function isValidById(id) {
  28. if (document.getElementById(id)) {
  29. return true;
  30. } else {
  31. return false;
  32. }
  33. }
  34.  
  35. /**
  36. * check whether the element is valid by class name.
  37. * @param {String} className element class name.
  38. */
  39. function isValidByClassName(className) {
  40. if (document.getElementsByClassName(className) && document.getElementsByClassName(className)[0] !== undefined) {
  41. return true;
  42. } else {
  43. return false;
  44. }
  45. }
  46.  
  47. /**
  48. * 将Date转化为指定格式的String.
  49. * 年(y)可以用 1-4 个占位符,
  50. * 月(M)、日(d)、小时(H)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
  51. * 毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  52. * 例子:
  53. * format("yyyy-MM-dd hh:mm:ss.S", new Date()) ==> 2006-07-02 08:09:04.423
  54. * format("yyyy-M-d h:m:s.S", new Date()) ==> 2006-7-2 8:9:4.182
  55. * @param {String} fmt fotmat 格式字符串.
  56. * @param {Date} date Date object.
  57. */
  58. function format (fmt, date) {
  59. var ret;
  60. var opt = {
  61. "y+": date.getFullYear().toString(),
  62. "M+": (date.getMonth() + 1).toString(),
  63. "d+": date.getDate().toString(),
  64. "H+": date.getHours().toString(),
  65. "m+": date.getMinutes().toString(),
  66. "s+": date.getSeconds().toString(), //秒
  67. "q+": (Math.floor((date.getMonth() + 3) / 3)).toString(), //季度
  68. "S": date.getMilliseconds().toString() //毫秒
  69. // 有其他格式化字符需求可以继续添加,必须转化成字符串
  70. };
  71. for (var k in opt) {
  72. ret = new RegExp("(" + k + ")").exec(fmt);
  73. if (ret) {
  74. fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
  75. }
  76. }
  77. return fmt;
  78. }