@andy.ian/dsh-homing 2.1.15 → 2.2.2
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.
- package/README.md +151 -110
- package/SECURITY.md +3 -0
- package/bin/restart-daemon.mjs +0 -0
- package/bin/restart.mjs +0 -0
- package/index.js +200 -301
- package/lib/app-proxy.mjs +262 -0
- package/lib/csrf.mjs +38 -0
- package/lib/local-apps-page.mjs +64 -0
- package/lib/local-apps.mjs +270 -0
- package/lib/mobile-patch.mjs +321 -0
- package/lib/nav.mjs +1 -0
- package/lib/vendor/vnc.bundle.js +189 -73
- package/lib/vnc-app.js +112 -23
- package/lib/vnc-boot.js +1 -1
- package/lib/vnc-page.mjs +34 -4
- package/lib/vnc-probe.mjs +48 -0
- package/lib/window-follow.mjs +66 -0
- package/package.json +2 -2
- package/permissions.json +7 -0
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 手机端补丁:index.js 把它注入到 DSH 页面(/dsh_homing/mobile.js、mobile.css)。
|
|
3
|
+
*
|
|
4
|
+
* 只做「手机上明显做不到、或做了也白做」的事:
|
|
5
|
+
* - 页头「文件夹 + 小箭头」分体按钮 = 在 DSH 那台电脑上打开工作区目录。
|
|
6
|
+
* 你人不在那台电脑前,手机上点它只会收到「已在其它设备打开」,什么也看不到;
|
|
7
|
+
* 这里统一接到本插件的文件页 /files。
|
|
8
|
+
* - 应用菜单里只能在电脑上启动的项(资源管理器 / VS Code / Git Bash / 终端)
|
|
9
|
+
* 同样接到对应网页(文件页 / 终端页)。
|
|
10
|
+
* - 窄屏自动收起右侧栏、记住最后点过的工作区、上报活跃信标。
|
|
11
|
+
*
|
|
12
|
+
* 定位按钮不靠易变的 class 名:优先用 DSH 的稳定标记 data-open-target,
|
|
13
|
+
* 老版本没有标记时退回结构判断。细节见下面 splitHost 的注释。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export const MOBILE_CSS = `html { -webkit-text-size-adjust: 100%; }
|
|
17
|
+
@media (max-width: 760px) {
|
|
18
|
+
[class*="_frame"] { min-width: 0; }
|
|
19
|
+
}
|
|
20
|
+
`;
|
|
21
|
+
|
|
22
|
+
export const MOBILE_JS = String.raw`(() => {
|
|
23
|
+
const narrow = () => window.innerWidth <= 760
|
|
24
|
+
// 记住最后点过的工作区:切换工作区必然有一次点击,这是最可靠的信号。
|
|
25
|
+
// 存的只是名字,由服务端映射成路径;对不上就退回默认工作区。
|
|
26
|
+
const LAST_WS_KEY = 'dsh-homing:last-workspace'
|
|
27
|
+
document.addEventListener('click', (event) => {
|
|
28
|
+
const node = event.target
|
|
29
|
+
if (node === null || typeof node.closest !== 'function') return
|
|
30
|
+
const item = node.closest('[role="menuitem"]')
|
|
31
|
+
const side = node.closest('[class*="sidebarCol"]')
|
|
32
|
+
const candidate = item ?? (side === null ? null : node.closest('button, [role="treeitem"], [class*="session"], [class*="item"], div'))
|
|
33
|
+
if (candidate === null) return
|
|
34
|
+
const text = (candidate.textContent || '').replace(/\s+/g, ' ').trim()
|
|
35
|
+
if (text === '' || text.length > 40) return
|
|
36
|
+
// 排除操作类菜单项,只记看起来像工作区名字的
|
|
37
|
+
if (/文件资源管理器|VS Code|Git Bash|添加工作区|新会话|搜索|设置|插件|自动化任务|更多|账号|退出/u.test(text)) return
|
|
38
|
+
try { window.localStorage.setItem(LAST_WS_KEY, text) } catch { /* 隐私模式 */ }
|
|
39
|
+
}, true)
|
|
40
|
+
|
|
41
|
+
// 当前会话标题:从标题栏(页面顶部、class 里带 title/tab/header 的元素)取最长的文本。
|
|
42
|
+
// 用它去反查会话记录里的 cwd,就能打开"当前会话所属工作区"的目录。
|
|
43
|
+
const currentSessionTitle = () => {
|
|
44
|
+
let best = ''
|
|
45
|
+
for (const node of document.querySelectorAll('div, span, h1, h2, header')) {
|
|
46
|
+
const rect = node.getBoundingClientRect()
|
|
47
|
+
if (rect.width === 0 || rect.height === 0) continue
|
|
48
|
+
if (rect.top > 120 || rect.height > 44) continue
|
|
49
|
+
const cls = String(node.className || '')
|
|
50
|
+
if (!/title|tab|header|crumb|label/iu.test(cls)) continue
|
|
51
|
+
const text = (node.textContent || '').trim()
|
|
52
|
+
if (text.length < 2 || text.length > 60) continue
|
|
53
|
+
if (text.length > best.length) best = text
|
|
54
|
+
}
|
|
55
|
+
return best
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 应用把"当前会话"存在 localStorage 里,直接读它 —— 这是确定值,不靠猜。
|
|
59
|
+
const currentSessionId = () => {
|
|
60
|
+
try {
|
|
61
|
+
const raw = window.localStorage.getItem('dsh.sessions.current')
|
|
62
|
+
if (raw === null) return ''
|
|
63
|
+
const parsed = JSON.parse(raw)
|
|
64
|
+
const id = parsed && typeof parsed.sessionId === 'string' ? parsed.sessionId : ''
|
|
65
|
+
return /^session-[A-Za-z0-9-]+$/u.test(id) ? id : ''
|
|
66
|
+
} catch {
|
|
67
|
+
return ''
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const goFiles = () => {
|
|
72
|
+
const session = currentSessionId()
|
|
73
|
+
if (session !== '') {
|
|
74
|
+
window.location.href = '/files?session=' + encodeURIComponent(session)
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
let hint = ''
|
|
78
|
+
try { hint = currentSessionTitle() || window.localStorage.getItem(LAST_WS_KEY) || '' } catch { hint = '' }
|
|
79
|
+
window.location.href = '/files' + (hint === '' ? '' : '?hint=' + encodeURIComponent(hint))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/* ---------- 页头「文件夹 + 小箭头」分体按钮 ----------
|
|
83
|
+
* 它做的事是在 DSH 所在的电脑上启动程序(资源管理器 / VS Code / Git Bash)。
|
|
84
|
+
* 手机上没有那台电脑的屏幕,点了只会收到「已在其它设备打开」——纯噪音。
|
|
85
|
+
* 这里把它(主按钮和小箭头都算)接到文件页 /files。
|
|
86
|
+
*
|
|
87
|
+
* 定位必须稳:页头里还有子智能体面板、更多菜单等 aria-haspopup 控件,而且
|
|
88
|
+
* 它们排在分体按钮前面。「找第一个 aria-haspopup 的父容器」会认成子智能体
|
|
89
|
+
* 那个容器,于是点分体按钮时判定不命中、补丁失效(手机端历史 bug)。
|
|
90
|
+
* 所以:
|
|
91
|
+
* 1. 优先用 DSH 的稳定标记 data-open-target="directory"(分体按钮容器);
|
|
92
|
+
* 2. 老版本没有标记时退回结构判断 —— 同一容器里恰好两个按钮,一个带
|
|
93
|
+
* aria-haspopup="menu"(小箭头),另一个是纯图标主按钮(里面是应用图标)。
|
|
94
|
+
*/
|
|
95
|
+
const splitHost = (node) => {
|
|
96
|
+
if (node === null || node === undefined || typeof node.closest !== 'function') return null
|
|
97
|
+
const marked = node.closest('[data-open-target="directory"]')
|
|
98
|
+
if (marked !== null) return marked
|
|
99
|
+
const button = node.closest('button, [role="button"]')
|
|
100
|
+
if (button === null) return null
|
|
101
|
+
const host = button.parentElement
|
|
102
|
+
if (host === null) return null
|
|
103
|
+
const buttons = host.querySelectorAll('button, [role="button"]')
|
|
104
|
+
if (buttons.length !== 2) return null
|
|
105
|
+
let main = null
|
|
106
|
+
let chevron = null
|
|
107
|
+
for (const item of buttons) {
|
|
108
|
+
if (item.hasAttribute('aria-haspopup')) chevron = item
|
|
109
|
+
else main = item
|
|
110
|
+
}
|
|
111
|
+
if (main === null || chevron === null) return null
|
|
112
|
+
if (chevron.getAttribute('aria-haspopup') !== 'menu') return null
|
|
113
|
+
if ((main.textContent || '').trim() !== '') return null
|
|
114
|
+
if (main.querySelector('img, svg') === null) return null
|
|
115
|
+
const rect = chevron.getBoundingClientRect()
|
|
116
|
+
if (rect.width === 0 || rect.height === 0) return null
|
|
117
|
+
if (rect.top > 200) return null
|
|
118
|
+
return host
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
document.addEventListener('click', (event) => {
|
|
122
|
+
const node = event.target
|
|
123
|
+
if (node === null || typeof node.closest !== 'function') return
|
|
124
|
+
const host = splitHost(node)
|
|
125
|
+
if (host === null) return
|
|
126
|
+
event.preventDefault()
|
|
127
|
+
event.stopPropagation()
|
|
128
|
+
goFiles()
|
|
129
|
+
}, true)
|
|
130
|
+
|
|
131
|
+
/* ---------- 只能在本机启动的菜单项 ----------
|
|
132
|
+
* 应用下拉里的「文件资源管理器 / VS Code / Git Bash / 终端」全是在 DSH 那台
|
|
133
|
+
* 电脑上启动桌面程序。这里接到对应网页:文件类 → /files,终端类 → /term。
|
|
134
|
+
* 只在 role=menuitem / role=option 上生效,避免误伤正文里的同名文字。
|
|
135
|
+
*/
|
|
136
|
+
const openFilesFromMenu = (item) => {
|
|
137
|
+
// 尽量从属性里挖工作区路径,挖不到就让文件页自己决定默认目录
|
|
138
|
+
let target = ''
|
|
139
|
+
for (const holder of [item, ...(item.querySelectorAll('[data-path], [title]') || [])]) {
|
|
140
|
+
if (holder === null) continue
|
|
141
|
+
const dataPath = holder.getAttribute && holder.getAttribute('data-path')
|
|
142
|
+
const title = holder.getAttribute && holder.getAttribute('title')
|
|
143
|
+
if (dataPath) { target = dataPath; break }
|
|
144
|
+
if (title && /^[A-Za-z]:[\\/]|^\//u.test(title)) { target = title; break }
|
|
145
|
+
}
|
|
146
|
+
// 带上最后选中的工作区名,服务端会映射成路径;映射不到就落到默认工作区
|
|
147
|
+
let remembered = ''
|
|
148
|
+
try { remembered = window.localStorage.getItem(LAST_WS_KEY) || '' } catch { /* 隐私模式 */ }
|
|
149
|
+
const selected = remembered !== '' ? remembered : (() => {
|
|
150
|
+
const side = document.querySelector('[class*="sidebarCol"]')
|
|
151
|
+
if (side === null) return ''
|
|
152
|
+
let best = ''
|
|
153
|
+
for (const node of side.querySelectorAll('[class*="selected"], [class*="active"], [aria-current]')) {
|
|
154
|
+
const text = (node.textContent || '').trim()
|
|
155
|
+
if (text === '' || text.length > 40) continue
|
|
156
|
+
if (best === '' || text.length < best.length) best = text
|
|
157
|
+
}
|
|
158
|
+
return best
|
|
159
|
+
})()
|
|
160
|
+
const session = currentSessionId()
|
|
161
|
+
const query = target ? '?path=' + encodeURIComponent(target)
|
|
162
|
+
: session !== '' ? '?session=' + encodeURIComponent(session)
|
|
163
|
+
: '?hint=' + encodeURIComponent(currentSessionTitle() || selected || '')
|
|
164
|
+
window.location.href = '/files' + query
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const openTermFromMenu = () => {
|
|
168
|
+
const session = currentSessionId()
|
|
169
|
+
window.location.href = '/term' + (session === '' ? '' : '?session=' + encodeURIComponent(session))
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
document.addEventListener('click', (event) => {
|
|
173
|
+
const node = event.target
|
|
174
|
+
if (node === null || typeof node.closest !== 'function') return
|
|
175
|
+
const item = node.closest('[role="menuitem"], [role="option"]')
|
|
176
|
+
if (item === null) return
|
|
177
|
+
const text = (item.textContent || '').replace(/\s+/g, ' ').trim()
|
|
178
|
+
if (text === '' || text.length > 60) return
|
|
179
|
+
if (/文件资源管理器|文件管理器|访达|显示文件位置|在文件夹显示|在访达显示|VS Code|Visual Studio Code/u.test(text)) {
|
|
180
|
+
event.preventDefault()
|
|
181
|
+
event.stopPropagation()
|
|
182
|
+
openFilesFromMenu(item)
|
|
183
|
+
return
|
|
184
|
+
}
|
|
185
|
+
if (/Git Bash|终端|Terminal|PowerShell|命令提示符/u.test(text)) {
|
|
186
|
+
event.preventDefault()
|
|
187
|
+
event.stopPropagation()
|
|
188
|
+
openTermFromMenu()
|
|
189
|
+
}
|
|
190
|
+
}, true)
|
|
191
|
+
|
|
192
|
+
let lastRight = 0
|
|
193
|
+
const closeRightBar = () => {
|
|
194
|
+
if (!narrow()) return
|
|
195
|
+
if (document.querySelector('[data-rightbar-collapsed="false"]') === null) return
|
|
196
|
+
if (Date.now() - lastRight < 2500) return
|
|
197
|
+
const button = document.querySelector('[aria-label="收起右侧边栏"]')
|
|
198
|
+
if (button === null) return
|
|
199
|
+
lastRight = Date.now()
|
|
200
|
+
button.click()
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const start = () => {
|
|
204
|
+
closeRightBar()
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/* ---------- SESSION_MENU_ITEMS ----------
|
|
208
|
+
* 在 DSH 会话「…」菜单里追加两项:
|
|
209
|
+
* - 下载工作区文件:跳到文件页(可逐个文件下载)
|
|
210
|
+
* - 重启 DSH / 守护进程:跳到重启页
|
|
211
|
+
* 做法:复制一个原生菜单项(连同图标容器与样式),只替换文字和图标路径;
|
|
212
|
+
* 每轮先清掉旧项,并全局 prune,保证每项最多一个。
|
|
213
|
+
*/
|
|
214
|
+
const MENU_HINT = /下载 Session 日志|复制会话为 Markdown|反馈/u
|
|
215
|
+
const ICON_RESTART = '<path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/>'
|
|
216
|
+
const ICON_DOWNLOAD = '<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><path d="M7 10l5 5 5-5"/><path d="M12 15V3"/>'
|
|
217
|
+
let menuTimer = null
|
|
218
|
+
|
|
219
|
+
const textNodeMatching = (node, pattern) => {
|
|
220
|
+
const walker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT)
|
|
221
|
+
for (let current = walker.nextNode(); current !== null; current = walker.nextNode()) {
|
|
222
|
+
if (pattern.test(current.nodeValue || '')) return current
|
|
223
|
+
}
|
|
224
|
+
return null
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const buildMenuItem = (template, label, icon, action) => {
|
|
228
|
+
const link = template.cloneNode(true)
|
|
229
|
+
link.setAttribute('role', 'menuitem')
|
|
230
|
+
link.setAttribute('href', '#')
|
|
231
|
+
link.style.textDecoration = 'none'
|
|
232
|
+
const textNode = textNodeMatching(link, /反馈|下载 Session 日志|复制会话为 Markdown/u)
|
|
233
|
+
if (textNode !== null) textNode.nodeValue = label
|
|
234
|
+
else link.textContent = label
|
|
235
|
+
const svg = link.querySelector('svg')
|
|
236
|
+
if (svg !== null) {
|
|
237
|
+
svg.innerHTML = icon
|
|
238
|
+
svg.setAttribute('viewBox', '0 0 24 24')
|
|
239
|
+
svg.setAttribute('fill', 'none')
|
|
240
|
+
svg.setAttribute('stroke', 'currentColor')
|
|
241
|
+
svg.setAttribute('stroke-width', '2')
|
|
242
|
+
svg.setAttribute('stroke-linecap', 'round')
|
|
243
|
+
svg.setAttribute('stroke-linejoin', 'round')
|
|
244
|
+
}
|
|
245
|
+
link.addEventListener('click', (event) => {
|
|
246
|
+
event.preventDefault()
|
|
247
|
+
event.stopPropagation()
|
|
248
|
+
action()
|
|
249
|
+
}, true)
|
|
250
|
+
return link
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const enhanceMenus = () => {
|
|
254
|
+
const done = new Set()
|
|
255
|
+
for (const item of document.querySelectorAll('[role="menuitem"]')) {
|
|
256
|
+
const menu = item.closest('[role="menu"]') ?? item.parentElement
|
|
257
|
+
if (menu === null || done.has(menu)) continue
|
|
258
|
+
if (!MENU_HINT.test(menu.textContent || '')) continue
|
|
259
|
+
done.add(menu)
|
|
260
|
+
|
|
261
|
+
for (const old of menu.querySelectorAll('[data-dsh-homing-download],[data-dsh-homing-restart]')) old.remove()
|
|
262
|
+
|
|
263
|
+
const items = [...menu.querySelectorAll('[role="menuitem"]')]
|
|
264
|
+
const template = items.find((node) => node.querySelector('svg')) ?? items[0]
|
|
265
|
+
if (!template) continue
|
|
266
|
+
|
|
267
|
+
const download = buildMenuItem(template, '下载工作区文件', ICON_DOWNLOAD, () => goFiles())
|
|
268
|
+
download.setAttribute('data-dsh-homing-download', '1')
|
|
269
|
+
const restart = buildMenuItem(template, '重启 DSH / 守护进程', ICON_RESTART, () => window.location.assign('/dsh_homing/restart'))
|
|
270
|
+
restart.setAttribute('data-dsh-homing-restart', '1')
|
|
271
|
+
|
|
272
|
+
menu.appendChild(download)
|
|
273
|
+
menu.appendChild(restart)
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// 兜底:无论菜单容器怎么嵌套,每类最多保留一个
|
|
278
|
+
const pruneMenuItems = () => {
|
|
279
|
+
for (const selector of ['[data-dsh-homing-download]', '[data-dsh-homing-restart]']) {
|
|
280
|
+
const all = [...document.querySelectorAll(selector)]
|
|
281
|
+
for (let i = 0; i < all.length - 1; i += 1) all[i].remove()
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const menuObserver = new MutationObserver(() => {
|
|
286
|
+
if (menuTimer) return
|
|
287
|
+
menuTimer = setTimeout(() => { menuTimer = null; enhanceMenus(); pruneMenuItems() }, 80)
|
|
288
|
+
})
|
|
289
|
+
menuObserver.observe(document.body, { childList: true, subtree: true })
|
|
290
|
+
|
|
291
|
+
/* ---------- ALIVE_BEACON ----------
|
|
292
|
+
* 告诉插件"用户正在操作":重启脚本据此判断能否安全重启。
|
|
293
|
+
* 手机上打字/滑动手势不会修改会话文件,只有这个信标能反映"人在用"。
|
|
294
|
+
* 节流 2 秒,页面隐藏时不发(后台标签页不算活跃)。
|
|
295
|
+
*/
|
|
296
|
+
const ALIVE_INTERVAL = 2000
|
|
297
|
+
let aliveAt = 0
|
|
298
|
+
const alive = () => {
|
|
299
|
+
const now = Date.now()
|
|
300
|
+
if (now - aliveAt < ALIVE_INTERVAL) return
|
|
301
|
+
aliveAt = now
|
|
302
|
+
try {
|
|
303
|
+
if (document.visibilityState === 'hidden') return
|
|
304
|
+
const body = JSON.stringify({ at: now, path: location.pathname })
|
|
305
|
+
if (navigator.sendBeacon) navigator.sendBeacon('/dsh_homing/alive', body)
|
|
306
|
+
else fetch('/dsh_homing/alive', { method: 'POST', headers: { 'content-type': 'application/json' }, body, keepalive: true }).catch(() => {})
|
|
307
|
+
} catch (error) {
|
|
308
|
+
/* 忽略 */
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
document.addEventListener('keydown', alive, true)
|
|
312
|
+
document.addEventListener('pointerdown', alive, true)
|
|
313
|
+
document.addEventListener('input', alive, true)
|
|
314
|
+
document.addEventListener('compositionstart', alive, true)
|
|
315
|
+
window.addEventListener('focus', alive, true)
|
|
316
|
+
|
|
317
|
+
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start)
|
|
318
|
+
else start()
|
|
319
|
+
})();
|
|
320
|
+
|
|
321
|
+
`;
|