coofoUtils

一些工具

目前为 2022-03-24 提交的版本,查看 最新版本

此脚本不应直接安装,它是供其他脚本使用的外部库。如果你需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/442002/1031848/coofoUtils.js

  1. // ==UserScript==
  2. // @name coofoUtils
  3. // @namespace https://github.com/coofo/someScript
  4. // @version 0.0.3
  5. // @license MIT License
  6. // @description 一些工具
  7. // @author coofo
  8. // @downloadURL https://github.com/coofo/someScript/raw/main/tampermonkey/coofoUtils.user.js
  9. // @supportURL https://github.com/coofo/someScript/issues
  10. // @grant GM_download
  11. // @grant GM_xmlhttpRequest
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16. window.coofoUtils = {
  17. commonUtils: {
  18. format: {
  19. num: {
  20. fullNum: function (num, length) {
  21. return (Array(length).join('0') + num).slice(-length);
  22. },
  23. toThousands: function (value, seperator, digitNum) {
  24. if ((value = ((value = value + "").replace(/^\s*|\s*$|,*/g, ''))).match(/^\d*\.?\d*$/) == null)
  25. return value;
  26. value = digitNum >= 0 ? (Number(value).toFixed(digitNum) + "") : value;
  27. let r = [],
  28. tl = value.split(".")[0],
  29. tr = value.split(".")[1];
  30. tr = typeof tr !== "undefined" ? tr : "";
  31. if (seperator != null && seperator !== "") {
  32. while (tl.length >= 3) {
  33. r.push(tl.substring(tl.length - 3));
  34. tl = tl.substring(0, tl.length - 3);
  35. }
  36. if (tl.length > 0)
  37. r.push(tl);
  38. r.reverse();
  39. r = r.join(seperator);
  40. return tr === "" ? r : r + "." + tr;
  41. }
  42. return value;
  43. }
  44. },
  45. file: {
  46. getSuffix: function (name) {
  47. let index = name.lastIndexOf('.');
  48. if (index < 0) {
  49. return "";
  50. } else {
  51. return name.substring(index + 1);
  52. }
  53. }
  54. },
  55. string: {
  56. byMap: function (str, map, preprocessing) {
  57. let reg = new RegExp('\\${([a-z][a-zA-Z0-9_.]+)}', 'g');
  58. return str.replace(reg, function (match, pos, originalText) {
  59. let key = match.replace(reg, '$1');
  60. let value = map[key];
  61. if (value === null || value === undefined) {
  62. value = match;
  63. }
  64. if (typeof preprocessing === "function") {
  65. value = preprocessing(value);
  66. }
  67. return value;
  68. });
  69. }
  70. },
  71. url: {
  72. fullUrl: function (url) {
  73. if (url.match(/^[a-zA-Z0-9]+:\/\//) !== null) {
  74. return url;
  75. } else if (url.match(/^\/\/[a-zA-Z0-9]+/) !== null) {
  76. return window.location.protocol + url;
  77. } else if (url.match(/^\/[a-zA-Z0-9]+/) !== null) {
  78. return window.location.origin + url;
  79. } else {
  80. return url;
  81. }
  82. }
  83. }
  84. },
  85. assert: {
  86. isTrue: function (value, message) {
  87. if (true !== value) {
  88. console.error(message);
  89. console.error(value);
  90. throw message;
  91. }
  92. },
  93. isNull: function (value, message) {
  94. if (value !== null) {
  95. console.error(message);
  96. console.error(value);
  97. throw message;
  98. }
  99. },
  100. notNull: function (value, message) {
  101. if (value === null) {
  102. console.error(message);
  103. console.error(value);
  104. throw message;
  105. }
  106. },
  107. hasLength: function (value, message) {
  108. if (!(value !== null && value.length > 0)) {
  109. console.error(message);
  110. console.error(value);
  111. throw message;
  112. }
  113. },
  114. },
  115. downloadHelp: {
  116. toBlob: {},
  117. toUser: {
  118. asTagA4Url: function (url, fileName) {
  119. let aLink = document.createElement('a');
  120. if (fileName) {
  121. aLink.download = fileName;
  122. } else {
  123. aLink.download = url.substring(url.lastIndexOf('/') + 1);
  124. }
  125. aLink.className = 'download-temp-node';
  126. aLink.target = "_blank";
  127. aLink.style = "display:none;";
  128. aLink.href = url;
  129. document.body.appendChild(aLink);
  130. if (document.all) {
  131. aLink.click(); //IE
  132. } else {
  133. let evt = document.createEvent("MouseEvents");
  134. evt.initEvent("click", true, true);
  135. aLink.dispatchEvent(evt); // 其它浏览器
  136. }
  137. document.body.removeChild(aLink);
  138. },
  139. asTagA4Blob: function (content, fileName) {
  140. if ('msSaveOrOpenBlob' in navigator) {
  141. navigator.msSaveOrOpenBlob(content, fileName);
  142. } else {
  143. let aLink = document.createElement('a');
  144. aLink.className = 'download-temp-node';
  145. aLink.download = fileName;
  146. aLink.style = "display:none;";
  147. let blob = new Blob([content], {type: content.type});
  148. aLink.href = window.URL.createObjectURL(blob);
  149. document.body.appendChild(aLink);
  150. if (document.all) {
  151. aLink.click(); //IE
  152. } else {
  153. let evt = document.createEvent("MouseEvents");
  154. evt.initEvent("click", true, true);
  155. aLink.dispatchEvent(evt); // 其它浏览器
  156. }
  157. window.URL.revokeObjectURL(aLink.href);
  158. document.body.removeChild(aLink);
  159. }
  160. }
  161. }
  162. }
  163. },
  164. tampermonkeyUtils: {
  165. downloadHelp: {
  166. toBlob: {
  167. asBlob: function (url, onSuccess) {
  168. GM_xmlhttpRequest({
  169. method: "GET",
  170. url: url,
  171. responseType: "arraybuffer",
  172. onload: function (responseDetails) {
  173. onSuccess(responseDetails);
  174. }
  175. });
  176. }
  177. },
  178. toUser: {
  179. asGMdownload: function (url, fileName, setting) {
  180. let details;
  181. if (typeof setting === "object" && typeof setting.gmDownload === "object") {
  182. details = setting.gmDownload;
  183. } else {
  184. details = {saveAs: false};
  185. }
  186. details.url = url;
  187. details.name = fileName;
  188. // console.log(details.url);
  189. // console.log(details.name);
  190. GM_download(details);
  191. }
  192. }
  193. }
  194. },
  195. service: {
  196. task: {
  197. create: function (callBack) {
  198. let task = {
  199. runtime: {taskList: [], callBack: callBack},
  200. api: {
  201. getRuntime: function () {
  202. return this.runtime;
  203. },
  204. addTask: function (exec, taskInfo, lastRetryTimes) {
  205. let taskItem = {
  206. taskInfo: taskInfo,
  207. handler: null,
  208. complete: false,
  209. lastFinishTime: 0,
  210. lastRetryTimes: lastRetryTimes + 1,
  211. exec: function (onTaskFinish) {
  212. this.onTaskFinish = onTaskFinish;
  213. exec(this.taskInfo, this);
  214. },
  215. success: function () {
  216. this.handler = null;
  217. this.complete = true;
  218. this.lastFinishTime = Date.now();
  219. this.onTaskFinish();
  220.  
  221. },
  222. failed: function () {
  223. this.handler = null;
  224. this.lastRetryTimes--;
  225. this.lastFinishTime = Date.now();
  226. this.onTaskFinish();
  227. },
  228. onTaskFinish: null
  229. };
  230. task.runtime.taskList.push(taskItem);
  231. },
  232. exec: function (handler) {
  233. let taskList = task.runtime.taskList;
  234. //判断该执行器是否有未完任务,并指定为失败
  235. for (let i = 0; i < taskList.length; i++) {
  236. let taskItem = taskList[i];
  237. if (taskItem.handler === handler) {
  238. taskItem.failed();
  239. }
  240. }
  241.  
  242. //寻找新任务并标记返回
  243. let allFinished = true;
  244. for (let i = 0; i < taskList.length; i++) {
  245. let taskItem = taskList[i];
  246.  
  247. if (taskItem.complete === false && taskItem.lastRetryTimes > 0) {
  248. if (taskItem.handler == null) {
  249. taskItem.handler = handler;
  250. taskItem.exec(function () {
  251. setTimeout(function () {
  252. task.api.exec(handler);
  253. }, 1);
  254. });
  255. return;
  256. } else {
  257. allFinished = false;
  258. }
  259. }
  260. }
  261. if (allFinished) task.runtime.callBack();
  262. },
  263.  
  264. }
  265. };
  266. return task;
  267. }
  268. }
  269. }
  270. };
  271. })();