@andy.ian/dsh-homing 2.3.9 → 2.4.0

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.
@@ -6,7 +6,8 @@
6
6
  * 1. 捕获 window.onerror / unhandledrejection / 资源加载失败 → 立即上报
7
7
  * 2. 捕获 console.error / console.warn 的前 2 条(插件初始化失败常被应用
8
8
  * try/catch 后写 logger,window.onerror 看不到)
9
- * 3. 12 秒后若仍在启动页:先自动换 URL 重载一次,仍不行则显示自救提示
9
+ * 3. 启动检测:只在连续 60 秒毫无加载进展时才提示,不自动重载
10
+ * (首次打开要下载约 3MB 脚本,慢网络下 20–40 秒属正常,别误报)
10
11
  *
11
12
  * 必须是外部文件(页面 CSP 只有 script-src 'self')。
12
13
  */
@@ -67,6 +68,16 @@
67
68
  })
68
69
  })
69
70
 
71
+ // 记录"还有进展":资源加载、DOM 状态变化、console 输出都会刷新它。
72
+ // 首次打开手机端要下载约 3MB(gzip 后)的 DSH 前端脚本,慢 networks 下
73
+ // 需要 20–40 秒;只要还在进展就耐心等,不能当成"卡住"(2026-11-23 实测:
74
+ // 旧的 12 秒检测会误报,还会自动重载把已下载的资源丢掉,反而更慢)。
75
+ let lastProgressAt = Date.now()
76
+ const markProgress = () => { lastProgressAt = Date.now() }
77
+ window.addEventListener('load', markProgress, true)
78
+ window.addEventListener('error', markProgress, true)
79
+ document.addEventListener('readystatechange', markProgress, true)
80
+
70
81
  // 只拦 error / warn,每级 2 条:足够定位插件初始化失败,又不影响性能
71
82
  for (const level of ['error', 'warn']) {
72
83
  const original = console[level]
@@ -74,6 +85,7 @@
74
85
  let reported = 0
75
86
  console[level] = function patched(...args) {
76
87
  try {
88
+ markProgress()
77
89
  if (reported < 2) {
78
90
  reported += 1
79
91
  report({
@@ -90,51 +102,46 @@
90
102
  }
91
103
  }
92
104
 
93
- // 12 秒后检查应用是否挂载:仍在启动页或 #root 为空,
94
- // 先自动换一个带随机参数的 URL 重载一次(破解缓存壳/浏览器进程中的旧状态),
95
- // 再不行才显示自救提示。
96
- setTimeout(() => {
105
+ // 启动检测:12 秒后第一次检查,若仍在启动页:
106
+ // - 最近 60 秒内有任何加载进展 → 继续等(每 10 秒复查),不提示、不重载;
107
+ // - 连续 60 秒毫无进展 → 才上报并显示温和提示。
108
+ // 不再自动换 URL 重载:慢网络下重载会丢弃已经下好的资源,让首次打开更慢。
109
+ const BOOT_IDLE_LIMIT_MS = 60_000
110
+ const BOOT_RECHECK_MS = 10_000
111
+
112
+ function checkBoot() {
97
113
  try {
98
114
  const root = document.getElementById('root')
99
115
  const splash = document.querySelector('[data-dsh-boot]')
100
116
  const mounted = Boolean(root && root.childElementCount > 0) && !splash
101
- if (mounted) return
117
+ if (mounted) return true
118
+ const idleMs = Date.now() - lastProgressAt
119
+ if (idleMs < BOOT_IDLE_LIMIT_MS) {
120
+ setTimeout(checkBoot, BOOT_RECHECK_MS)
121
+ return false
122
+ }
102
123
  report({
103
124
  kind: 'boot-timeout',
104
125
  message: String(document.body?.innerText ?? '').replace(/\s+/g, ' ').slice(0, 400) || '(页面无文字)',
105
126
  rootExists: Boolean(root),
106
127
  splashPresent: Boolean(splash),
107
128
  readyState: document.readyState,
129
+ idleMs,
108
130
  })
109
-
110
- // 自动重载:每次会话只尝试一次,避免死循环
111
- let tried = false
112
- try {
113
- tried = sessionStorage.getItem('dsh-homing-boot-retry') === '1'
114
- if (!tried) sessionStorage.setItem('dsh-homing-boot-retry', '1')
115
- } catch (error) {
116
- tried = true // 拿不到 sessionStorage 就不要再自动刷新
117
- }
118
- if (!tried) {
119
- const url = new URL(location.href)
120
- url.searchParams.set('_r', Date.now().toString(36))
121
- location.replace(url.href)
122
- return
123
- }
124
-
125
131
  showStuckOverlay()
126
132
  } catch (error) {
127
133
  // 忽略
128
134
  }
129
- }, 12000)
135
+ return false
136
+ }
137
+ setTimeout(checkBoot, 12_000)
130
138
 
131
139
  /**
132
- * 启动卡住时给出可见的自救提示。
140
+ * 真正长时间无进展时给出的提示。
133
141
  *
134
- * 为什么需要(2026-11-15 实测):手机浏览器进程会积累坏状态(泄漏的 IndexedDB 连接等),
135
- * 表现就是界面永远停在 "Loading plugins…",而**刷新、重开标签都没用**,
136
- * 必须把浏览器从后台彻底关掉再打开才恢复。用户当时试了很多次都不知道这一点,
137
- * 所以卡住时直接把这句话显示出来,配一个重新加载按钮。
142
+ * 注意措辞(2026-11-23 修正):首次打开要下载 DSH 前端约 3MB 脚本,慢网络
143
+ * 本来就要几十秒,不能一上来就让人"彻底关浏览器";只有在确认毫无进展时才
144
+ * 给出自救建议,且把"重新加载"作为首选而不是自动执行。
138
145
  */
139
146
  function showStuckOverlay() {
140
147
  if (document.getElementById('dsh-homing-stuck')) return
@@ -145,12 +152,12 @@
145
152
  + 'padding:14px;font:14px/1.6 system-ui,-apple-system,"Noto Sans SC",sans-serif;'
146
153
  + 'box-shadow:0 8px 28px rgba(0,0,0,.5)'
147
154
  const title = document.createElement('div')
148
- title.textContent = 'DSH 启动卡住了'
155
+ title.textContent = '页面加载较慢或暂时没有进展'
149
156
  title.style.cssText = 'font-weight:700;margin-bottom:6px'
150
157
  const hint = document.createElement('div')
151
158
  hint.style.cssText = 'color:#b7bec9;margin-bottom:10px'
152
- hint.textContent = '请在手机的多任务里把这个浏览器彻底关掉(不是切后台),再重新打开。'
153
- + '今天多次刷新都无效、只有彻底关掉浏览器才恢复,原因通常是浏览器进程里的旧状态没被清掉。'
159
+ hint.textContent = '首次打开需要下载约 3MB 的页面资源,网络慢时可能需要 30–60 秒。'
160
+ + '如果一直不动,可先点「重新加载」;仍不行再在手机多任务里彻底关闭浏览器后重开。'
154
161
  const row = document.createElement('div')
155
162
  row.style.cssText = 'display:flex;gap:8px'
156
163
  const reload = document.createElement('button')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andy.ian/dsh-homing",
3
- "version": "2.3.9",
3
+ "version": "2.4.0",
4
4
  "type": "module",
5
5
  "description": "把本机 DSH 通过 SSH 反向隧道安全开放到手机浏览器:TLS 端到端(服务器只透传)、账号 + TOTP、局域网与公网双入口,随 DSH 启动。",
6
6
  "keywords": [