xctc-utils 1.6.75 → 1.6.77

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1,2 @@
1
+ import 'whatwg-fetch';
1
2
  export declare function autoUpdateLastVersion(duration?: number, toast?: boolean): void;
@@ -1,20 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.autoUpdateLastVersion = void 0;
4
+ require("whatwg-fetch");
4
5
  let autoUpdateLastSrcs;
5
6
  let autoUpdateScriptReg = /\<script.*src=["'](?<src>[^"']+)/gm;
6
7
  let autoUpdateDuration = 5000;
7
8
  let autoUpdateToast = false;
8
9
  let autoUpdateStatus = true;
9
10
  async function getNewScripts() {
10
- const html = await fetch("/").then(res => res.text());
11
- autoUpdateScriptReg['lastIndex'] = 0;
12
- let result = [];
13
- let match;
14
- while ((match = autoUpdateScriptReg.exec(html))) {
15
- result.push(match.groups.src);
11
+ if (typeof fetch === 'function') {
12
+ console.log('Browser supports fetch.');
13
+ const html = await fetch("/").then(res => res.text());
14
+ autoUpdateScriptReg['lastIndex'] = 0;
15
+ let result = [];
16
+ let match;
17
+ while ((match = autoUpdateScriptReg.exec(html))) {
18
+ result.push(match.groups.src);
19
+ }
20
+ return result;
21
+ }
22
+ else {
23
+ console.log('Browser does not support fetch.');
24
+ return [];
16
25
  }
17
- return result;
18
26
  }
19
27
  async function needUpdate() {
20
28
  const newScripts = await getNewScripts();
@@ -36,6 +44,7 @@ async function needUpdate() {
36
44
  return result;
37
45
  }
38
46
  function autoRefresh() {
47
+ // 发起循环检测
39
48
  setTimeout(async () => {
40
49
  const willUpdate = await needUpdate();
41
50
  if (willUpdate) {
@@ -54,6 +63,16 @@ function autoUpdateLastVersion(duration, toast) {
54
63
  if (typeof toast === 'boolean') {
55
64
  autoUpdateToast = toast;
56
65
  }
57
- autoRefresh();
66
+ // 先默认检测一遍
67
+ setTimeout(async () => {
68
+ const willUpdate = await needUpdate();
69
+ if (willUpdate) {
70
+ if (autoUpdateToast) {
71
+ alert('有新的版本更新');
72
+ }
73
+ location.reload();
74
+ }
75
+ autoRefresh();
76
+ }, 200);
58
77
  }
59
78
  exports.autoUpdateLastVersion = autoUpdateLastVersion;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "xctc-utils",
3
3
  "private": false,
4
- "version": "1.6.75",
4
+ "version": "1.6.77",
5
5
  "description": "localStorage存储\r ```\r sessionStorage存储\r ```\r crypto-js加密、解密\r ```\r 微信授权登录、微信分享\r ```\r 设备环境获取\r ```\r 是否是微信浏览器\r ```\r 时间戳转时间,字符串转时间戳",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -19,7 +19,8 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "crypto-js": "^4.2.0",
22
- "weixin-js-sdk": "^1.6.5"
22
+ "weixin-js-sdk": "^1.6.5",
23
+ "whatwg-fetch": "^3.6.20"
23
24
  },
24
25
  "repository": {
25
26
  "type": "git",