WhiteSevsUtils

一个好用的工具类

目前为 2023-01-04 提交的版本,查看 最新版本

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

  1. /*
  2. * @overview 自己常用的工具类定义
  3. * @copyright GPL-3.0-only
  4. * @author WhiteSevs
  5. * @version 0.3
  6. */
  7. let Utils = {};
  8.  
  9. /*
  10. * @param {string|function} func - 需要捕获错误的函数或函数格式的字符串
  11. * @param {object} params - 该函数的参数和捕获到错误的函数的参数,类型为数组Array
  12. * @param {string|function} errorFunc - 捕获到错误后执行的函数或函数格式的字符串
  13. * @example Utils.tryCatch("(pam)=>{console.log('this is a function and params is' + pam[0])}",["参数1"],"()=>{console.log('对错误进行处理判断')}");
  14. */
  15. Utils.tryCatch = function (func, params, errorFunc) {
  16. /* 捕获错误 */
  17. if (func == null) {
  18. throw "警告: 参数 func 为不存在";
  19. }
  20. if (typeof func !== "function" && typeof func !== "string") {
  21. throw "参数 func 类型必须为function类型或者string类型";
  22. }
  23. if (
  24. params != null &&
  25. typeof params !== "object" &&
  26. typeof params !== "string"
  27. ) {
  28. throw "参数 params 类型必须为object类型或者string类型";
  29. }
  30. if (
  31. errorFunc != null &&
  32. typeof errorFunc !== "object" &&
  33. typeof errorFunc !== "string"
  34. ) {
  35. throw "参数 errorFunc 类型必须为function类型或者string类型";
  36. }
  37. var result = null;
  38. try {
  39. result = typeof func === "string" ? window.eval(func) : func(params);
  40. } catch (error) {
  41. console.log(
  42. "%c" + (func.name ? func.name : func + "出现错误"),
  43. "color: #f20000"
  44. );
  45. console.log("%c" + ("错误原因:" + error), "color: #f20000");
  46. console.trace(func);
  47. result =
  48. typeof func === "string" ? window.eval(errorFunc) : errorFunc(params);
  49. } finally {
  50. return result;
  51. }
  52. };
  53.  
  54. /*
  55. * @description 格式化时间字符串
  56. * @param {string} str - 字符串格式的时间,例如:2022-11-21 00:00:00,或者是 00:00:00
  57. * @return {number} - 返回时间戳
  58. * @example Utils.formatTextToTimeStamp("2022-11-21 00:00:00");
  59. */
  60. Utils.formatTextToTimeStamp = function (text) {
  61. /* 把字符串格式的时间(完整,包括日期和时间)格式化成时间戳 */
  62. if (typeof text !== "string") {
  63. throw "参数 text 必须为string类型";
  64. }
  65. if (text.length === 8) {
  66. /* 参数只有时间 */
  67. var today = new Date();
  68. text =
  69. today.getFullYear() +
  70. "-" +
  71. (today.getMonth() + 1) +
  72. "-" +
  73. today.getDate() +
  74. " " +
  75. text;
  76. }
  77. text = text.substring(0, 19);
  78. text = text.replace(/-/g, "/");
  79. var timestamp = new Date(text).getTime();
  80. return timestamp;
  81. };
  82.  
  83. /*
  84. * @description 定位网页中字符串位置并标亮,注意,该字符串必须是能在网页中看得到的,隐藏的是无法定位的
  85. * @param {string} str - 需要寻找的字符串
  86. * @param {boolean} caseSensitive - 区分大小写
  87. * @return {boolean} true/false - 找到为true,否则false
  88. * @example Utils.findWindowPageString("xxxxx");
  89. * @exampleResult true
  90. */
  91. Utils.findWindowPageString = function (str, caseSensitive = false) {
  92. var TRange = null;
  93. var strFound;
  94. if (window.find) {
  95. /* CODE FOR BROWSERS THAT SUPPORT window.find */
  96. strFound = self.find(str, caseSensitive, true, true, false);
  97. if (strFound && self.getSelection && !self.getSelection().anchorNode) {
  98. strFound = self.find(str, caseSensitive, true, true, false);
  99. }
  100. if (!strFound) {
  101. strFound = self.find(str, 0, 1);
  102. while (self.find(str, 0, 1)) continue;
  103. }
  104. } else if (navigator.appName.indexOf("Microsoft") != -1) {
  105. /* EXPLORER-SPECIFIC CODE */
  106. if (TRange != null) {
  107. TRange.collapse(false);
  108. strFound = TRange.findText(str);
  109. if (strFound) TRange.select();
  110. }
  111. if (TRange == null || strFound == 0) {
  112. TRange = self.document.body.createTextRange();
  113. strFound = TRange.findText(str);
  114. if (strFound) TRange.select();
  115. }
  116. } else if (navigator.appName == "Opera") {
  117. alert("Opera browsers not supported, sorry...");
  118. return;
  119. }
  120. return strFound ? true : false;
  121. };
  122.  
  123. /*
  124. * @description 格式化byte为KB、MB、GB、TB、PB、EB、ZB、YB、BB、NB、DB
  125. * @param {number} bitSize - 字节
  126. * @param {boolean} addType - 是否添加单位,默认添加
  127. * @return {string|number} - 添加单位就是字符串,否则为float类型,保留两位
  128. * @example Utils.formatByteToSize("812304");
  129. * @exampleResult
  130. */
  131. Utils.formatByteToSize = function (byteSize, addType = true) {
  132. /* B字节转KB、MB、GB */
  133. byteSize = parseInt(byteSize);
  134. if (isNaN(byteSize)) {
  135. throw "参数 byteSize 格式不正确";
  136. }
  137. var result = 0;
  138. var resultType = "KB";
  139. var sizeData = {};
  140. sizeData.KB = 1024;
  141. sizeData.MB = sizeData.KB * sizeData.KB;
  142. sizeData.GB = sizeData.MB * sizeData.KB;
  143. sizeData.TB = sizeData.GB * sizeData.KB;
  144. sizeData.PB = sizeData.TB * sizeData.KB;
  145. sizeData.EB = sizeData.PB * sizeData.KB;
  146. sizeData.ZB = sizeData.EB * sizeData.KB;
  147. sizeData.YB = sizeData.ZB * sizeData.KB;
  148. sizeData.BB = sizeData.YB * sizeData.KB;
  149. sizeData.NB = sizeData.BB * sizeData.KB;
  150. sizeData.DB = sizeData.NB * sizeData.KB;
  151. for (key in sizeData) {
  152. result = byteSize / sizeData[key];
  153. resultType = key;
  154. if (sizeData.KB >= result) {
  155. break;
  156. }
  157. }
  158. result = result.toFixed(2);
  159. result = addType ? result + resultType.toString() : parseFloat(result);
  160. return result;
  161. };
  162.  
  163. /*
  164. * @description 数组按照内部某个值的大小比对排序,如[{"time":"2022-1-1"},{"time":"2022-2-2"}]
  165. * @param {string} getPropertyValueFunc - 数组内部项的某个属性的值的方法,参数为这个项
  166. * @param {boolean} sortByDesc - 排序方式,true倒序(值最大排第一个,如:6、5、4、3...),false为正序(值最小排第一个,如:1、2、3、4...)
  167. * @return {object} - 返回比较排序完成的数组
  168. * @example [{"time":"2022-1-1"},{"time":"2022-2-2"}].sort(Utils.sortListByProperty((item)=>{return item["time"]}))
  169. * @example [{"time":"2022-1-1"},{"time":"2022-2-2"}].sort(Utils.sortListByProperty((item)=>{return item["time"]},false))
  170. */
  171. Utils.sortListByProperty = function (getPropertyValueFunc, sortByDesc = true) {
  172. if (typeof sortByDesc !== "boolean") {
  173. throw "参数 sortByDesc 必须为boolean类型";
  174. }
  175. if (getPropertyValueFunc == null) {
  176. throw "获取前面的值或后面的值的方法不能为空";
  177. }
  178. return function (after_obj, before_obj) {
  179. var beforeValue = getPropertyValueFunc(before_obj); /* 前 */
  180. var afterValue = getPropertyValueFunc(after_obj); /* 后 */
  181. if (sortByDesc) {
  182. if (afterValue > beforeValue) {
  183. return -1;
  184. } else if (afterValue < beforeValue) {
  185. return 1;
  186. } else {
  187. return 0;
  188. }
  189. } else {
  190. if (afterValue < beforeValue) {
  191. return -1;
  192. } else if (afterValue > beforeValue) {
  193. return 1;
  194. } else {
  195. return 0;
  196. }
  197. }
  198. };
  199. };
  200.  
  201. /*
  202. * @description Array添加一个内部属性,数组根据相同的字段合并成字符串
  203. * @example [{"name":"Array内部方法: "},{"name":"mergeToString"}].mergeToString("name");
  204. */
  205. Utils.mergeArrayToString = function (list, propertyName) {
  206. if (list == null) {
  207. throw "参数 list 不能为空";
  208. }
  209. if (propertyName == null) {
  210. throw "参数 propertyName 不能为null";
  211. }
  212. var content = "";
  213. Array.from(list).forEach((item) => {
  214. content = content + item[propertyName];
  215. });
  216. return content;
  217. };
  218.  
  219. /*
  220. * @description JSON内所有的值转为Array数组
  221. * @param {object} _json_ - JSON数据
  222. * @return {object} - 返回数组
  223. * @example Utils.jsonAllValueToArray({"Utils":"jsonToArray","return","Array"});
  224. * @exampleResult ['jsonToArray', 'Array']
  225. */
  226. Utils.jsonAllValueToArray = function (_json_) {
  227. if (typeof _json_ !== "object") {
  228. throw "参数 _json_ 必须为object类型";
  229. }
  230. var retArray = [];
  231. Object.keys(_json_).forEach(function (key) {
  232. retArray = [...retArray, _json_[key]];
  233. });
  234. return retArray;
  235. };
  236.  
  237. /*
  238. * @description JSON格式的字符串转为JSON对象
  239. * @param {string} text - JSON格式的字符串
  240. * @return {object} - 返回JSON对象
  241. * @example Utils.jsonStrToObject('{"Utils":"jsonStrToObject","return","json"}');
  242. * @exampleResult {"Utils":"jsonStrToObject","return","json"}
  243. */
  244. Utils.jsonStrToObject = function (text) {
  245. if (typeof text !== "string") {
  246. throw "参数 text 类型必须为string类型";
  247. }
  248. return window.eval("(" + text + ")");
  249. };
  250.  
  251. /*
  252. * @description 获取数组的随机值
  253. * @param {string} array - 数组数据
  254. * @return {string} - 返回数组的随机值
  255. * @example Utils.getArrayRandValue(["Utils","getArrayRandValue"]);
  256. * @exampleResult getArrayRandValue
  257. */
  258. Utils.getArrayRandValue = function (_array_) {
  259. return _array_[Math.floor(Math.random() * _array_.length)];
  260. };
  261.  
  262. /*
  263. * @description 获取两个数字区间的随机值
  264. * @param {number} number - 数字区间
  265. * @param {number} number2 - 数字区间
  266. * @return {number} - 返回两个数字区间的随机值
  267. * @example Utils.getRandNumber(1,10);
  268. * @exampleResult 5
  269. * @example Utils.getRandNumber(10,1);
  270. * @exampleResult 8
  271. */
  272. Utils.getRandNumber = function (number, number2) {
  273. if (typeof number !== "number") {
  274. throw "参数 number 必须为number类型";
  275. }
  276. if (typeof number2 !== "number") {
  277. throw "参数 number2 必须为number类型";
  278. }
  279. var leftNumber = number > number2 ? number2 : number;
  280. var rightNumber = number > number2 ? number : number2;
  281. return Math.round(Math.random() * (rightNumber - leftNumber)) + leftNumber;
  282. };
  283.  
  284. /*
  285. * @description 获取格式化后的Date类型时间
  286. * @param {string} text - 需要格式化的字符串或者时间戳
  287. * @param {string} types - 格式化成的显示类型
  288. * yyyy 年
  289. * MM 月
  290. * dd 天
  291. * HH 时 (24小时制)
  292. * hh 时 (12小时制)
  293. * mm 分
  294. * ss 秒
  295. * @return {string} - 返回格式化后的时间
  296. * @example Utils.getFormatTime("HH:mm:ss","2022-08-21 23:59:00");
  297. * @exampleResult 23:59:00
  298. * @example Utils.getFormatTime("HH:mm:ss",1899187424988);
  299. * @exampleResult 15:10:13
  300. */
  301. Utils.getFormatTime = function (types = "yyyy-MM-dd HH:mm:ss", text) {
  302. if (typeof types !== "string") {
  303. throw "参数 types 必须是string类型";
  304. }
  305. if (text != null && typeof text !== "string" && typeof text !== "number") {
  306. throw "参数 text 必须是string类型或者number类型";
  307. }
  308. var time = text == null ? new Date() : new Date(text);
  309. function _checkTime_(i) {
  310. /* 校验时间补0 */
  311. if (i < 10) return "0" + i;
  312. return i;
  313. }
  314.  
  315. function _timeSystemChange_(_hour_) {
  316. /* 时间制修改 24小时制转12小时制 */
  317. return _hour_ > 12 ? _hour_ - 12 : _hour_;
  318. }
  319.  
  320. var timeRegexp = {
  321. yyyy: time.getFullYear(),
  322. /* 年 */
  323. MM: _checkTime_(time.getMonth() + 1),
  324. /* 月 */
  325. dd: _checkTime_(time.getDate()),
  326. /* 日 */
  327. HH: _checkTime_(time.getHours()),
  328. /* 时 (24小时制) */
  329. hh: _checkTime_(_timeSystemChange_(time.getHours())),
  330. /* 时 (12小时制) */
  331. mm: _checkTime_(time.getMinutes()),
  332. /* 分 */
  333. ss: _checkTime_(time.getSeconds()),
  334. /* 秒 */
  335. };
  336. Object.keys(timeRegexp).forEach(function (key) {
  337. var replaecRegexp = new RegExp(key, "g");
  338. types = types.replace(replaecRegexp, timeRegexp[key]);
  339. });
  340. return types;
  341. };
  342. /*
  343. * @description 【手机】检测点击的地方是否在该元素区域内
  344. * @param {object} obj - 需要检测的DOM元素
  345. * @return {boolean} - 返回true或false
  346. * @example Utils.checkClickInDOM(document.querySelector(".xxx"));
  347. * @exampleResult false
  348. */
  349. Utils.checkClickInDOM = function (checkDOM) {
  350. if (!(checkDOM instanceof HTMLElement)) {
  351. throw "参数 checkDOM 类型必须为DOM元素";
  352. }
  353. var mouseClickPosX = Number(window.event.clientX); /* 鼠标相对屏幕横坐标 */
  354. var mouseClickPosY = Number(window.event.clientY); /* 鼠标相对屏幕纵坐标 */
  355. var elementPosXLeft = Number(
  356. checkDOM.getBoundingClientRect().left
  357. ); /* 要检测的元素的相对屏幕的横坐标最左边 */
  358. var elementPosXRight = Number(
  359. checkDOM.getBoundingClientRect().right
  360. ); /* 要检测的元素的相对屏幕的横坐标最右边 */
  361. var elementPosYTop = Number(
  362. checkDOM.getBoundingClientRect().top
  363. ); /* 要检测的元素的相对屏幕的纵坐标最上边 */
  364. var elementPosYBottom = Number(
  365. checkDOM.getBoundingClientRect().bottom
  366. ); /* 要检测的元素的相对屏幕的纵坐标最下边 */
  367. if (
  368. mouseClickPosX >= elementPosXLeft &&
  369. mouseClickPosX <= elementPosXRight &&
  370. mouseClickPosY >= elementPosYTop &&
  371. mouseClickPosY <= elementPosYBottom
  372. ) {
  373. return true;
  374. } else if (checkDOM.innerHTML.indexOf(window.event.target.innerHTML) !== -1) {
  375. /* 这种情况是应对在界面中隐藏的元素,getBoundingClientRect获取的都是0 */
  376. return true;
  377. } else {
  378. return false;
  379. }
  380. };
  381.  
  382. /*
  383. * @description 同步执行延时函数
  384. * @param {object|string} fnStr - 需要延时的函数或字符串格式的函数
  385. * @param {number} delayTime - 需要检测的DOM元素
  386. * @return {All|无返回值} - 返回自定义类型数据或者无返回
  387. * @example await Utils.asyncSetTimeOut(xxxFunction, 2500);
  388. * @exampleResult xxx
  389. * @example await Utils.asyncSetTimeOut("()=>{console.log(12345)}", 2500);
  390. * @exampleResult 无返回值
  391. */
  392. Utils.asyncSetTimeOut = function (fnStr, delayTime) {
  393. var _this = this;
  394. if (typeof fnStr !== "function" && typeof fnStr !== "string") {
  395. throw "参数 fnStr 类型必须为function类型或者string类型";
  396. }
  397. if (typeof delayTime !== "number") {
  398. throw "参数 delayTime 类型必须为number类型";
  399. }
  400. return new Promise((resolve) => {
  401. setTimeout(() => {
  402. resolve(_this.tryCatch(fnStr));
  403. }, delayTime);
  404. });
  405. };
  406.  
  407. /*
  408. * @description 同步执行,等待数组内部执行完毕,注意,该内部不能同步
  409. * @param {object} arrayData - 需要遍历的数组
  410. * @param {function} handleDataFunction - 对该数组进行操作的函数,该函数的参数为数组格式的参数,[数组下标,数组项]
  411. * @return 无返回值
  412. * @example await Utils.asyncArrayForEach([1,2,3],xxxFunction);
  413. */
  414. Utils.asyncArrayForEach = function (arrayData, handleDataFunction) {
  415. var _this = this;
  416. if (typeof arrayData !== "object") {
  417. throw "参数 arrayData 类型必须为object类型";
  418. }
  419. if (
  420. typeof handleDataFunction !== "function" &&
  421. typeof handleDataFunction !== "string"
  422. ) {
  423. throw "参数 handleDataFunction 类型必须为function或者string类型";
  424. }
  425. return Promise.all(
  426. Array.from(arrayData).map(async (item, index) => {
  427. await _this.tryCatch(handleDataFunction, [index, item]);
  428. })
  429. );
  430. };
  431.  
  432. /*
  433. * @description 同步延迟xxx毫秒
  434. * @param {number} delayTime - 需要遍历的数组
  435. * @return {无返回值}
  436. * @example await Utils.sleep(2500); - 同步延时2500毫秒
  437. */
  438. Utils.sleep = function (delayTime) {
  439. if (typeof delayTime !== "number") {
  440. throw "参数 delayTime 类型必须为number类型";
  441. }
  442. return new Promise((resolve) => {
  443. setTimeout(() => {
  444. resolve();
  445. }, delayTime);
  446. });
  447. };
  448.  
  449. /*
  450. * @description 注册全局函数Cookies
  451. * @function Cookies.get("key")
  452. * @param {string|number} key - 需要获取的cookie的key
  453. * @return {string} 获取到的cookie值或者为空
  454. * @function Cookies.set("key","value",8400);
  455. * @param {string|number} key - 需要设置的cookie的key
  456. * @param {All} key - 需要设置的cookie的value
  457. * @param {number} time - 需要设置的cookie的过期时间
  458. * @return 无返回值
  459. * @example Utils.registerWindowCookies();
  460. * Cookies.get("xxxx");
  461. */
  462. Utils.registerWindowCookies = () => {
  463. /*! js-cookie v3.0.1 | MIT */
  464. (function (global, factory) {
  465. typeof exports === "object" && typeof module !== "undefined"
  466. ? (module.exports = factory())
  467. : typeof define === "function" && define.amd
  468. ? define(factory)
  469. : ((global = global || self),
  470. (function () {
  471. var current = global.Cookies;
  472. var exports = (global.Cookies = factory());
  473. exports.noConflict = function () {
  474. global.Cookies = current;
  475. return exports;
  476. };
  477. })());
  478. })(this, function () {
  479. "use strict";
  480. /* eslint-disable no-var */
  481. function assign(target) {
  482. for (var i = 1; i < arguments.length; i++) {
  483. var source = arguments[i];
  484. for (var key in source) {
  485. target[key] = source[key];
  486. }
  487. }
  488. return target;
  489. }
  490. /* eslint-enable no-var */
  491. /* eslint-disable no-var */
  492. var defaultConverter = {
  493. read: function (value) {
  494. if (value[0] === '"') {
  495. value = value.slice(1, -1);
  496. }
  497. return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent);
  498. },
  499. write: function (value) {
  500. return encodeURIComponent(value).replace(
  501. /%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,
  502. decodeURIComponent
  503. );
  504. },
  505. };
  506. /* eslint-enable no-var */
  507.  
  508. /* eslint-disable no-var */
  509.  
  510. function init(converter, defaultAttributes) {
  511. function set(key, value, attributes) {
  512. if (typeof document === "undefined") {
  513. return;
  514. }
  515.  
  516. attributes = assign({}, defaultAttributes, attributes);
  517.  
  518. if (typeof attributes.expires === "number") {
  519. attributes.expires = new Date(
  520. Date.now() + attributes.expires * 864e5
  521. );
  522. }
  523. if (attributes.expires) {
  524. attributes.expires = attributes.expires.toUTCString();
  525. }
  526.  
  527. key = encodeURIComponent(key)
  528. .replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent)
  529. .replace(/[()]/g, escape);
  530.  
  531. var stringifiedAttributes = "";
  532. for (var attributeName in attributes) {
  533. if (!attributes[attributeName]) {
  534. continue;
  535. }
  536.  
  537. stringifiedAttributes += "; " + attributeName;
  538.  
  539. if (attributes[attributeName] === true) {
  540. continue;
  541. }
  542. /* Considers RFC 6265 section 5.2:
  543. ...
  544. 3. If the remaining unparsed-attributes contains a %x3B (";")
  545. character:
  546. Consume the characters of the unparsed-attributes up to,
  547. not including, the first %x3B (";") character.
  548. ... */
  549. stringifiedAttributes +=
  550. "=" + attributes[attributeName].split(";")[0];
  551. }
  552.  
  553. return (document.cookie =
  554. key + "=" + converter.write(value, key) + stringifiedAttributes);
  555. }
  556.  
  557. function get(key) {
  558. if (typeof document === "undefined" || (arguments.length && !key)) {
  559. return;
  560. }
  561.  
  562. /* To prevent the for loop in the first place assign an empty array
  563. in case there are no cookies at all. */
  564. var cookies = document.cookie ? document.cookie.split("; ") : [];
  565. var jar = {};
  566. for (var i = 0; i < cookies.length; i++) {
  567. var parts = cookies[i].split("=");
  568. var value = parts.slice(1).join("=");
  569.  
  570. try {
  571. var foundKey = decodeURIComponent(parts[0]);
  572. jar[foundKey] = converter.read(value, foundKey);
  573.  
  574. if (key === foundKey) {
  575. break;
  576. }
  577. } catch (e) {}
  578. }
  579.  
  580. return key ? jar[key] : jar;
  581. }
  582.  
  583. return Object.create(
  584. {
  585. set: set,
  586. get: get,
  587. remove: function (key, attributes) {
  588. set(
  589. key,
  590. "",
  591. assign({}, attributes, {
  592. expires: -1,
  593. })
  594. );
  595. },
  596. withAttributes: function (attributes) {
  597. return init(
  598. this.converter,
  599. assign({}, this.attributes, attributes)
  600. );
  601. },
  602. withConverter: function (converter) {
  603. return init(assign({}, this.converter, converter), this.attributes);
  604. },
  605. },
  606. {
  607. attributes: {
  608. value: Object.freeze(defaultAttributes),
  609. },
  610. converter: {
  611. value: Object.freeze(converter),
  612. },
  613. }
  614. );
  615. }
  616.  
  617. var api = init(defaultConverter, {
  618. path: "/",
  619. });
  620. /* eslint-enable no-var */
  621.  
  622. return api;
  623. });
  624. };
  625.  
  626. /*
  627. * @description base64转blob
  628. * @param {string} dataurl - base64的数据
  629. * @return {string} blob的链接
  630. * @example Utils.base64ToBlob("data:image/jpeg;base64,.....");
  631. * @exampleResult blob://xxxxxxx
  632. */
  633. Utils.base64ToBlob = function (dataurl) {
  634. if (typeof dataurl !== "string") {
  635. throw "参数 dataurl 类型必须为string类型";
  636. }
  637. var arr = dataurl.split(","),
  638. mime = arr[0].match(/:(.*?);/)[1],
  639. bstr = atob(arr[1]),
  640. n = bstr.length,
  641. u8arr = new Uint8Array(n);
  642. while (n--) {
  643. u8arr[n] = bstr.charCodeAt(n);
  644. }
  645. return new Blob([u8arr], {
  646. type: mime,
  647. });
  648. };
  649. /*
  650. * @description base64转File对象
  651. * @param {string} dataurl - base64的数据
  652. * @return {string} blob的链接
  653. * @example Utils.base64ToFile("data:image/jpeg;base64,.....");
  654. * @exampleResult object
  655. */
  656. Utils.base64ToFile = function (dataurl, fileName) {
  657. if (typeof dataurl !== "string") {
  658. throw "参数 dataurl 类型必须为string类型";
  659. }
  660. if (typeof fileName !== "string") {
  661. throw "参数 fileName 类型必须为string类型";
  662. }
  663. var arr = dataurl.split(","),
  664. mime = arr[0].match(/:(.*?);/)[1],
  665. bstr = atob(arr[1]),
  666. n = bstr.length,
  667. u8arr = new Uint8Array(n);
  668. while (n--) {
  669. u8arr[n] = bstr.charCodeAt(n);
  670. }
  671. return new File([u8arr], fileName, {
  672. type: mime,
  673. });
  674. };
  675.  
  676. /*
  677. * @description blob转File对象
  678. * @param {string} theBlob - 需要转换的blob的链接
  679. * @param {string} fileName - 转换成的File对象的文件名称
  680. * @return {object} File对象
  681. * @example Utils.blobToFile("blob://xxxxx");
  682. * @exampleResult object
  683. */
  684. Utils.blobToFile = function (theBlob, fileName) {
  685. if (typeof theBlob !== "string") {
  686. throw "参数 theBlob 类型必须为string类型";
  687. }
  688. if (typeof fileName !== "string") {
  689. throw "参数 fileName 类型必须为string类型";
  690. }
  691. theBlob.lastModifiedDate = new Date();
  692. theBlob.name = fileName;
  693. return theBlob;
  694. };
  695.  
  696. /*
  697. * @description 同步File对象转base64
  698. * @param {object} file - 需要转换的File对象
  699. * @return {string} base64格式的数据
  700. * @example await Utils.asyncFileToBase64(object);
  701. * @exampleResult data:image/jpeg:base64/,xxxxxx
  702. */
  703. Utils.asyncFileToBase64 = function (file) {
  704. var reader = new FileReader();
  705. reader.readAsDataURL(file);
  706. return new Promise((resolve) => {
  707. reader.onload = function (e) {
  708. resolve(e.target.result);
  709. };
  710. });
  711. };
  712.  
  713. /*
  714. * @description 下载base64格式的数据
  715. * @param {string} base64Content - 需要转换的base64数据
  716. * @param {string} fileName - 需要保存的文件名
  717. * @example Utils.downloadBase64("data:image/jpeg:base64/,xxxxxx");
  718. */
  719. Utils.downloadBase64 = function (base64Content, fileName) {
  720. var aLink = document.createElement("a");
  721. var blob = this.base64ToBlob(base64Content);
  722. var evt = document.createEvent("HTMLEvents");
  723. evt.initEvent(
  724. "click",
  725. true,
  726. true
  727. ); /* initEvent 不加后两个参数在FF下会报错 事件类型,是否冒泡,是否阻止浏览器的默认行为 */
  728. aLink.download = fileName;
  729. aLink.href = URL.createObjectURL(blob);
  730. aLink.click();
  731. };
  732.  
  733. /*
  734. * @description 判断是否是手机访问
  735. * @return {boolean} - 返回如果是手机true,否则false
  736. * @example Utils.isPhone();
  737. * @exampleResult true
  738. */
  739. Utils.isPhone = function () {
  740. return Boolean(/(iPhone|iPad|iPod|iOS|Android)/i.test(navigator.userAgent));
  741. };
  742.  
  743. /*
  744. * @description 自定义字典,用于new
  745. * @example new let dictionary = Utils.Dictionary();
  746. * @example dictionary.set("xxx","xxx");
  747. * @example dictionary.get("xxx");
  748. * @example dictionary.has("xxx");
  749. */
  750. Utils.Dictionary = function () {
  751. this.items = {};
  752. this.has = function (key) {
  753. /* 检查是否有某一个键 */
  754. return this.items.hasOwnProperty(key);
  755. };
  756. this.set = function (key, val = "") {
  757. /* 为字典添加某一个值 */
  758. if (key === undefined) {
  759. throw "参数 key 不能为空";
  760. }
  761. this.items[key] = val;
  762. };
  763. this.delete = function (key) {
  764. /* 删除某一个键 */
  765. if (this.has(key)) {
  766. delete this.items[key];
  767. return true;
  768. }
  769. return false;
  770. };
  771. this.get = function (key) {
  772. /* 查找某一特定项 */
  773. return this.has(key) ? this.items[key] : undefined;
  774. };
  775. this.values = function () {
  776. /* 返回字典中的所有值 */
  777. var resultList = [];
  778. for (var prop in this.items) {
  779. if (this.has(prop)) {
  780. resultList.push(this.items[prop]);
  781. }
  782. }
  783. return resultList;
  784. };
  785. this.clear = function () {
  786. /* 清空字典 */
  787. this.items = {};
  788. };
  789. this.size = function () {
  790. /* 获取字典的长度 */
  791. return Object.keys(this.items).length;
  792. };
  793. this.keys = function () {
  794. /* 获取字典所有的键 */
  795. return Object.keys(this.items);
  796. };
  797. this.getItems = function () {
  798. /* 返回字典本身 */
  799. return this.items;
  800. };
  801. };
  802.  
  803. /*
  804. * @description JSON数据从源端替换到目标端中,如果目标端存在该数据则替换,不添加,返回结果为目标端替换完毕的结果
  805. * @param {object} target - 目标端
  806. * @param {object} source - 源端
  807. * @example Utils.assignJSON({"1":1,"2":{"3":3}},{"2":{"3":4}});
  808. * @exampleResult {"1":1,"2":{"3":4}}
  809. */
  810. Utils.assignJSON = function (target, source) {
  811. for (var target_key in target) {
  812. if (typeof source[target_key] !== "undefined") {
  813. if (
  814. typeof source[target_key] === "object" &&
  815. !(source[target_key] instanceof HTMLElement)
  816. ) {
  817. target[target_key] = this.assignJSON(
  818. target[target_key],
  819. source[target_key]
  820. );
  821. } else {
  822. target[target_key] = source[target_key];
  823. }
  824. }
  825. }
  826. return target;
  827. };
  828.  
  829. /*
  830. * @description 判断对象或数据是否为空
  831. * @param {all} object - 需要判断的变量
  832. * @example Utils.isNull({});
  833. * @exampleResult true
  834. * @example Utils.isNull([]);
  835. * @exampleResult true
  836. */
  837. Utils.isNull = function (object) {
  838. var result = false;
  839. if (typeof object === "undefined") {
  840. result = true;
  841. } else if (typeof object === "object") {
  842. if (Object.keys(object).length === 0) {
  843. result = true;
  844. }
  845. } else if (typeof object === "number") {
  846. result = object === 0 ? true : false;
  847. }
  848. return result;
  849. };
  850.  
  851. /*
  852. * @description 自动锁对象,用于循环判断运行的函数,在循环外new后使用,注意,如果函数内部存在异步操作,需要使用await
  853. * @param {object} func - 需要执行的函数
  854. * @param {object|undefined} funcArgs - 需要执行的函数的参数
  855. * @example var lock = new Utils.lockFunction(xxxx)
  856. * --- 此处是循环内 ---
  857. * lock.run();
  858. * --- 此处是循环内 ---
  859. * @example var lock = new Utils.lockFunction(xxxx,true) -- 异步操作
  860. * --- 此处是循环内 ---
  861. * await lock.run();
  862. * --- 此处是循环内 ---
  863. */
  864. Utils.lockFunction = function (func) {
  865. this.flag = false;
  866. this.lock = function () {
  867. this.flag = true;
  868. };
  869. this.unlock = function () {
  870. this.flag = false;
  871. };
  872. this.run = async function () {
  873. if (this.flag) {
  874. return;
  875. }
  876. this.lock();
  877. await func(arguments); /* arguments调用 */
  878. this.unlock();
  879. };
  880. };
  881.  
  882. /*
  883. * @description 等待某个对象出现,结果为异步,需要await或者then
  884. * @param {object} target - 需要寻找的DOM,传入id或class或div等...
  885. * @param {number} intervalNumMax - 循环次数
  886. * @param {number} intervalTime - 每次的循环时间
  887. * @return {dom|null} - 如果找到返回数组形式的dom对象,否则返回空数组
  888. * @example await Utils.waitForDOM("div.xxx");
  889. * @example Utils.waitForDOM("div#xxx").then((dom)=>{xxxx});
  890. */
  891. Utils.waitForDOM = function (target, intervalNumMax = 90, intervalTime = 300) {
  892. if (typeof target !== "string") {
  893. throw "参数 target 必须为string类型";
  894. }
  895. intervalNumMax = parseInt(intervalNumMax);
  896. intervalTime = parseInt(intervalTime);
  897. var intervalNum = 0;
  898. return new Promise((resolve) => {
  899. var interval = setInterval(function () {
  900. if (intervalNum > intervalNumMax) {
  901. resolve([]);
  902. clearInterval(interval);
  903. return;
  904. }
  905. if (document.querySelectorAll(target).length !== 0) {
  906. resolve(document.querySelectorAll(target));
  907. clearInterval(interval);
  908. return;
  909. }
  910. intervalNum++;
  911. }, intervalTime);
  912. });
  913. };
  914.  
  915. /*
  916. * @description 删除某个父元素,父元素可能在上层或上上层或上上上层...
  917. * @param {object} target - 当前元素
  918. * @param {object} parentDOMFunc - 满足父元素的条件方法判断,参数为当前处理的父元素,如果满足条件返回true
  919. * @return {boolean} - 如果找到就删除返回true,如果未删除返回false
  920. * @example Utils.deleteParentDOM(document.querySelector(".xxx"),(dom)=>{return dom.id="xxx" ? true:false});
  921. * @exampleResult true;
  922. */
  923. Utils.deleteParentDOM = function (target, parentDOMFunc) {
  924. if (target == null) {
  925. throw "参数 target 不能为null";
  926. }
  927. if (!(target instanceof HTMLElement)) {
  928. throw "参数 target 类型必须为DOM元素";
  929. }
  930. if (typeof parentDOMFunc !== "function") {
  931. throw "参数 parentDOMFunc 类型必须为function类型";
  932. }
  933. var result = false;
  934. var _parentElement_ = target.parentElement;
  935. while (!0) {
  936. if (_parentElement_ == null) {
  937. return;
  938. }
  939. var handleStatus = parentDOMFunc(_parentElement_);
  940. if (handleStatus) {
  941. result = true;
  942. _parentElement_.remove();
  943. break;
  944. }
  945. _parentElement_ = _parentElement_.parentElement;
  946. }
  947. return result;
  948. };
  949.  
  950. /*
  951. * @description 获取某个父元素,父元素可能在上层或上上层或上上上层...
  952. * @param {object} target - 当前元素
  953. * @param {object} parentDOMFunc - 满足父元素的条件方法判断,参数为当前处理的父元素,如果满足条件返回true
  954. * @return {boolean} - 如果找到返回满足要求的父元素,如果未找到返回null
  955. * @example Utils.findParentDOM(document.querySelector(".xxx"),(dom)=>{return dom.id="xxx" ? true:false});
  956. * @exampleResult {HTMLElement};
  957. */
  958. Utils.findParentDOM = function (target, parentDOMFunc) {
  959. if (target == null) {
  960. throw "参数 target 不能为null";
  961. }
  962. if (!(target instanceof HTMLElement)) {
  963. throw "参数 target 类型必须为DOM元素";
  964. }
  965. if (typeof parentDOMFunc !== "function") {
  966. throw "参数 parentDOMFunc 类型必须为function类型";
  967. }
  968. var result = null;
  969. var _parentElement_ = target.parentElement;
  970. while (!0) {
  971. if (_parentElement_ == null) {
  972. return;
  973. }
  974. var handleStatus = parentDOMFunc(_parentElement_);
  975. if (handleStatus) {
  976. result = _parentElement_;
  977. break;
  978. }
  979. _parentElement_ = _parentElement_.parentElement;
  980. }
  981. return result;
  982. };
  983.  
  984. /*
  985. * @description 复制到剪贴板
  986. * @param {string|number} text - 需要复制到剪贴板的文本
  987. * @example Utils.setClip("xxxx");
  988. */
  989. Utils.setClip = function (text) {
  990. if (text == null) {
  991. return;
  992. }
  993. var clipBoardDOM = document.createElement("input");
  994. clipBoardDOM.type = "text";
  995. clipBoardDOM.setAttribute("style", "opacity:0;position:absolute;");
  996. clipBoardDOM.id = "whitesevClipBoardInput";
  997. document.body.append(clipBoardDOM);
  998. var clipBoardInputNode = document.querySelector("#whitesevClipBoardInput");
  999. clipBoardInputNode.value = text;
  1000. clipBoardInputNode.removeAttribute("disabled");
  1001. clipBoardInputNode.select();
  1002. document.execCommand("copy");
  1003. clipBoardInputNode.remove();
  1004. };
  1005.  
  1006. /*
  1007. * @description 监听页面元素改变并处理
  1008. * @param {object|string} target - 需要监听的元素,如果不存在,可以等待它出现
  1009. * @param {object} observer_config - MutationObserver的配置
  1010. * @example Utils.mutationObserver("div.xxxx",{"fn":(mutations)=>{},"config":{childList:true,attributes:true}});
  1011. */
  1012. Utils.mutationObserver = function (target, observer_config) {
  1013. if (typeof target !== "string" && !(target instanceof HTMLElement)) {
  1014. throw "参数 target 类型必须为string或者HTMLElement类型";
  1015. }
  1016. var default_obverser_config = {
  1017. /* 监听到元素有反馈,需执行的函数 */
  1018. fn: () => {},
  1019. config: {
  1020. /* 当为 true 时,将会监听以 target 为根节点的整个子树。包括子树中所有节点的属性,而不仅仅是针对 target。默认值为 false */
  1021. subtree: undefined,
  1022. /* 当为 true 时,监听 target 节点中发生的节点的新增与删除(同时,如果 subtree 为 true,会针对整个子树生效)。默认值为 false。 */
  1023. childList: undefined,
  1024. /* 当为 true 时观察所有监听的节点属性值的变化。默认值为 true,当声明了 attributeFilter 或 attributeOldValue,默认值则为 false */
  1025. attributes: undefined,
  1026. /* 一个用于声明哪些属性名会被监听的数组。如果不声明该属性,所有属性的变化都将触发通知 */
  1027. attributeFilter: undefined,
  1028. /* 当为 true 时,记录上一次被监听的节点的属性变化;可查阅 MutationObserver 中的 Monitoring attribute values 了解关于观察属性变化和属性值记录的详情。默认值为 false */
  1029. attributeOldValue: undefined,
  1030. /* 当为 true 时,监听声明的 target 节点上所有字符的变化。默认值为 true,如果声明了 characterDataOldValue,默认值则为 false */
  1031. characterData: undefined,
  1032. /* 当为 true 时,记录前一个被监听的节点中发生的文本变化。默认值为 false */
  1033. characterDataOldValue: undefined,
  1034. },
  1035. };
  1036. observer_config = this.assignJSON(default_obverser_config, observer_config);
  1037. var MutationObserver =
  1038. window.MutationObserver ||
  1039. window.webkitMutationObserver ||
  1040. window.MozMutationObserver;
  1041. var mutationObserver = new MutationObserver(function (mutations) {
  1042. observer_config.fn(mutations);
  1043. });
  1044. if (target instanceof HTMLElement) {
  1045. /* 传入的参数是节点元素 */
  1046. mutationObserver.observe(target, observer_config.config);
  1047. } else {
  1048. /* 传入的target是字符串 */
  1049. this.waitForDOM(target).then((dom) => {
  1050. if (dom.length) {
  1051. mutationObserver.observe(dom[dom.length - 1], observer_config.config);
  1052. }
  1053. });
  1054. }
  1055. };
  1056.  
  1057. /*
  1058. * @description 获取随机的安卓手机User-Agent(25)个
  1059. * @return {string} - User-Agent
  1060. * @example Utils.getRandomAndroidUA();
  1061. * @exampleResult Mozilla/5.0....
  1062. */
  1063. Utils.getRandomAndroidUA = function () {
  1064. const ANDROID_UA = [
  1065. "Mozilla/5.0 (Linux; Android 12; LDN-LX3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.3538.80 Mobile Safari/537.36",
  1066. "Mozilla/5.0 (Linux; Android 12; RNE-L03) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.3538.80 Mobile Safari/537.36",
  1067. "Mozilla/5.0 (Linux; Android 12; ASUS_X00ID Build/NMF26F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.3497.100 Mobile Safari/537.36",
  1068. "Mozilla/5.0 (Linux; Android 12; WAS-LX3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.3538.80 Mobile Safari/537.36",
  1069. "Mozilla/5.0 (Linux; Android 12; PRA-LX3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.3538.80 Mobile Safari/537.36",
  1070. "Mozilla/5.0 (Linux; Android 12; MYA-L03) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.3538.64 Mobile Safari/537.36",
  1071. "Mozilla/5.0 (Linux; Android 12; PBEM00 Build/PKQ1.190519.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/89.3904.62 XWEB/2891 MMWEBSDK/200901 Mobile Safari/537.36 MMWEBID/4773 MicroMessenger/12.19.1760(0x28901335) Process/toolsmp WeChat/arm64 NetType/4G Language/zh_CN ABI/arm64",
  1072. "Mozilla/5.0 (Linux; Android 11; M2003J15SC Build/RP1A.200720.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/89.0.4389.72 MQQBrowser/6.2 TBS/046011 Mobile Safari/537.36",
  1073. "Mozilla/5.0 (Linux; Android 11; Moto G Play) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.3538.64 Mobile Safari/537.36",
  1074. "Mozilla/5.0 (Linux; Android 11; Moto C Build/NRD90M.063) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.3440.91 Mobile Safari/537.36",
  1075. "Mozilla/5.0 (Linux; Android 11; Redmi Note 4 Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.3396.87 Mobile Safari/537.36",
  1076. "Mozilla/5.0 (Linux; Android 11; HUAWEI VNS-L21 Build/HUAWEIVNS-L21) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.3359.158 Mobile Safari/537.36",
  1077. "Mozilla/5.0 (Linux; Android 10; VTR-L09) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.3538.80 Mobile Safari/537.36",
  1078. "Mozilla/5.0 (Linux; Android 10; ANG-AN00 Build/HUAWEIANG-AN00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/89.0.4389.72 MQQBrowser/6.2 TBS/046011 Mobile Safari/537.36",
  1079. "Mozilla/5.0 (Linux; Android 10; MI 5X Build/OPM1.171019.019; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/89.3904.62 XWEB/2891 MMWEBSDK/200801 Mobile Safari/537.36 MMWEBID/9633 MicroMessenger/12.18.1740(0x2890123B) Process/toolsmp WeChat/arm64 NetType/4G Language/zh_CN ABI/arm64",
  1080. "Mozilla/5.0 (Linux; Android 10; Moto C Plus Build/NRD90M.04.026) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.3440.91 Mobile Safari/537.36",
  1081. "Mozilla/5.0 (Linux; Android 10; TRT-LX3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.3538.64 Mobile Safari/537.36",
  1082. "Mozilla/5.0 (Linux; Android 9; Moto G (5) Build/NPPS25.137-93-14; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/89.0.3497.100 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/193.0.0.45.101;]",
  1083. "Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 9.0; ARM; Trident/12; Touch; rv:11.0; IEMobile/11.0; HTC; Windows Phone 8X by HTC) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko) Mobile Safari/537",
  1084. "Mozilla/5.0 (Linux; Android 9; Moto G Build/MOB30M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/89.0.2403.119 Mobile Safari/537.36",
  1085. "Mozilla/5.0 (Linux; Android 8; MI 6 Build/OPR1.170623.027; wv) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.24 Mobile Safari/537.36",
  1086. "Mozilla/5.0 (Linux; Android 8; M2003J15SC Build/RP1A.200720.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.4960.1 Mobile Safari/537.36",
  1087. "Mozilla/5.0 (Linux; Android 8; M2003J15SC Build/RP1A.200720.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4873.1 Mobile Safari/537.36",
  1088. "Mozilla/5.0 (Linux; Android 8; M2003J15SC Build/RP1A.200720.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4749.1 Mobile Safari/537.36",
  1089. "Mozilla/5.0 (Linux; Android 8; M2003J15SC Build/RP1A.200720.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4600.1 Mobile Safari/537.36",
  1090. ];
  1091. return ANDROID_UA[Math.floor(Math.random() * ANDROID_UA.length)];
  1092. };
  1093.  
  1094. /*
  1095. * @description 获取随机的电脑端User-Agent(25)个
  1096. * @return {string} - User-Agent
  1097. * @example Utils.getRandomPCUA();
  1098. * @exampleResult Mozilla/5.0....
  1099. */
  1100. Utils.getRandomPCUA = function () {
  1101. const PC_UA = [
  1102. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.5304.107 Safari/537.36",
  1103. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.5249.119 Safari/537.36",
  1104. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36",
  1105. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36",
  1106. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36",
  1107. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5089.1 Safari/537.36",
  1108. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.24 Safari/537.36",
  1109. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.4960.1 Safari/537.36",
  1110. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4873.1 Safari/537.36",
  1111. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.94 Safari/537.36",
  1112. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4749.1 Safari/537.36",
  1113. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4687.2 Safari/537.36",
  1114. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4658.2 Safari/537.36",
  1115. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4635.4 Safari/537.36",
  1116. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4600.1 Safari/537.36",
  1117. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4573.1 Safari/537.36 Edge/43.0.2442.991",
  1118. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4510.2 Safari/537.36",
  1119. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4461.1 Safari/537.36",
  1120. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4412.5 Safari/537.36",
  1121. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4388.4 Safari/537.36",
  1122. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.2272.101 Safari/537.36",
  1123. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.218 Safari/537.36 Edge/13.10586",
  1124. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4279.4 Safari/537.36 Edge/13.10586",
  1125. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.2228.0 Safari/537.36",
  1126. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.3538.67 Safari/537.36",
  1127. ];
  1128. return PC_UA[Math.floor(Math.random() * PC_UA.length)];
  1129. };
  1130.  
  1131. /*
  1132. * @description 浏览器端的indexedDB操作封装
  1133. * let db = new Utils.indexedDB('web_DB', 'nav_text')
  1134. *
  1135. * let data = {name:'管理员', roleId: 1, type: 1};
  1136. *
  1137. * db.save('list',data).then((resolve)=>{
  1138. * console.log(resolve,'存储成功')
  1139. * })
  1140. *
  1141. * db.get('list').then((resolve)=>{
  1142. * console.log(resolve,'查询成功')
  1143. * })
  1144. *
  1145. * db.getPaging('list',20,10).then((resolve)=>{
  1146. * console.log(resolve,'查询分页偏移第20,一共10行成功');
  1147. * })
  1148. *
  1149. * db.delete('list').then(resolve=>{
  1150. * console.log(resolve,'删除成功---->>>>>>name')
  1151. * })
  1152. *
  1153. * db.deleteAll().then(resolve=>{
  1154. * console.log(resolve,'清除数据库---->>>>>>name')
  1155. * })
  1156. *
  1157. */
  1158. Utils.indexedDB = function (dbName = "default_db", storeName = "default_form") {
  1159. this.dbName = dbName; /* 数据存储名 */
  1160. this.slqVersion = "1"; /* websql的版本号,由于ios的问题,版本号的写法不一样 */
  1161. this.dbVersion = 1; /* indexDB的版本号 */
  1162. this.storeName = storeName; /* store----即“表”的名字 */
  1163. this.indexedDB =
  1164. window.indexedDB ||
  1165. window.mozIndexedDB ||
  1166. window.webkitIndexedDB ||
  1167. window.msIndexedDB; /* 监听IndexDB */
  1168. if (!this.indexedDB) {
  1169. alert("很抱歉,您的浏览器不支持indexedDB");
  1170. }
  1171. this.db = {}; /* 缓存数据库,避免同一个页面重复创建和销毁 */
  1172. this.store = null;
  1173. this.errorCode = {
  1174. /* 错误码 */
  1175. success: {
  1176. code: 200,
  1177. msg: "操作成功",
  1178. },
  1179. error: {
  1180. code: 401,
  1181. msg: "操作失败",
  1182. },
  1183. open: { code: 91001, msg: "打开数据库失败" },
  1184. save: { code: 91002, msg: "保存数据失败" },
  1185. get: { code: 91003, msg: "获取数据失败" },
  1186. delete: { code: 91004, msg: "删除数据失败" },
  1187. deleteAll: { code: 91005, msg: "清空数据库失败" },
  1188. };
  1189. /* 创建“表” */
  1190. this.createStore = function (dbName) {
  1191. let txn, store;
  1192. if (this.indexedDB) {
  1193. /* 如果是支持IndexDB的 */
  1194. txn = this.db[dbName].transaction(
  1195. this.storeName,
  1196. "readwrite"
  1197. ); /* IndexDB的读写权限 */
  1198. store = txn.objectStore(this.storeName);
  1199. }
  1200. return store;
  1201. };
  1202. this.open = function (callback, dbName) {
  1203. let that = this;
  1204. /* 打开数据库 */
  1205. if (that.indexedDB) {
  1206. /* 如果支持IndexDB */
  1207. if (!that.db[dbName]) {
  1208. /* 如果缓存中没有,则进行数据库的创建或打开,提高效率 */
  1209. let request = that.indexedDB.open(dbName, that.dbVersion);
  1210. request.onerror = function (e) {
  1211. callback({
  1212. code: that.errorCode.open.code,
  1213. msg: that.errorCode.open.msg,
  1214. error: e,
  1215. });
  1216. };
  1217. request.onsuccess = function (e) {
  1218. if (!that.db[dbName]) {
  1219. that.db[dbName] = e.target.result;
  1220. }
  1221. let store = that.createStore(dbName);
  1222. callback(store);
  1223. };
  1224. request.onupgradeneeded = function (e) {
  1225. that.db[dbName] = e.target.result;
  1226. let store = that.db[dbName].createObjectStore(that.storeName, {
  1227. keyPath: "key",
  1228. });
  1229. store.transaction.oncomplete = function (event) {
  1230. callback(store);
  1231. };
  1232. };
  1233. } else {
  1234. /* 如果缓存中已经打开了数据库,就直接使用 */
  1235. let store = that.createStore(dbName);
  1236. callback(store);
  1237. }
  1238. }
  1239. };
  1240. this.save = function (key, value) {
  1241. /* 保存数据到数据库 key---数据key value----数据值 */
  1242. let that = this;
  1243. if (that.indexedDB) {
  1244. return new Promise((resolve, reject) => {
  1245. let dbName = that.dbName;
  1246. let inData = {
  1247. key: key,
  1248. value: value,
  1249. };
  1250. that.open(function (result) {
  1251. let error = result.hasOwnProperty("error");
  1252. if (error) {
  1253. resolve(result);
  1254. } else {
  1255. let request = result.put(inData);
  1256. request.onsuccess = function (e) {
  1257. resolve({
  1258. code: that.errorCode.success.code,
  1259. msg: that.errorCode.success.msg,
  1260. success: true,
  1261. }); /* 保存成功有success 字段 */
  1262. };
  1263. request.onerror = function (e) {
  1264. resolve({
  1265. code: that.errorCode.save.code,
  1266. msg: that.errorCode.save.msg,
  1267. error: e,
  1268. });
  1269. };
  1270. }
  1271. }, dbName);
  1272. });
  1273. }
  1274. };
  1275. this.get = function (key) {
  1276. /* 根据key获取值 */
  1277. let that = this;
  1278. return new Promise((resolve, reject) => {
  1279. let dbName = that.dbName;
  1280. if (that.indexedDB) {
  1281. that.open(function (result) {
  1282. let error =
  1283. result.hasOwnProperty(
  1284. "error"
  1285. ); /* 判断返回的数据中是否有error字段 */
  1286. if (error) {
  1287. reject({
  1288. code: that.errorCode.open.get,
  1289. msg: that.errorCode.get.msg,
  1290. error: error,
  1291. result: result,
  1292. });
  1293. } else {
  1294. let request = result.get(key);
  1295. request.onsuccess = function (e) {
  1296. let result = e.target.result;
  1297. let data = result ? result.value : undefined;
  1298. resolve({
  1299. code: data
  1300. ? that.errorCode.success.code
  1301. : that.errorCode.error.code,
  1302. msg: data
  1303. ? that.errorCode.success.msg
  1304. : that.errorCode.error.msg,
  1305. data: data || [],
  1306. success: true,
  1307. });
  1308. };
  1309. request.onerror = function (e) {
  1310. reject({
  1311. code: that.errorCode.get.code,
  1312. msg: that.errorCode.get.msg,
  1313. result: result,
  1314. error: e,
  1315. });
  1316. };
  1317. }
  1318. }, dbName);
  1319. }
  1320. });
  1321. };
  1322. this.regexpGet = function (key) {
  1323. let that = this;
  1324. var list = [];
  1325. return new Promise((resolve, reject) => {
  1326. /* 正则查询 */
  1327. let dbName = that.dbName;
  1328. if (that.indexedDB) {
  1329. that.open(function (result) {
  1330. let error =
  1331. result.hasOwnProperty(
  1332. "error"
  1333. ); /* 判断返回的数据中是否有error字段 */
  1334. if (error) {
  1335. reject({
  1336. code: that.errorCode.open.get,
  1337. msg: that.errorCode.get.msg,
  1338. error: error,
  1339. result: result,
  1340. });
  1341. } else {
  1342. let request = result.getAll();
  1343. request.onsuccess = function (e) {
  1344. let result = e.target.result;
  1345. if (result.length !== 0) {
  1346. result.forEach((item, index) => {
  1347. if (item["key"].match(key)) {
  1348. var concatList = item["value"];
  1349. concatList["key"] = item["key"];
  1350. list = [...list, concatList];
  1351. }
  1352. });
  1353. }
  1354. resolve({
  1355. code: that.errorCode.success.code,
  1356. msg: that.errorCode.success.msg,
  1357. data: list,
  1358. success: true,
  1359. });
  1360. };
  1361. request.onerror = function (e) {
  1362. reject({
  1363. code: that.errorCode.get.code,
  1364. msg: that.errorCode.get.msg,
  1365. result: result,
  1366. error: e,
  1367. });
  1368. };
  1369. }
  1370. }, dbName);
  1371. }
  1372. });
  1373. };
  1374. this.getPaging = function (key, offset = 0, count = 1) {
  1375. /* 查询分页(未完成) */
  1376. let that = this;
  1377. return new Promise((resolve, reject) => {
  1378. that.get(key).then((_resolve_) => {
  1379. if (_resolve_["code"] !== 200) {
  1380. resolve(_resolve_);
  1381. }
  1382. resolve();
  1383. }),
  1384. (_reject_) => {
  1385. reject(_reject_);
  1386. };
  1387. });
  1388. };
  1389. this.delete = function (key) {
  1390. let that = this;
  1391. return new Promise((resolve, reject) => {
  1392. /* 根据key删除某条数据 */
  1393. let dbName = that.dbName;
  1394. if (that.indexedDB) {
  1395. that.open(function (result) {
  1396. let error = result.hasOwnProperty("error");
  1397. if (error) {
  1398. resolve(result);
  1399. } else {
  1400. let request = result.get(key);
  1401. request.onsuccess = function (e) {
  1402. let recode = e.target.result;
  1403. if (recode) {
  1404. request = result.delete(key);
  1405. }
  1406. resolve({
  1407. code: recode
  1408. ? that.errorCode.success.code
  1409. : that.errorCode.error.code,
  1410. msg: recode
  1411. ? that.errorCode.success.msg
  1412. : that.errorCode.error.msg,
  1413. success: true,
  1414. });
  1415. };
  1416. request.onerror = function (e) {
  1417. resolve({
  1418. code: that.errorCode.delete.code,
  1419. msg: that.errorCode.delete.msg,
  1420. error: e,
  1421. });
  1422. };
  1423. }
  1424. }, dbName);
  1425. }
  1426. });
  1427. };
  1428. this.deleteAll = function () {
  1429. let that = this;
  1430. return new Promise((resolve, reject) => {
  1431. /* 清空数据库 */
  1432. let dbName = that.dbName;
  1433. if (that.indexedDB) {
  1434. that.open(function (result) {
  1435. let error = result.hasOwnProperty("error");
  1436. if (error) {
  1437. resolve({
  1438. code: that.errorCode.deleteAll.code,
  1439. msg: that.errorCode.deleteAll.msg,
  1440. error: error,
  1441. result: result,
  1442. });
  1443. } else {
  1444. result.clear();
  1445. resolve({
  1446. code: that.errorCode.success.code,
  1447. msg: that.errorCode.success.msg,
  1448. success: true,
  1449. });
  1450. }
  1451. }, dbName);
  1452. }
  1453. });
  1454. };
  1455. };
  1456.  
  1457. /*
  1458. * @description 获取页面中最大的z-index
  1459. * @return {string} - User-Agent
  1460. * @example Utils.getRandomPCUA();
  1461. * @exampleResult Mozilla/5.0....
  1462. */
  1463. Utils.getMaxZIndex = function () {
  1464. let arr = [...document.all].map(
  1465. (e) => +window.getComputedStyle(e).zIndex || 0
  1466. );
  1467. return arr.length ? Math.max(...arr) + 1 : 0;
  1468. };