@sunshine-star/dsh-access-pc 0.1.7 → 0.1.9
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/main.js +41 -12
- package/package.json +1 -1
- package/scripts/shortcut.js +15 -0
- package/start-preload.js +1 -0
- package/start.html +2 -0
package/main.js
CHANGED
|
@@ -22,6 +22,7 @@ const DEFAULT_WINDOW = '1440,900'
|
|
|
22
22
|
// 客户端只渲染一个网页,软件渲染足够,先关掉更稳。
|
|
23
23
|
app.disableHardwareAcceleration()
|
|
24
24
|
if (process.platform === 'win32') app.setAppUserModelId('com.sunshine-star.dsh-access')
|
|
25
|
+
app.setName('dsh-access-pc')
|
|
25
26
|
|
|
26
27
|
/** 解析 --key value 形式的参数。 */
|
|
27
28
|
function parseArgs(argv) {
|
|
@@ -132,9 +133,10 @@ if (args.version) {
|
|
|
132
133
|
|
|
133
134
|
const hubPrefix = () => cfg.hub.replace(/\/$/, '')
|
|
134
135
|
const singleWindow = cfg.singleWindow === true
|
|
135
|
-
const showStart =
|
|
136
|
+
const showStart = error => {
|
|
136
137
|
if (!mainWindow || mainWindow.isDestroyed()) return
|
|
137
|
-
|
|
138
|
+
const options = error ? { query: { error: String(error).slice(0, 200) } } : undefined
|
|
139
|
+
void mainWindow.loadFile(path.join(__dirname, 'start.html'), options)
|
|
138
140
|
}
|
|
139
141
|
|
|
140
142
|
const fromStartPage = event => String(event.senderFrame?.url ?? '').startsWith('file://')
|
|
@@ -142,6 +144,7 @@ if (args.version) {
|
|
|
142
144
|
if (!fromStartPage(event)) throw new Error('forbidden')
|
|
143
145
|
return cfg.hub
|
|
144
146
|
})
|
|
147
|
+
ipcMain.on('app:show-start', () => showStart())
|
|
145
148
|
ipcMain.handle('start:connect', (event, value) => {
|
|
146
149
|
if (!fromStartPage(event)) throw new Error('forbidden')
|
|
147
150
|
const hub = String(value ?? '').trim().replace(/\/$/, '')
|
|
@@ -185,6 +188,31 @@ if (args.version) {
|
|
|
185
188
|
})()`, true).catch(() => {})
|
|
186
189
|
}
|
|
187
190
|
|
|
191
|
+
const LOGIN_BACK_ID = '__dsh_connect_back'
|
|
192
|
+
const installLoginBackButton = contents => {
|
|
193
|
+
let url
|
|
194
|
+
try { url = new URL(contents.getURL()) } catch { return }
|
|
195
|
+
if (url.pathname !== '/login' && url.pathname !== '/login/') return
|
|
196
|
+
contents.executeJavaScript(`(() => {
|
|
197
|
+
if (document.getElementById('${LOGIN_BACK_ID}')) return
|
|
198
|
+
const button = document.createElement('button')
|
|
199
|
+
button.id = '${LOGIN_BACK_ID}'
|
|
200
|
+
button.type = 'button'
|
|
201
|
+
button.title = '返回连接页'
|
|
202
|
+
button.textContent = String.fromCharCode(8592)
|
|
203
|
+
Object.assign(button.style, {
|
|
204
|
+
position: 'fixed', left: '10px', top: '50%', transform: 'translateY(-50%)',
|
|
205
|
+
width: '38px', height: '38px', borderRadius: '19px',
|
|
206
|
+
border: '1px solid rgba(124,156,255,.55)', background: 'rgba(20,27,49,.88)',
|
|
207
|
+
color: '#e8ecf7', fontSize: '18px', lineHeight: '1', cursor: 'pointer',
|
|
208
|
+
zIndex: '2147483647', boxShadow: '0 6px 20px rgba(0,0,0,.35)',
|
|
209
|
+
backdropFilter: 'blur(6px)',
|
|
210
|
+
})
|
|
211
|
+
button.addEventListener('click', () => { window.start?.back?.() })
|
|
212
|
+
document.body.appendChild(button)
|
|
213
|
+
})()`, true).catch(() => {})
|
|
214
|
+
}
|
|
215
|
+
|
|
188
216
|
app.on('web-contents-created', (_event, contents) => {
|
|
189
217
|
// 右键菜单:导航、链接、编辑、检查元素。
|
|
190
218
|
contents.on('context-menu', (_event, params) => {
|
|
@@ -229,8 +257,8 @@ if (args.version) {
|
|
|
229
257
|
Menu.buildFromTemplate(items).popup({ window })
|
|
230
258
|
})
|
|
231
259
|
|
|
232
|
-
contents.on('did-finish-load', () => installHostBackButton(contents))
|
|
233
|
-
contents.on('did-navigate-in-page', () => installHostBackButton(contents))
|
|
260
|
+
contents.on('did-finish-load', () => { installHostBackButton(contents); installLoginBackButton(contents) })
|
|
261
|
+
contents.on('did-navigate-in-page', () => { installHostBackButton(contents); installLoginBackButton(contents) })
|
|
234
262
|
// 单窗口模式下没有浏览器工具栏,Alt+← 用来回主机列表。
|
|
235
263
|
contents.on('before-input-event', (event, input) => {
|
|
236
264
|
if (input.type !== 'keyDown') return
|
|
@@ -288,17 +316,18 @@ if (args.version) {
|
|
|
288
316
|
})
|
|
289
317
|
|
|
290
318
|
// 加载失败时给出可读提示,而不是白屏。
|
|
291
|
-
mainWindow.webContents.on('did-fail-load', (_event, code, description, url) => {
|
|
319
|
+
mainWindow.webContents.on('did-fail-load', (_event, code, description, url, isMainFrame) => {
|
|
320
|
+
if (isMainFrame === false) return
|
|
321
|
+
if (code === -3) return // 主动导航取消
|
|
322
|
+
// 连接失败:先回到连接页(可编辑地址),再弹提示。
|
|
323
|
+
showStart(`${description}(${code})`)
|
|
292
324
|
void dialog.showMessageBox(mainWindow, {
|
|
293
325
|
type: 'error',
|
|
294
|
-
title: '
|
|
295
|
-
message:
|
|
296
|
-
detail: `${url}\n\n
|
|
297
|
-
buttons: ['
|
|
326
|
+
title: '无法连接 Hub',
|
|
327
|
+
message: `连接失败:${description}(${code})`,
|
|
328
|
+
detail: `${url}\n\n请检查地址与网络,修改后重试。`,
|
|
329
|
+
buttons: ['返回连接页'],
|
|
298
330
|
defaultId: 0,
|
|
299
|
-
cancelId: 1,
|
|
300
|
-
}).then(result => {
|
|
301
|
-
if (result.response === 0) showStart()
|
|
302
331
|
})
|
|
303
332
|
console.error(`[${APP_NAME}] 加载失败 ${code} ${description} ${url}`)
|
|
304
333
|
})
|
package/package.json
CHANGED
package/scripts/shortcut.js
CHANGED
|
@@ -94,10 +94,19 @@ StartupWMClass=dsh-access-pc
|
|
|
94
94
|
`
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
+
function markTrusted(file) {
|
|
98
|
+
try {
|
|
99
|
+
spawnSync('gio', ['set', file, 'metadata::trusted', 'true'], { stdio: 'ignore' })
|
|
100
|
+
} catch {
|
|
101
|
+
// 没有 gio 或非 GNOME 环境,忽略。
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
97
105
|
function createLinuxEntry(targetPath) {
|
|
98
106
|
fs.mkdirSync(path.dirname(targetPath), { recursive: true })
|
|
99
107
|
fs.writeFileSync(targetPath, desktopEntry())
|
|
100
108
|
fs.chmodSync(targetPath, 0o755)
|
|
109
|
+
markTrusted(targetPath)
|
|
101
110
|
return targetPath
|
|
102
111
|
}
|
|
103
112
|
|
|
@@ -116,11 +125,17 @@ function createShortcuts({ desktop = true, menu = true } = {}) {
|
|
|
116
125
|
const base = path.join(os.homedir(), '.local', 'share', 'applications')
|
|
117
126
|
const entry = path.join(base, 'dsh-access-pc.desktop')
|
|
118
127
|
created.push(createLinuxEntry(entry))
|
|
128
|
+
try {
|
|
129
|
+
spawnSync('update-desktop-database', [base], { stdio: 'ignore' })
|
|
130
|
+
} catch {
|
|
131
|
+
// 忽略
|
|
132
|
+
}
|
|
119
133
|
if (desktop) {
|
|
120
134
|
const desktopPath = path.join(desktopDir(), 'dsh-access-pc.desktop')
|
|
121
135
|
try {
|
|
122
136
|
fs.copyFileSync(entry, desktopPath)
|
|
123
137
|
fs.chmodSync(desktopPath, 0o755)
|
|
138
|
+
markTrusted(desktopPath)
|
|
124
139
|
created.push(desktopPath)
|
|
125
140
|
} catch {
|
|
126
141
|
// 没有桌面目录就只留应用菜单项。
|
package/start-preload.js
CHANGED
package/start.html
CHANGED
|
@@ -57,6 +57,8 @@
|
|
|
57
57
|
const input = document.getElementById('hub')
|
|
58
58
|
const err = document.getElementById('err')
|
|
59
59
|
const button = document.getElementById('connect')
|
|
60
|
+
const failed = new URLSearchParams(location.search).get('error')
|
|
61
|
+
if (failed) err.textContent = '连接失败:' + failed
|
|
60
62
|
window.start.get().then(value => {
|
|
61
63
|
input.value = value || ''
|
|
62
64
|
input.focus()
|