Monkey Utils

Useful library with JavaScript utilities.

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/405813/821710/Monkey%20Utils.js

  1. // ==UserScript==
  2. // @name Utils
  3. // @namespace https://rafaelgssa.gitlab.io/monkey-scripts
  4. // @version 2.2.2
  5. // @author rafaelgssa
  6. // @description Useful library with JavaScript utilities.
  7. // @match *://*/*
  8. // ==/UserScript==
  9.  
  10. /**
  11. * @typedef {'m' | 'h' | 'd' | 'w' | 'M' | 'y'} TimeUnit 'm' for minutes, 'h' for hours, 'd' for days, 'w' for weeks, 'M' for months and 'y' for years
  12. */
  13.  
  14. // eslint-disable-next-line
  15. const Utils = (() => {
  16. const ONE_SECOND_IN_MILLI = 1000;
  17. const ONE_MINUTE_IN_MILLI = ONE_SECOND_IN_MILLI * 60;
  18. const ONE_HOUR_IN_MILLI = ONE_MINUTE_IN_MILLI * 60;
  19. const ONE_DAY_IN_MILLI = ONE_HOUR_IN_MILLI * 24;
  20. const ONE_WEEK_IN_MILLI = ONE_DAY_IN_MILLI * 7;
  21. const ONE_MONTH_IN_MILLI = ONE_DAY_IN_MILLI * 30;
  22.  
  23. /**
  24. * Returns the plural form of a word if a number is different than 1, or the singular form otherwise.
  25. * @param {string | [string, string]} wordOrPair If a word is provided, it is used as the singular form and the plural form is created by adding an 's' to the end of it. If a pair of words is provided, the first word is used as the singular form and the second word is used as the plural form.
  26. * @param {number} n The number to use as reference.
  27. * @param {boolean} doPrepend If true, the number is prepended to the output. Defaults to false.
  28. * @returns {string} The word in the correct form, based on the number.
  29. *
  30. * @example
  31. * Utils.getPlural('parent', 0); // 'parents'
  32. * Utils.getPlural('parent', 1); // 'parent'
  33. * Utils.getPlural('parent', 2); // 'parents'
  34. * Utils.getPlural('parent', 0, true); // '0 parents'
  35. * Utils.getPlural('parent', 1, true); // '1 parent'
  36. * Utils.getPlural('parent', 2, true); // '2 parents'
  37. * Utils.getPlural(['child', 'children'], 0); // 'children'
  38. * Utils.getPlural(['child', 'children'], 1); // 'child'
  39. * Utils.getPlural(['child', 'children'], 2); // 'children'
  40. * Utils.getPlural(['child', 'children'], 0, true); // '0 children'
  41. * Utils.getPlural(['child', 'children'], 1, true); // '1 child'
  42. * Utils.getPlural(['child', 'children'], 2, true); // '2 children'
  43. */
  44. const getPlural = (wordOrPair, n, doPrepend = false) => {
  45. let form;
  46. if (n === 1) {
  47. form = Array.isArray(wordOrPair) ? wordOrPair[0] : wordOrPair;
  48. } else {
  49. form = Array.isArray(wordOrPair) ? wordOrPair[1] : `${wordOrPair}s`;
  50. }
  51. return doPrepend ? `${n} ${form}` : form;
  52. };
  53.  
  54. /**
  55. * Returns a relative time string for a UNIX time.
  56. * @param {number} unix The UNIX time.
  57. * @param {TimeUnit} unit The unit to use. Defaults to 'y'.
  58. * @returns {string} The relative time string.
  59. */
  60. const getRelativeTimeFromUnix = (unix, unit = 'y') => {
  61. const now = Date.now() / 1e3;
  62. const minutes = Math.round(Math.abs(now - unix) / 60);
  63. return getRelativeTimeFromMinutes(minutes, unit);
  64. };
  65.  
  66. /**
  67. * Returns a relative time string for a number of minutes.
  68. * @param {number} minutes The number of minutes.
  69. * @param {TimeUnit} unit The unit to use. Defaults to 'y'.
  70. * @returns {string} The relative time string.
  71. *
  72. * @example
  73. * Utils.getRelativeTimeFromMinutes(30); // '30 minutes'
  74. * Utils.getRelativeTimeFromMinutes(60); // 'about 1 hour'
  75. * Utils.getRelativeTimeFromMinutes(60, 'm'); // '60 minutes'
  76. * Utils.getRelativeTimeFromMinutes(60 * 24); // 'about 1 day'
  77. * Utils.getRelativeTimeFromMinutes(60 * 24, 'h'); // 'about 24 hours'
  78. * Utils.getRelativeTimeFromMinutes(60 * 24 * 7); // 'about 1 week'
  79. * Utils.getRelativeTimeFromMinutes(60 * 24 * 7, 'd'); // 'about 7 days'
  80. * Utils.getRelativeTimeFromMinutes(60 * 24 * 30); // 'about 1 month'
  81. * Utils.getRelativeTimeFromMinutes(60 * 24 * 30, 'w'); // 'about 4 weeks'
  82. * Utils.getRelativeTimeFromMinutes(60 * 24 * 30 * 12); // 'about 1 year'
  83. * Utils.getRelativeTimeFromMinutes(60 * 24 * 30 * 12, 'M'); // 'about 12 months'
  84. */
  85. const getRelativeTimeFromMinutes = (minutes, unit = 'y') => {
  86. if (minutes < 60 || unit === 'm') {
  87. return getPlural('minute', minutes, true);
  88. }
  89. const hours = Math.round(minutes / 60);
  90. if (hours < 24 || unit === 'h') {
  91. return `about ${getPlural('hour', hours, true)}`;
  92. }
  93. const days = Math.round(hours / 24);
  94. if (days < 7 || unit === 'd') {
  95. return `about ${getPlural('day', days, true)}`;
  96. }
  97. const weeks = Math.round(days / 7);
  98. if (weeks < 4 || unit === 'w') {
  99. return `about ${getPlural('week', weeks, true)}`;
  100. }
  101. const months = Math.round(days / 30);
  102. if (months < 12 || unit === 'M') {
  103. return `about ${getPlural('month', months, true)}`;
  104. }
  105. const years = Math.round(months / 12);
  106. return `about ${getPlural('year', years, true)}`;
  107. };
  108.  
  109. /**
  110. * Returns a date string in the 'yyyy-MM-dd hh:mm:ss UTC' format.
  111. * @param {Date} date The date to format.
  112. * @returns {string} The formatted date string.
  113. */
  114. const getUtcString = (date) => {
  115. const isoString = date.toISOString(); // yyyy-MM-ddThh:mm:ss.sssZ
  116. return `${isoString.replace('T', ' ').slice(0, -5)} UTC`;
  117. };
  118.  
  119. /**
  120. * Checks if a value is set.
  121. * @template T
  122. * @param {T} value The value to check.
  123. * @returns {value is NonNullable<T>} Whether the value is set or not.
  124. */
  125. const isSet = (value) => {
  126. return typeof value !== 'undefined' && value !== null;
  127. };
  128.  
  129. /**
  130. * Sleeps for a number of seconds.
  131. * @param {number} seconds How many seconds to sleep for.
  132. * @returns {Promise<void>}
  133. */
  134. const sleep = (seconds) => {
  135. return new Promise((resolve) => window.setTimeout(resolve, seconds * ONE_SECOND_IN_MILLI));
  136. };
  137.  
  138. return {
  139. ONE_SECOND_IN_MILLI,
  140. ONE_MINUTE_IN_MILLI,
  141. ONE_HOUR_IN_MILLI,
  142. ONE_DAY_IN_MILLI,
  143. ONE_WEEK_IN_MILLI,
  144. ONE_MONTH_IN_MILLI,
  145. getPlural,
  146. getRelativeTimeFromUnix,
  147. getRelativeTimeFromMinutes,
  148. getUtcString,
  149. isSet,
  150. sleep,
  151. };
  152. })();

QingJ © 2025

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