CommonUtils

通用工具

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/398010/781197/CommonUtils.js

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址