@inkbite/shotink 0.0.0-stage → 1.4.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/LICENSE +21 -0
- package/README.md +108 -2
- package/README.zh.md +109 -0
- package/capture.ps1 +2266 -0
- package/client.js +791 -0
- package/cordis.patch.yml +3 -0
- package/icon.svg +4 -0
- package/index.js +565 -0
- package/locale/en.json +13 -0
- package/locale/zh.json +13 -0
- package/package.json +63 -6
package/cordis.patch.yml
ADDED
package/icon.svg
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36" width="36" height="36">
|
|
2
|
+
<path d="M9.15 12.75H13.5Q18 7.65 22.5 12.75H26.85A3.75 3.75 0 0 1 30.6 16.5V26.85A3.75 3.75 0 0 1 26.85 30.6H9.15A3.75 3.75 0 0 1 5.4 26.85V16.5A3.75 3.75 0 0 1 9.15 12.75Z" fill="none" stroke="#3b7dd8" stroke-width="3" stroke-linejoin="round" stroke-linecap="round"/>
|
|
3
|
+
<circle cx="18" cy="21.6" r="5.25" fill="none" stroke="#3b7dd8" stroke-width="3"/>
|
|
4
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,565 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import { appendFile, readFile, mkdir, rename, unlink } from 'node:fs/promises'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { basename, dirname, join } from 'node:path'
|
|
5
|
+
import { fileURLToPath } from 'node:url'
|
|
6
|
+
|
|
7
|
+
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
8
|
+
const CAPTURE_SCRIPT = join(HERE, 'capture.ps1')
|
|
9
|
+
const CAPTURE_ROUTE = '/api/dsh-screenshot/capture'
|
|
10
|
+
// The browser half reports its registration outcome here; the plugin has no
|
|
11
|
+
// visible error surface, and a silent failure looks exactly like "no rows".
|
|
12
|
+
const DIAG_ROUTE = '/api/dsh-screenshot/diag'
|
|
13
|
+
const DIAG_FILE = join(tmpdir(), 'dsh-screenshot-client.log')
|
|
14
|
+
const OUT_DIR = join(tmpdir(), 'dsh-screenshot-tool')
|
|
15
|
+
// Screenshots taken with the global hotkey have no HTTP request waiting for
|
|
16
|
+
// them, so they wait here until the renderer polls for them.
|
|
17
|
+
const INBOX_DIR = join(tmpdir(), 'dsh-screenshot-inbox')
|
|
18
|
+
const HOTKEY_ROUTE = '/api/dsh-screenshot/hotkey'
|
|
19
|
+
const INBOX_ROUTE = '/api/dsh-screenshot/inbox'
|
|
20
|
+
const FLOAT_ROUTE = '/api/dsh-screenshot/float'
|
|
21
|
+
const STATE_ROUTE = '/api/dsh-screenshot/state'
|
|
22
|
+
const DEFAULT_HOTKEY = 'alt+shift+s'
|
|
23
|
+
// The floating ball is on unless the renderer says otherwise.
|
|
24
|
+
const DEFAULT_FLOAT = 'on'
|
|
25
|
+
|
|
26
|
+
// One global hotkey binding for the whole process; the renderer pushes its
|
|
27
|
+
// stored choice on load, so the helper always ends up matching the setting.
|
|
28
|
+
let hotkeySpec = DEFAULT_HOTKEY
|
|
29
|
+
let floatSpec = DEFAULT_FLOAT
|
|
30
|
+
// Files waiting for the renderer: { name, path }, oldest first.
|
|
31
|
+
const inbox = []
|
|
32
|
+
|
|
33
|
+
// A renderer that is waiting for the next shot parks one request here instead
|
|
34
|
+
// of asking again every few seconds; the attach then starts within a frame of
|
|
35
|
+
// the capture landing rather than on the next tick.
|
|
36
|
+
const inboxWaiters = new Set()
|
|
37
|
+
|
|
38
|
+
function wakeInbox() {
|
|
39
|
+
const waiting = [...inboxWaiters]
|
|
40
|
+
inboxWaiters.clear()
|
|
41
|
+
for (const done of waiting) done()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function waitForInbox(ms) {
|
|
45
|
+
if (inbox.length > 0 || !(ms > 0)) return Promise.resolve()
|
|
46
|
+
return new Promise((resolve) => {
|
|
47
|
+
let timer
|
|
48
|
+
const done = () => {
|
|
49
|
+
inboxWaiters.delete(done)
|
|
50
|
+
clearTimeout(timer)
|
|
51
|
+
resolve()
|
|
52
|
+
}
|
|
53
|
+
timer = setTimeout(done, Math.min(ms, 30000))
|
|
54
|
+
inboxWaiters.add(done)
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// The overlay is a Windows PowerShell WinForms window (per-monitor DPI aware
|
|
59
|
+
// capture, window hit-testing, topmost form). Other platforms get an explicit
|
|
60
|
+
// refusal instead of a spawn failure.
|
|
61
|
+
const SUPPORTED = process.platform === 'win32'
|
|
62
|
+
|
|
63
|
+
// A cold powershell.exe spends ~1.2s before the first pixel is grabbed, and the
|
|
64
|
+
// two Add-Type compiles add another ~0.4s. Both are paid once per helper process
|
|
65
|
+
// instead of once per click.
|
|
66
|
+
const HELPER_START_TIMEOUT_MS = 20000
|
|
67
|
+
const CAPTURE_TIMEOUT_MS = 130000
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* One-shot capture: spawn a throwaway process, run the whole gesture, exit.
|
|
71
|
+
* Kept as the fallback so a broken preheat never takes the feature down.
|
|
72
|
+
*/
|
|
73
|
+
function runCapture() {
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
const args = [
|
|
76
|
+
'-NoProfile', '-STA', '-ExecutionPolicy', 'Bypass',
|
|
77
|
+
'-File', CAPTURE_SCRIPT, '-OutDir', OUT_DIR,
|
|
78
|
+
]
|
|
79
|
+
let child
|
|
80
|
+
try {
|
|
81
|
+
child = spawn('powershell.exe', args, {
|
|
82
|
+
windowsHide: true,
|
|
83
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
84
|
+
})
|
|
85
|
+
} catch (error) {
|
|
86
|
+
reject(error)
|
|
87
|
+
return
|
|
88
|
+
}
|
|
89
|
+
let stdout = ''
|
|
90
|
+
let stderr = ''
|
|
91
|
+
child.stdout.setEncoding('utf8')
|
|
92
|
+
child.stderr.setEncoding('utf8')
|
|
93
|
+
child.stdout.on('data', (chunk) => { stdout += chunk })
|
|
94
|
+
child.stderr.on('data', (chunk) => { stderr += chunk })
|
|
95
|
+
const timer = setTimeout(() => {
|
|
96
|
+
try { child.kill() } catch { }
|
|
97
|
+
reject(new Error('capture timed out'))
|
|
98
|
+
}, CAPTURE_TIMEOUT_MS)
|
|
99
|
+
child.on('error', (error) => {
|
|
100
|
+
clearTimeout(timer)
|
|
101
|
+
reject(error)
|
|
102
|
+
})
|
|
103
|
+
child.on('close', (code) => {
|
|
104
|
+
clearTimeout(timer)
|
|
105
|
+
const lastLine = stdout.trim().split(/\r?\n/).pop() || ''
|
|
106
|
+
if (code !== 0) {
|
|
107
|
+
reject(new Error(stderr.trim() || `capture process exited with ${code}`))
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
if (lastLine === 'CANCELLED') {
|
|
111
|
+
resolve({ cancelled: true })
|
|
112
|
+
return
|
|
113
|
+
}
|
|
114
|
+
if (!lastLine) {
|
|
115
|
+
reject(new Error('capture produced no file path'))
|
|
116
|
+
return
|
|
117
|
+
}
|
|
118
|
+
resolve({ cancelled: false, file: lastLine })
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** A resident helper: `capture.ps1 -Serve` waiting on stdin for gestures. */
|
|
124
|
+
let helper = null
|
|
125
|
+
|
|
126
|
+
function startHelper() {
|
|
127
|
+
const child = spawn('powershell.exe', [
|
|
128
|
+
'-NoProfile', '-STA', '-ExecutionPolicy', 'Bypass',
|
|
129
|
+
'-File', CAPTURE_SCRIPT, '-Serve', '-OutDir', OUT_DIR,
|
|
130
|
+
// The helper registers the hotkey itself (RegisterHotKey needs a Windows
|
|
131
|
+
// message loop, which the resident PowerShell already owns).
|
|
132
|
+
'-Hotkey', hotkeySpec,
|
|
133
|
+
// Same story for the ball: the resident helper owns the always-on-top
|
|
134
|
+
// window, so it must exist before the first click can land on it.
|
|
135
|
+
'-Float', floatSpec,
|
|
136
|
+
// The helper closes any open overlay if this process disappears.
|
|
137
|
+
'-ParentPid', String(process.pid),
|
|
138
|
+
], {
|
|
139
|
+
windowsHide: true,
|
|
140
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
const state = {
|
|
144
|
+
child,
|
|
145
|
+
buffer: '',
|
|
146
|
+
stderr: '',
|
|
147
|
+
pending: null,
|
|
148
|
+
ready: null,
|
|
149
|
+
resolveReady: null,
|
|
150
|
+
rejectReady: null,
|
|
151
|
+
}
|
|
152
|
+
state.ready = new Promise((resolve, reject) => {
|
|
153
|
+
state.resolveReady = resolve
|
|
154
|
+
state.rejectReady = reject
|
|
155
|
+
})
|
|
156
|
+
// The rejection is awaited per request; keep node from flagging it as unhandled
|
|
157
|
+
// when nothing is waiting at the moment the helper dies.
|
|
158
|
+
state.ready.catch(() => {})
|
|
159
|
+
|
|
160
|
+
child.stdout.setEncoding('utf8')
|
|
161
|
+
child.stderr.setEncoding('utf8')
|
|
162
|
+
child.stdout.on('data', (chunk) => {
|
|
163
|
+
state.buffer += chunk
|
|
164
|
+
let index
|
|
165
|
+
while ((index = state.buffer.indexOf('\n')) !== -1) {
|
|
166
|
+
const line = state.buffer.slice(0, index).replace(/\r$/, '')
|
|
167
|
+
state.buffer = state.buffer.slice(index + 1)
|
|
168
|
+
handleHelperLine(state, line)
|
|
169
|
+
}
|
|
170
|
+
})
|
|
171
|
+
child.stderr.on('data', (chunk) => {
|
|
172
|
+
state.stderr = (state.stderr + chunk).slice(-2000)
|
|
173
|
+
})
|
|
174
|
+
child.on('error', (error) => {
|
|
175
|
+
state.rejectReady(error)
|
|
176
|
+
failPending(state, error)
|
|
177
|
+
if (helper === state) helper = null
|
|
178
|
+
})
|
|
179
|
+
child.on('close', (code) => {
|
|
180
|
+
const message = state.stderr.trim() || `capture helper exited with ${code}`
|
|
181
|
+
state.rejectReady(new Error(message))
|
|
182
|
+
failPending(state, new Error(message))
|
|
183
|
+
if (helper === state) helper = null
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
const startTimer = setTimeout(() => {
|
|
187
|
+
state.rejectReady(new Error('capture helper did not become ready'))
|
|
188
|
+
}, HELPER_START_TIMEOUT_MS)
|
|
189
|
+
startTimer.unref?.()
|
|
190
|
+
state.ready.then(() => clearTimeout(startTimer), () => clearTimeout(startTimer))
|
|
191
|
+
|
|
192
|
+
return state
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function handleHelperLine(state, line) {
|
|
196
|
+
if (line === 'READY') {
|
|
197
|
+
state.resolveReady()
|
|
198
|
+
return
|
|
199
|
+
}
|
|
200
|
+
// A global hotkey press: no HTTP request is behind it, so the helper hands the
|
|
201
|
+
// gesture to the inbox instead of replying on stdin.
|
|
202
|
+
if (line === 'HOTKEY') {
|
|
203
|
+
void captureFromHotkey()
|
|
204
|
+
return
|
|
205
|
+
}
|
|
206
|
+
// A click on the floating ball: same handoff as the hotkey, no request behind it.
|
|
207
|
+
if (line === 'FLOAT') {
|
|
208
|
+
void appendFile(DIAG_FILE, `${new Date().toISOString()} {"stage":"float-triggered"}\n`, 'utf8').catch(() => {})
|
|
209
|
+
void captureFromHotkey()
|
|
210
|
+
return
|
|
211
|
+
}
|
|
212
|
+
if (line.startsWith('FLOATSTATE|')) {
|
|
213
|
+
state.floatState = line.slice('FLOATSTATE|'.length)
|
|
214
|
+
void appendFile(
|
|
215
|
+
DIAG_FILE,
|
|
216
|
+
`${new Date().toISOString()} {"stage":"float","state":${JSON.stringify(state.floatState)}}\n`,
|
|
217
|
+
'utf8',
|
|
218
|
+
).catch(() => {})
|
|
219
|
+
return
|
|
220
|
+
}
|
|
221
|
+
if (line.startsWith('HOTKEYSTATE|')) {
|
|
222
|
+
state.hotkeyState = line.slice('HOTKEYSTATE|'.length)
|
|
223
|
+
void appendFile(
|
|
224
|
+
DIAG_FILE,
|
|
225
|
+
`${new Date().toISOString()} {"stage":"hotkey","state":${JSON.stringify(state.hotkeyState)},"spec":${JSON.stringify(hotkeySpec)}}\n`,
|
|
226
|
+
'utf8',
|
|
227
|
+
).catch(() => {})
|
|
228
|
+
return
|
|
229
|
+
}
|
|
230
|
+
// SHOWN carries the overlay's own timing; log it so a slow start is visible.
|
|
231
|
+
if (line.startsWith('SHOWN|')) {
|
|
232
|
+
void appendFile(DIAG_FILE, `${new Date().toISOString()} {"stage":"overlay-shown","ms":${JSON.stringify(line.slice('SHOWN|'.length))}}\n`, 'utf8').catch(() => {})
|
|
233
|
+
return
|
|
234
|
+
}
|
|
235
|
+
if (!line.startsWith('RESULT|')) { return }
|
|
236
|
+
|
|
237
|
+
const [, status, detail = ''] = line.split('|')
|
|
238
|
+
const pending = state.pending
|
|
239
|
+
state.pending = null
|
|
240
|
+
if (pending === null) { return }
|
|
241
|
+
clearTimeout(pending.timer)
|
|
242
|
+
if (status === 'OK') {
|
|
243
|
+
pending.resolve({ cancelled: false, file: detail })
|
|
244
|
+
} else if (status === 'CANCELLED') {
|
|
245
|
+
pending.resolve({ cancelled: true })
|
|
246
|
+
} else {
|
|
247
|
+
pending.reject(new Error(detail || 'capture failed'))
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function failPending(state, error) {
|
|
252
|
+
const pending = state.pending
|
|
253
|
+
state.pending = null
|
|
254
|
+
if (pending === null) { return }
|
|
255
|
+
clearTimeout(pending.timer)
|
|
256
|
+
pending.reject(error)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function stopHelper(state = helper) {
|
|
260
|
+
if (state === null) { return }
|
|
261
|
+
if (helper === state) { helper = null }
|
|
262
|
+
try { state.child.stdin.write('QUIT\n') } catch { }
|
|
263
|
+
// Give the overlay a moment to unwind, then make sure the process is gone: a
|
|
264
|
+
// stuck full-screen topmost form would block the whole desktop.
|
|
265
|
+
const timer = setTimeout(() => {
|
|
266
|
+
try { state.child.kill() } catch { }
|
|
267
|
+
}, 500)
|
|
268
|
+
timer.unref?.()
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function helperUnavailable(error) {
|
|
272
|
+
const wrapped = error instanceof Error ? error : new Error(String(error))
|
|
273
|
+
wrapped.code = 'helper-unavailable'
|
|
274
|
+
return wrapped
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** Send one gesture to the warm helper, or throw helper-unavailable. */
|
|
278
|
+
async function captureResident() {
|
|
279
|
+
if (helper === null) { helper = startHelper() }
|
|
280
|
+
const state = helper
|
|
281
|
+
|
|
282
|
+
try {
|
|
283
|
+
await state.ready
|
|
284
|
+
} catch (error) {
|
|
285
|
+
stopHelper(state)
|
|
286
|
+
throw helperUnavailable(error)
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return new Promise((resolve, reject) => {
|
|
290
|
+
const timer = setTimeout(() => {
|
|
291
|
+
state.pending = null
|
|
292
|
+
reject(new Error('capture timed out'))
|
|
293
|
+
// The overlay is still on screen; kill the process so it cannot outlive
|
|
294
|
+
// the request and hold the desktop hostage.
|
|
295
|
+
stopHelper(state)
|
|
296
|
+
}, CAPTURE_TIMEOUT_MS)
|
|
297
|
+
|
|
298
|
+
state.pending = { resolve, reject, timer }
|
|
299
|
+
try {
|
|
300
|
+
state.child.stdin.write('CAPTURE|0|0\n')
|
|
301
|
+
} catch (error) {
|
|
302
|
+
clearTimeout(timer)
|
|
303
|
+
state.pending = null
|
|
304
|
+
stopHelper(state)
|
|
305
|
+
reject(error)
|
|
306
|
+
}
|
|
307
|
+
})
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// One overlay at a time: a second capture would stack another full-screen
|
|
311
|
+
// topmost form over the first, and neither would be dismissible by the other.
|
|
312
|
+
let captureInFlight = false
|
|
313
|
+
|
|
314
|
+
/** Fetch-shaped handler for the authenticated /api channel. */
|
|
315
|
+
async function handleCapture() {
|
|
316
|
+
if (!SUPPORTED) {
|
|
317
|
+
return Response.json({
|
|
318
|
+
ok: false,
|
|
319
|
+
error: `screen capture needs Windows (running on ${process.platform})`,
|
|
320
|
+
}, { status: 501 })
|
|
321
|
+
}
|
|
322
|
+
if (captureInFlight) {
|
|
323
|
+
return Response.json({ ok: false, error: 'a capture is already running' }, { status: 409 })
|
|
324
|
+
}
|
|
325
|
+
captureInFlight = true
|
|
326
|
+
let file
|
|
327
|
+
try {
|
|
328
|
+
await mkdir(OUT_DIR, { recursive: true })
|
|
329
|
+
let result
|
|
330
|
+
try {
|
|
331
|
+
result = await captureResident()
|
|
332
|
+
} catch (error) {
|
|
333
|
+
// Only fall back when no gesture was ever delivered; a request that already
|
|
334
|
+
// opened an overlay must not spawn a second one.
|
|
335
|
+
if (error?.code !== 'helper-unavailable') { throw error }
|
|
336
|
+
result = await runCapture()
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (result.cancelled) {
|
|
340
|
+
return Response.json({ ok: true, cancelled: true })
|
|
341
|
+
}
|
|
342
|
+
file = result.file
|
|
343
|
+
const bytes = await readFile(file)
|
|
344
|
+
return Response.json({
|
|
345
|
+
ok: true,
|
|
346
|
+
cancelled: false,
|
|
347
|
+
data: bytes.toString('base64'),
|
|
348
|
+
mediaType: 'image/png',
|
|
349
|
+
// The stamp in the file name keeps two shots in one draft distinguishable.
|
|
350
|
+
name: basename(file),
|
|
351
|
+
})
|
|
352
|
+
} catch (error) {
|
|
353
|
+
return Response.json({
|
|
354
|
+
ok: false,
|
|
355
|
+
error: error instanceof Error ? error.message : String(error),
|
|
356
|
+
}, { status: 500 })
|
|
357
|
+
} finally {
|
|
358
|
+
captureInFlight = false
|
|
359
|
+
// The PNG only carries bytes to the renderer; the clipboard already holds a
|
|
360
|
+
// copy, so keeping every frame would grow %TEMP% without bound.
|
|
361
|
+
if (file !== undefined) await unlink(file).catch(() => {})
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* A hotkey press with no HTTP request behind it: run the same resident overlay,
|
|
367
|
+
* then park the PNG in the inbox so the next poll can attach it to whatever
|
|
368
|
+
* draft is open - the DSH window may not even have focus.
|
|
369
|
+
*/
|
|
370
|
+
async function captureFromHotkey() {
|
|
371
|
+
if (!SUPPORTED || captureInFlight) {
|
|
372
|
+
void appendFile(
|
|
373
|
+
DIAG_FILE,
|
|
374
|
+
`${new Date().toISOString()} {"stage":"hotkey-skip","supported":${SUPPORTED},"busy":${captureInFlight}}\n`,
|
|
375
|
+
'utf8',
|
|
376
|
+
).catch(() => {})
|
|
377
|
+
return
|
|
378
|
+
}
|
|
379
|
+
captureInFlight = true
|
|
380
|
+
void appendFile(DIAG_FILE, `${new Date().toISOString()} {"stage":"hotkey-triggered"}\n`, 'utf8').catch(() => {})
|
|
381
|
+
try {
|
|
382
|
+
await mkdir(INBOX_DIR, { recursive: true })
|
|
383
|
+
let result
|
|
384
|
+
try {
|
|
385
|
+
result = await captureResident()
|
|
386
|
+
} catch (error) {
|
|
387
|
+
// Same rule as the button: only fall back if no gesture was delivered.
|
|
388
|
+
if (error?.code !== 'helper-unavailable') { throw error }
|
|
389
|
+
result = await runCapture()
|
|
390
|
+
}
|
|
391
|
+
if (result.cancelled) { return }
|
|
392
|
+
const target = join(INBOX_DIR, basename(result.file))
|
|
393
|
+
await rename(result.file, target)
|
|
394
|
+
inbox.push({ name: basename(target), path: target })
|
|
395
|
+
void appendFile(DIAG_FILE, `${new Date().toISOString()} {"stage":"inbox-parked"}\n`, 'utf8').catch(() => {})
|
|
396
|
+
wakeInbox()
|
|
397
|
+
} catch (error) {
|
|
398
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
399
|
+
await appendFile(
|
|
400
|
+
DIAG_FILE,
|
|
401
|
+
`${new Date().toISOString()} {"stage":"hotkey-capture","error":${JSON.stringify(message)}}\n`,
|
|
402
|
+
'utf8',
|
|
403
|
+
).catch(() => {})
|
|
404
|
+
} finally {
|
|
405
|
+
captureInFlight = false
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** Hand every parked screenshot to the renderer once, then forget it. */
|
|
410
|
+
async function handleInbox(request) {
|
|
411
|
+
// The renderer asks with waitMs so the answer arrives the moment a capture
|
|
412
|
+
// lands; the plain poll (no body) still returns immediately.
|
|
413
|
+
const body = request === undefined || request === null
|
|
414
|
+
? null
|
|
415
|
+
: await request.json().catch(() => null)
|
|
416
|
+
const asked = body && Number(body.waitMs)
|
|
417
|
+
const waitMs = Number.isFinite(asked) ? Math.max(0, Math.min(30000, asked)) : 0
|
|
418
|
+
if (inbox.length === 0 && waitMs > 0) { await waitForInbox(waitMs) }
|
|
419
|
+
const batch = inbox.splice(0, inbox.length)
|
|
420
|
+
const items = []
|
|
421
|
+
for (const item of batch) {
|
|
422
|
+
try {
|
|
423
|
+
const bytes = await readFile(item.path)
|
|
424
|
+
items.push({ name: item.name, data: bytes.toString('base64'), mediaType: 'image/png' })
|
|
425
|
+
} catch { /* a file already cleaned up simply drops out */ }
|
|
426
|
+
await unlink(item.path).catch(() => {})
|
|
427
|
+
}
|
|
428
|
+
// The renderer polls this every few seconds anyway, so the ball's real state
|
|
429
|
+
// rides along: hiding it from its own menu has to reach the settings page.
|
|
430
|
+
return Response.json({ ok: true, items, float: currentFloat() })
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/** What the ball is actually doing right now, falling back to the last request. */
|
|
434
|
+
function currentFloat() {
|
|
435
|
+
if (helper !== null && typeof helper.floatState === 'string') {
|
|
436
|
+
return helper.floatState.indexOf('off') === 0 ? 'off' : 'on'
|
|
437
|
+
}
|
|
438
|
+
return floatSpec
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** Rebind the global hotkey. The renderer owns the value; the helper owns the OS binding. */
|
|
442
|
+
async function handleHotkey(request) {
|
|
443
|
+
const body = await request.json().catch(() => null)
|
|
444
|
+
const spec = body && typeof body.hotkey === 'string' ? body.hotkey.trim() : ''
|
|
445
|
+
if (!spec) {
|
|
446
|
+
return Response.json({ ok: false, error: 'hotkey is required' }, { status: 400 })
|
|
447
|
+
}
|
|
448
|
+
hotkeySpec = spec
|
|
449
|
+
const state = helper
|
|
450
|
+
if (state !== null) {
|
|
451
|
+
try { state.child.stdin.write(`HOTKEY|${spec}\n`) } catch { }
|
|
452
|
+
}
|
|
453
|
+
return Response.json({ ok: true, hotkey: hotkeySpec, applied: state !== null })
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/** Small, side-effect-free read of what the ball is doing. */
|
|
457
|
+
async function handleState() {
|
|
458
|
+
return Response.json({ ok: true, float: currentFloat() })
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/** Show or hide the floating ball; the helper owns the window. */
|
|
462
|
+
async function handleFloat(request) {
|
|
463
|
+
const body = await request.json().catch(() => null)
|
|
464
|
+
const on = body !== null && body.on === false ? false : true
|
|
465
|
+
floatSpec = on ? 'on' : 'off'
|
|
466
|
+
const state = helper
|
|
467
|
+
let applied = false
|
|
468
|
+
if (state !== null) {
|
|
469
|
+
try {
|
|
470
|
+
state.child.stdin.write(`FLOAT|${floatSpec}\n`)
|
|
471
|
+
applied = true
|
|
472
|
+
} catch { }
|
|
473
|
+
}
|
|
474
|
+
void appendFile(
|
|
475
|
+
DIAG_FILE,
|
|
476
|
+
`${new Date().toISOString()} {"stage":"float-request","on":${on},"applied":${applied}}\n`,
|
|
477
|
+
'utf8',
|
|
478
|
+
).catch(() => {})
|
|
479
|
+
return Response.json({ ok: true, on, applied })
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/** Append one browser-side diagnostic line; never fails the caller. */
|
|
483
|
+
async function handleDiag(request) {
|
|
484
|
+
try {
|
|
485
|
+
const body = await request.text()
|
|
486
|
+
await mkdir(tmpdir(), { recursive: true })
|
|
487
|
+
await appendFile(DIAG_FILE, `${new Date().toISOString()} ${body}\n`, 'utf8')
|
|
488
|
+
} catch { }
|
|
489
|
+
return Response.json({ ok: true })
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Host half: registers the capture endpoint on the Connection fetch registry
|
|
494
|
+
* (the authenticated /api channel), declaring POST explicitly.
|
|
495
|
+
*/
|
|
496
|
+
export const inject = ['connection']
|
|
497
|
+
|
|
498
|
+
export function apply(ctx) {
|
|
499
|
+
const connection = ctx.get('connection')
|
|
500
|
+
if (!connection || typeof connection.fetch?.register !== 'function') {
|
|
501
|
+
return
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
const dispose = connection.fetch.register({
|
|
505
|
+
path: CAPTURE_ROUTE,
|
|
506
|
+
methods: ['POST'],
|
|
507
|
+
requestBody: 'buffered',
|
|
508
|
+
fetch: handleCapture,
|
|
509
|
+
})
|
|
510
|
+
|
|
511
|
+
const disposeDiag = connection.fetch.register({
|
|
512
|
+
path: DIAG_ROUTE,
|
|
513
|
+
methods: ['POST'],
|
|
514
|
+
requestBody: 'buffered',
|
|
515
|
+
fetch: handleDiag,
|
|
516
|
+
})
|
|
517
|
+
|
|
518
|
+
const disposeHotkey = connection.fetch.register({
|
|
519
|
+
path: HOTKEY_ROUTE,
|
|
520
|
+
methods: ['POST'],
|
|
521
|
+
requestBody: 'buffered',
|
|
522
|
+
fetch: handleHotkey,
|
|
523
|
+
})
|
|
524
|
+
|
|
525
|
+
const disposeFloat = connection.fetch.register({
|
|
526
|
+
path: FLOAT_ROUTE,
|
|
527
|
+
methods: ['POST'],
|
|
528
|
+
requestBody: 'buffered',
|
|
529
|
+
fetch: handleFloat,
|
|
530
|
+
})
|
|
531
|
+
|
|
532
|
+
const disposeState = connection.fetch.register({
|
|
533
|
+
path: STATE_ROUTE,
|
|
534
|
+
methods: ['POST'],
|
|
535
|
+
requestBody: 'buffered',
|
|
536
|
+
fetch: handleState,
|
|
537
|
+
})
|
|
538
|
+
|
|
539
|
+
const disposeInbox = connection.fetch.register({
|
|
540
|
+
path: INBOX_ROUTE,
|
|
541
|
+
methods: ['POST'],
|
|
542
|
+
requestBody: 'buffered',
|
|
543
|
+
fetch: handleInbox,
|
|
544
|
+
})
|
|
545
|
+
|
|
546
|
+
// Preheat one helper at load so the first click does not pay the process start.
|
|
547
|
+
// It costs one idle powershell process; every later click lands in ~200ms.
|
|
548
|
+
if (SUPPORTED) {
|
|
549
|
+
try {
|
|
550
|
+
if (helper === null) { helper = startHelper() }
|
|
551
|
+
} catch { }
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// ctx.effect runs the function now and keeps its return value as the disposer,
|
|
555
|
+
// so the cleanup must be the inner function, not an immediate call.
|
|
556
|
+
ctx.effect(() => () => {
|
|
557
|
+
stopHelper()
|
|
558
|
+
dispose()
|
|
559
|
+
disposeDiag()
|
|
560
|
+
disposeHotkey()
|
|
561
|
+
disposeFloat()
|
|
562
|
+
disposeState()
|
|
563
|
+
disposeInbox()
|
|
564
|
+
})
|
|
565
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Screenshot to chat",
|
|
4
|
+
"description": "Adds a screenshot button to the composer: select a screen region and insert it as an image attachment. Windows only.",
|
|
5
|
+
"detail": "docs/DETAIL.md"
|
|
6
|
+
},
|
|
7
|
+
"button": "Screenshot",
|
|
8
|
+
"hintSelect": "Drag to select a screen region, Esc to cancel",
|
|
9
|
+
"done": "Screenshot added",
|
|
10
|
+
"failed": "Screenshot failed",
|
|
11
|
+
"cancelled": "Screenshot cancelled",
|
|
12
|
+
"unsupported": "Screenshot is available on Windows only"
|
|
13
|
+
}
|
package/locale/zh.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "对话截屏",
|
|
4
|
+
"description": "在输入框添加截屏按钮:框选屏幕区域后作为图片附件插入草稿。仅支持 Windows。",
|
|
5
|
+
"detail": "docs/DETAIL.zh.md"
|
|
6
|
+
},
|
|
7
|
+
"button": "截屏",
|
|
8
|
+
"hintSelect": "拖动框选区域,Esc 取消",
|
|
9
|
+
"done": "截图已添加",
|
|
10
|
+
"failed": "截屏失败",
|
|
11
|
+
"cancelled": "已取消截屏",
|
|
12
|
+
"unsupported": "截屏仅支持 Windows"
|
|
13
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@inkbite/shotink",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@inkbite/shotink",
|
|
3
|
+
"version": "1.4.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "输入框截屏入口:工具栏按钮与「+」菜单两种位置可切换(菜单行排在「添加」分组、紧跟「文件」),框选后可涂画标注(画笔/方框/箭头/序号/文字/马赛克/撤销/换色,笔宽与马赛克强度右键切换;支持 Ctrl+Shift+X 窗口内快捷键、任意应用前台都能按的系统级全局热键 Alt+Shift+S,以及桌面上点一下就能截图的常驻悬浮球(可拖动、记位置、右键收起),截完直接进当前草稿、确认后 DSH 自动回到前台;键位与悬浮球都可在「设置 → 截图」里换或关闭),确认后作为图片附件插入 Draft。 Windows only.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"dsh",
|
|
8
|
+
"dsh-plugin",
|
|
9
|
+
"deepseek-harness",
|
|
10
|
+
"cordis",
|
|
11
|
+
"plugin",
|
|
12
|
+
"screenshot",
|
|
13
|
+
"capture",
|
|
14
|
+
"windows"
|
|
15
|
+
],
|
|
16
|
+
"author": "mbcc5732",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"os": [
|
|
19
|
+
"win32"
|
|
20
|
+
],
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./index.js",
|
|
23
|
+
"./client": "./client.js",
|
|
24
|
+
"./locale/*.json": "./locale/*.json",
|
|
25
|
+
"./icon": "./icon.svg"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"index.js",
|
|
29
|
+
"client.js",
|
|
30
|
+
"capture.ps1",
|
|
31
|
+
"cordis.patch.yml",
|
|
32
|
+
"icon.svg",
|
|
33
|
+
"locale/*.json",
|
|
34
|
+
"README.md",
|
|
35
|
+
"README.zh.md",
|
|
36
|
+
"LICENSE"
|
|
37
|
+
],
|
|
38
|
+
"dsh": {
|
|
39
|
+
"bundle": {
|
|
40
|
+
"patch": "./cordis.patch.yml"
|
|
41
|
+
},
|
|
42
|
+
"client": {
|
|
43
|
+
"platform": "web",
|
|
44
|
+
"immediately": true,
|
|
45
|
+
"inject": [
|
|
46
|
+
"@deepseek-ai/dsh-client-ui-conversation",
|
|
47
|
+
"@deepseek-ai/dsh-client-ui-commands"
|
|
48
|
+
]
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"repository": {
|
|
52
|
+
"url": "git+https://github.com/mbcc5732/dsh-screenshot.git",
|
|
53
|
+
"type": "git"
|
|
54
|
+
},
|
|
55
|
+
"homepage": "https://mbcc5732.github.io/dsh-screenshot/",
|
|
56
|
+
"bugs": {
|
|
57
|
+
"url": "https://github.com/mbcc5732/dsh-screenshot/issues"
|
|
58
|
+
},
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"registry": "https://registry.npmjs.org",
|
|
61
|
+
"access": "public"
|
|
62
|
+
}
|
|
63
|
+
}
|