cdpm-mcp 0.1.8 → 0.1.12
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/CHANGELOG.md +131 -89
- package/README.ko.md +326 -277
- package/README.md +249 -200
- package/dist/agent-setup.js +23 -23
- package/dist/cdp/client.js +13 -9
- package/dist/cdp/downloads.js +4 -1
- package/dist/cdp/sessions.js +45 -24
- package/dist/cdp/targets.js +12 -1
- package/dist/chrome/bubbles.js +14 -14
- package/dist/chrome/launcher.js +3 -0
- package/dist/claude-md-setup.js +22 -22
- package/dist/cli.js +6 -4
- package/dist/page/actions.js +248 -143
- package/dist/page/keys.js +1 -1
- package/dist/page/snapshot.js +11 -5
- package/dist/proxy-forwarder.js +17 -11
- package/dist/registry/lock.js +47 -4
- package/dist/registry/messages.js +28 -13
- package/dist/registry/store.js +19 -7
- package/dist/service.js +5 -5
- package/dist/taskbar/dock.js +31 -31
- package/dist/taskbar/icon-gdi.js +158 -158
- package/dist/taskbar/watcher.js +4 -0
- package/dist/tools/register.js +105 -73
- package/package.json +65 -64
package/dist/proxy-forwarder.js
CHANGED
|
@@ -150,40 +150,46 @@ server.on('connect', (req, client, head) => {
|
|
|
150
150
|
client.end(`HTTP/1.1 502 Bad Gateway\r\ncontent-type: text/plain; charset=utf-8\r\n\r\nCDPM proxy forwarder error: ${e.message}`);
|
|
151
151
|
});
|
|
152
152
|
});
|
|
153
|
-
/**
|
|
153
|
+
/**
|
|
154
|
+
* 그 슬롯 포트에 떠 있는 Chrome의 고유 주소 (Chrome마다 다름). gone: 포트가 닫힘(Chrome이 꺼짐).
|
|
155
|
+
* 응답이 늦은 것(바쁜 Chrome)은 gone이 아니다.
|
|
156
|
+
*/
|
|
154
157
|
function chromeId() {
|
|
155
158
|
return new Promise((resolve) => {
|
|
156
159
|
const r = http.get({ host: '127.0.0.1', port: cdpPort, path: '/json/version', timeout: 2000 }, (res) => {
|
|
157
160
|
let b = '';
|
|
158
161
|
res.on('data', (d) => (b += d));
|
|
159
162
|
res.on('end', () => { try {
|
|
160
|
-
resolve(JSON.parse(b).webSocketDebuggerUrl);
|
|
163
|
+
resolve({ id: JSON.parse(b).webSocketDebuggerUrl, gone: false });
|
|
161
164
|
}
|
|
162
165
|
catch {
|
|
163
|
-
resolve(
|
|
166
|
+
resolve({ gone: false });
|
|
164
167
|
} });
|
|
165
168
|
});
|
|
166
|
-
r.on('error', () => resolve(
|
|
167
|
-
r.on('timeout', () => { r.destroy(); resolve(
|
|
169
|
+
r.on('error', (e) => resolve({ gone: e.code === 'ECONNREFUSED' }));
|
|
170
|
+
r.on('timeout', () => { r.destroy(); resolve({ gone: false }); });
|
|
168
171
|
});
|
|
169
172
|
}
|
|
170
173
|
server.listen(0, '127.0.0.1', () => {
|
|
171
174
|
const addr = server.address();
|
|
172
175
|
process.stdout.write(`LISTEN ${addr.port}\n`);
|
|
173
|
-
// Chrome이 뜨기를 1분까지 기다린다. 뜬 뒤에는 그 Chrome이 꺼지거나(두 번 연속
|
|
174
|
-
// 같은 포트에 다른 Chrome이 뜨면(슬롯을 끄고 다시 켬) 끝난다.
|
|
176
|
+
// Chrome이 뜨기를 1분까지 기다린다. 뜬 뒤에는 그 Chrome이 꺼지거나(두 번 연속 포트 닫힘)
|
|
177
|
+
// 같은 포트에 다른 Chrome이 뜨면(슬롯을 끄고 다시 켬) 끝난다. 바빠서 늦게 답하는 것은 꺼진 것으로 세지 않는다.
|
|
178
|
+
// (1분 넘게 한 번도 답이 없으면 그때는 끝난 것으로 본다: 혼자 남아 계속 떠 있지 않게)
|
|
175
179
|
const started = Date.now();
|
|
176
|
-
let mine, misses = 0;
|
|
180
|
+
let mine, misses = 0, lastSeen = Date.now();
|
|
177
181
|
const check = async () => {
|
|
178
|
-
const id = await chromeId();
|
|
182
|
+
const { id, gone } = await chromeId();
|
|
179
183
|
if (id && !mine)
|
|
180
184
|
mine = id;
|
|
181
185
|
if (id && mine && id !== mine)
|
|
182
186
|
process.exit(0);
|
|
183
|
-
if (!id && (mine ? ++misses >= 2 : Date.now() - started > 60_000))
|
|
187
|
+
if (!id && (mine ? (gone && ++misses >= 2) || Date.now() - lastSeen > 60_000 : Date.now() - started > 60_000))
|
|
184
188
|
process.exit(0);
|
|
185
|
-
if (id)
|
|
189
|
+
if (id) {
|
|
186
190
|
misses = 0;
|
|
191
|
+
lastSeen = Date.now();
|
|
192
|
+
}
|
|
187
193
|
// 내 Chrome을 알아보기 전에는 자주 본다 (금방 꺼지는 Chrome도 놓치지 않게)
|
|
188
194
|
setTimeout(check, mine ? 2000 : 300);
|
|
189
195
|
};
|
package/dist/registry/lock.js
CHANGED
|
@@ -55,7 +55,26 @@ export async function withFileLock(lockPath, fn, timeoutMs, timeoutMessage) {
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
-
|
|
58
|
+
/**
|
|
59
|
+
* 잡은 프로세스가 죽은 잠금을 지운다. 지우는 일은 정리용 잠금(.reap)을 잡은 한 프로세스만 한다.
|
|
60
|
+
* 그러지 않으면 "죽었다"고 읽어 둔 두 프로세스 중 늦은 쪽이, 먼저 정리하고 새로 잡은 쪽의 살아 있는 잠금을 지운다.
|
|
61
|
+
*/
|
|
62
|
+
export function removeIfOwnerDead(lockPath) {
|
|
63
|
+
const reap = `${lockPath}.reap`;
|
|
64
|
+
try {
|
|
65
|
+
fs.closeSync(fs.openSync(reap, 'wx'));
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
// 다른 프로세스가 정리 중. 정리는 순식간이므로 오래 남아 있으면 정리하던 프로세스가 죽은 것
|
|
69
|
+
try {
|
|
70
|
+
if (Date.now() - fs.statSync(reap).mtimeMs > UNREADABLE_GRACE_MS)
|
|
71
|
+
fs.unlinkSync(reap);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
// 그 사이 풀렸으면 무시
|
|
75
|
+
}
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
59
78
|
try {
|
|
60
79
|
const owner = Number(fs.readFileSync(lockPath, 'utf8'));
|
|
61
80
|
if (owner > 0) {
|
|
@@ -70,9 +89,34 @@ function removeIfOwnerDead(lockPath) {
|
|
|
70
89
|
catch {
|
|
71
90
|
// 그 사이 풀렸으면 무시
|
|
72
91
|
}
|
|
92
|
+
finally {
|
|
93
|
+
try {
|
|
94
|
+
fs.unlinkSync(reap);
|
|
95
|
+
}
|
|
96
|
+
catch { /* 무시 */ }
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const syncWait = new Int32Array(new SharedArrayBuffer(4));
|
|
100
|
+
/**
|
|
101
|
+
* 파일을 읽는다. 없으면 undefined. Windows에서 백신·인덱서가 잠깐 잡고 있으면(EBUSY 등) 몇 번 다시 읽고,
|
|
102
|
+
* 그래도 못 읽으면 던진다 (못 읽은 것을 "빈 파일"로 여기고 덮어쓰면 기록이 통째로 사라지므로).
|
|
103
|
+
*/
|
|
104
|
+
export function readFileIfExists(file) {
|
|
105
|
+
for (let i = 0;; i++) {
|
|
106
|
+
try {
|
|
107
|
+
return fs.readFileSync(file, 'utf8');
|
|
108
|
+
}
|
|
109
|
+
catch (e) {
|
|
110
|
+
if (e.code === 'ENOENT')
|
|
111
|
+
return undefined;
|
|
112
|
+
if (i >= 5)
|
|
113
|
+
throw e;
|
|
114
|
+
Atomics.wait(syncWait, 0, 0, 10);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
73
117
|
}
|
|
74
118
|
/** 임시 파일에 쓰고 rename으로 교체. Windows에서 다른 프로세스가 읽는 중이면 잠깐 실패할 수 있어 재시도한다. */
|
|
75
|
-
export function writeFileAtomic(file, data) {
|
|
119
|
+
export async function writeFileAtomic(file, data) {
|
|
76
120
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
77
121
|
const tmp = `${file}.${process.pid}.${Math.random().toString(36).slice(2)}.tmp`;
|
|
78
122
|
fs.writeFileSync(tmp, data);
|
|
@@ -90,8 +134,7 @@ export function writeFileAtomic(file, data) {
|
|
|
90
134
|
catch { /* 무시 */ }
|
|
91
135
|
throw e;
|
|
92
136
|
}
|
|
93
|
-
|
|
94
|
-
while (Date.now() < until) { /* 짧게 대기 */ }
|
|
137
|
+
await sleep(10);
|
|
95
138
|
}
|
|
96
139
|
}
|
|
97
140
|
}
|
|
@@ -2,18 +2,22 @@ import fs from 'node:fs';
|
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { paths } from '../paths.js';
|
|
4
4
|
import { nowIso, sleep } from '../util.js';
|
|
5
|
-
import { withLock, writeFileAtomic } from './lock.js';
|
|
5
|
+
import { readFileIfExists, withLock, writeFileAtomic } from './lock.js';
|
|
6
6
|
const MAX_LINES = 1000;
|
|
7
7
|
const KEEP_LINES = 500;
|
|
8
8
|
const FIRST_VISIT_BACKLOG = 20;
|
|
9
|
-
|
|
9
|
+
/** forWrite: 고쳐 쓸 쪽이면 잠깐 못 읽었을 때 던진다 (빈 목록으로 여기면 seq가 1부터 다시 매겨짐). */
|
|
10
|
+
function readAll(slot, forWrite = false) {
|
|
10
11
|
let raw;
|
|
11
12
|
try {
|
|
12
|
-
raw =
|
|
13
|
+
raw = readFileIfExists(paths.messages(slot));
|
|
13
14
|
}
|
|
14
|
-
catch {
|
|
15
|
-
|
|
15
|
+
catch (e) {
|
|
16
|
+
if (forWrite)
|
|
17
|
+
throw e;
|
|
16
18
|
}
|
|
19
|
+
if (raw === undefined)
|
|
20
|
+
return [];
|
|
17
21
|
const out = [];
|
|
18
22
|
for (const line of raw.split('\n')) {
|
|
19
23
|
if (!line.trim())
|
|
@@ -27,23 +31,34 @@ function readAll(slot) {
|
|
|
27
31
|
}
|
|
28
32
|
return out;
|
|
29
33
|
}
|
|
30
|
-
|
|
34
|
+
/** forWrite: 고쳐 쓸 쪽이면 잠깐 못 읽었을 때 던진다 (빈 커서로 덮어쓰면 모두가 메시지를 다시 받음). */
|
|
35
|
+
function readCursors(slot, forWrite = false) {
|
|
36
|
+
let raw;
|
|
37
|
+
try {
|
|
38
|
+
raw = readFileIfExists(paths.cursors(slot));
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
if (forWrite)
|
|
42
|
+
throw e;
|
|
43
|
+
}
|
|
44
|
+
if (raw === undefined)
|
|
45
|
+
return {};
|
|
31
46
|
try {
|
|
32
|
-
return JSON.parse(
|
|
47
|
+
return JSON.parse(raw);
|
|
33
48
|
}
|
|
34
49
|
catch {
|
|
35
50
|
return {};
|
|
36
51
|
}
|
|
37
52
|
}
|
|
38
53
|
export async function sendMessage(slot, from, text, to) {
|
|
39
|
-
return withLock(() => {
|
|
40
|
-
const all = readAll(slot);
|
|
54
|
+
return withLock(async () => {
|
|
55
|
+
const all = readAll(slot, true);
|
|
41
56
|
const msg = { seq: (all.at(-1)?.seq ?? 0) + 1, at: nowIso(), from, to, text };
|
|
42
57
|
const file = paths.messages(slot);
|
|
43
58
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
44
59
|
if (all.length + 1 > MAX_LINES) {
|
|
45
60
|
const kept = [...all.slice(-(KEEP_LINES - 1)), msg];
|
|
46
|
-
writeFileAtomic(file, kept.map((m) => JSON.stringify(m)).join('\n') + '\n');
|
|
61
|
+
await writeFileAtomic(file, kept.map((m) => JSON.stringify(m)).join('\n') + '\n');
|
|
47
62
|
}
|
|
48
63
|
else {
|
|
49
64
|
fs.appendFileSync(file, JSON.stringify(msg) + '\n');
|
|
@@ -68,14 +83,14 @@ export async function takeUnread(slot, agent) {
|
|
|
68
83
|
unread = all.filter((m) => m.seq > cursor && visibleTo(agent, m));
|
|
69
84
|
}
|
|
70
85
|
if (cursor !== lastSeq) {
|
|
71
|
-
await withLock(() => {
|
|
72
|
-
const c = readCursors(slot);
|
|
86
|
+
await withLock(async () => {
|
|
87
|
+
const c = readCursors(slot, true);
|
|
73
88
|
c[agent] = Math.max(c[agent] ?? 0, lastSeq);
|
|
74
89
|
// 에이전트 이름이 계속 늘어나도 파일이 커지지 않게, 가장 오래 안 읽은 이름부터 지운다.
|
|
75
90
|
const names = Object.keys(c);
|
|
76
91
|
if (names.length > 200)
|
|
77
92
|
names.sort((a, b) => c[a] - c[b]).slice(0, names.length - 200).forEach((n) => delete c[n]);
|
|
78
|
-
writeFileAtomic(paths.cursors(slot), JSON.stringify(c, null, 2));
|
|
93
|
+
await writeFileAtomic(paths.cursors(slot), JSON.stringify(c, null, 2));
|
|
79
94
|
});
|
|
80
95
|
}
|
|
81
96
|
return unread;
|
package/dist/registry/store.js
CHANGED
|
@@ -1,24 +1,36 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
1
|
import { paths } from '../paths.js';
|
|
3
|
-
import { withLock, writeFileAtomic } from './lock.js';
|
|
2
|
+
import { readFileIfExists, withLock, writeFileAtomic } from './lock.js';
|
|
4
3
|
const empty = () => ({ version: 1, slots: {} });
|
|
4
|
+
/** 읽기만 하는 쪽: 못 읽으면 빈 저장소로 본다. */
|
|
5
5
|
export function readRegistry() {
|
|
6
6
|
try {
|
|
7
|
-
|
|
7
|
+
return readRegistryForWrite();
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return empty();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/** 고쳐 쓸 쪽: 파일이 없거나 깨졌으면 빈 저장소, 잠깐 못 읽으면 던진다 (빈 저장소로 덮어쓰지 않게). */
|
|
14
|
+
function readRegistryForWrite() {
|
|
15
|
+
const raw = readFileIfExists(paths.registry());
|
|
16
|
+
if (raw === undefined)
|
|
17
|
+
return empty();
|
|
18
|
+
try {
|
|
19
|
+
const r = JSON.parse(raw);
|
|
8
20
|
if (r && r.version === 1 && r.slots)
|
|
9
21
|
return r;
|
|
10
22
|
}
|
|
11
23
|
catch {
|
|
12
|
-
//
|
|
24
|
+
// 깨졌으면 빈 저장소
|
|
13
25
|
}
|
|
14
26
|
return empty();
|
|
15
27
|
}
|
|
16
28
|
/** 잠금을 잡고 읽고-고치고-쓴다. mutator의 반환값을 돌려준다. */
|
|
17
29
|
export async function updateRegistry(mutator) {
|
|
18
|
-
return withLock(() => {
|
|
19
|
-
const r =
|
|
30
|
+
return withLock(async () => {
|
|
31
|
+
const r = readRegistryForWrite();
|
|
20
32
|
const result = mutator(r);
|
|
21
|
-
writeFileAtomic(paths.registry(), JSON.stringify(r, null, 2));
|
|
33
|
+
await writeFileAtomic(paths.registry(), JSON.stringify(r, null, 2));
|
|
22
34
|
return result;
|
|
23
35
|
});
|
|
24
36
|
}
|
package/dist/service.js
CHANGED
|
@@ -9,7 +9,7 @@ import { withSlotLock } from './registry/lock.js';
|
|
|
9
9
|
import { newSlotRecord, readRegistry, updateRegistry } from './registry/store.js';
|
|
10
10
|
import { MAX_SLOT, portOf } from './slots.js';
|
|
11
11
|
import { applyDock, DOCK_SUPPORTED } from './taskbar/dock.js';
|
|
12
|
-
import { applyTaskbar } from './taskbar/watcher.js';
|
|
12
|
+
import { applyTaskbar, canApplyTaskbar } from './taskbar/watcher.js';
|
|
13
13
|
import { ago, nowIso, pidAlive, secondsSince, sleep } from './util.js';
|
|
14
14
|
export const EXTERNAL = '(external)';
|
|
15
15
|
/** 이 프로세스가 Chrome에게 직접 물어(또는 직접 띄워) 맞다고 확인한 슬롯 → Chrome 본체 번호 */
|
|
@@ -34,7 +34,7 @@ export const notRunningMessage = (slot) => `Chrome on slot ${slot} is off. CDPM
|
|
|
34
34
|
`Ask the user whether to start it; if yes, use browser_launch.`;
|
|
35
35
|
/** 떠 있는 슬롯에 붙는다 (기록·작업표시줄 맞춤). 꺼져 있으면 켜지 않고 오류. */
|
|
36
36
|
/** 포트 확인. 늦게 답하면(바쁜 Chrome) 길게 한 번 더 묻는다. */
|
|
37
|
-
async function probeSure(slot) {
|
|
37
|
+
export async function probeSure(slot) {
|
|
38
38
|
let p = await probe(slot);
|
|
39
39
|
if (p.state === 'slow')
|
|
40
40
|
p = await probe(slot, 8000);
|
|
@@ -125,8 +125,8 @@ async function launchSlotLocked(slot, agent, opts) {
|
|
|
125
125
|
// 처음 열린 탭을 지금(이 프로세스 안에서) 기록해야 "연 사람"이 실행한 에이전트로 남는다.
|
|
126
126
|
await syncTabs(slot).catch(() => undefined);
|
|
127
127
|
// 창이 뜨는 데 시간이 걸리므로 창이 나타날 때까지 적용을 시도 (창이 나타나는 것이 신호; 이후는 watcher가 맡음).
|
|
128
|
-
// 헤드리스는 창이
|
|
129
|
-
for (let i = 0; !opts.headless && i < 50; i++) {
|
|
128
|
+
// 헤드리스는 창이 없고, 아이콘을 붙일 수 없는 환경(Windows 외, koffi 없음)이면 기다릴 이유가 없다.
|
|
129
|
+
for (let i = 0; !opts.headless && canApplyTaskbar(pid) && i < 50; i++) {
|
|
130
130
|
if (applyTaskbar(slot, pid) > 0)
|
|
131
131
|
break;
|
|
132
132
|
await sleep(100);
|
|
@@ -211,7 +211,7 @@ export async function restartIfUpdated(slot, browser) {
|
|
|
211
211
|
for (const u of urls.slice(1))
|
|
212
212
|
await conn?.send('Target.createTarget', { url: u, background: true }).catch(() => undefined);
|
|
213
213
|
await syncTabs(slot).catch(() => undefined);
|
|
214
|
-
for (let i = 0; !rec?.headless && i < 50; i++) {
|
|
214
|
+
for (let i = 0; !rec?.headless && canApplyTaskbar(pid) && i < 50; i++) {
|
|
215
215
|
if (applyTaskbar(slot, pid) > 0)
|
|
216
216
|
break;
|
|
217
217
|
await sleep(100);
|
package/dist/taskbar/dock.js
CHANGED
|
@@ -19,37 +19,37 @@ const pngPath = (slot) => path.join(path.dirname(paths.icon(slot)), `${slot}-doc
|
|
|
19
19
|
* macOS Dock 아이콘 격자에 맞춰 둘레에 여백(약 10%)을 둔다.
|
|
20
20
|
*/
|
|
21
21
|
export function drawScript(slot, color) {
|
|
22
|
-
return `(async () => {
|
|
23
|
-
const S = ${SIZE}, pad = Math.round(S * 0.1), C = S - pad * 2, k = C / 64;
|
|
24
|
-
const cv = new OffscreenCanvas(S, S), g = cv.getContext('2d');
|
|
25
|
-
const hex = ${JSON.stringify(color)}, r = parseInt(hex.slice(1, 3), 16), gr = parseInt(hex.slice(3, 5), 16), b = parseInt(hex.slice(5, 7), 16);
|
|
26
|
-
const rgba = (a) => 'rgba(' + r + ',' + gr + ',' + b + ',' + a + ')';
|
|
27
|
-
const rr = (x, y, w, h, rad) => { g.beginPath(); g.roundRect(x, y, w, h, rad); };
|
|
28
|
-
const radius = Math.round(11 * k), barH = Math.round(16 * k), pw = Math.max(1, Math.round(k));
|
|
29
|
-
g.save();
|
|
30
|
-
rr(pad, pad, C, C, radius); g.clip();
|
|
31
|
-
const grad = g.createLinearGradient(0, pad, 0, pad + C); grad.addColorStop(0, rgba(1)); grad.addColorStop(1, rgba(205 / 255));
|
|
32
|
-
g.fillStyle = grad; g.fillRect(pad, pad, C, C);
|
|
33
|
-
g.fillStyle = 'rgba(0,0,0,' + (85 / 255) + ')'; g.fillRect(pad, pad, C, barH);
|
|
34
|
-
g.fillStyle = 'rgba(255,255,255,' + (70 / 255) + ')'; g.fillRect(pad, pad + barH, C, pw);
|
|
35
|
-
g.restore();
|
|
36
|
-
const dotD = Math.max(2, Math.round(6.4 * k)), gap = Math.max(1, Math.round(2.6 * k)), dotX = pad + Math.max(1, Math.round(6.5 * k));
|
|
37
|
-
g.fillStyle = 'rgba(255,255,255,' + (245 / 255) + ')';
|
|
38
|
-
for (let i = 0; i < 3; i++) { g.beginPath(); g.arc(dotX + i * (dotD + gap) + dotD / 2, pad + barH / 2, dotD / 2, 0, Math.PI * 2); g.fill(); }
|
|
39
|
-
g.strokeStyle = 'rgba(255,255,255,' + (128 / 255) + ')'; g.lineWidth = pw;
|
|
40
|
-
rr(pad + pw / 2, pad + pw / 2, C - pw, C - pw, Math.max(1, radius - pw / 2)); g.stroke();
|
|
41
|
-
const text = ${JSON.stringify(String(slot))};
|
|
42
|
-
g.font = 'bold ' + Math.round(33 * k) + 'px -apple-system, "SF Pro Display", "Helvetica Neue", Arial, sans-serif';
|
|
43
|
-
g.textBaseline = 'alphabetic'; g.textAlign = 'left';
|
|
44
|
-
const m = g.measureText(text);
|
|
45
|
-
const inkW = m.actualBoundingBoxLeft + m.actualBoundingBoxRight, inkH = m.actualBoundingBoxAscent + m.actualBoundingBoxDescent;
|
|
46
|
-
const top = pad + barH + pw, bottom = pad + C - pw;
|
|
47
|
-
const x = pad + (C - inkW) / 2 + m.actualBoundingBoxLeft;
|
|
48
|
-
const y = top + (bottom - top - inkH) / 2 + m.actualBoundingBoxAscent;
|
|
49
|
-
g.fillStyle = '#fff'; g.fillText(text, x, y);
|
|
50
|
-
const bytes = new Uint8Array(await (await cv.convertToBlob({ type: 'image/png' })).arrayBuffer());
|
|
51
|
-
let bin = ''; for (let i = 0; i < bytes.length; i += 0x8000) bin += String.fromCharCode.apply(null, bytes.subarray(i, i + 0x8000));
|
|
52
|
-
return btoa(bin);
|
|
22
|
+
return `(async () => {
|
|
23
|
+
const S = ${SIZE}, pad = Math.round(S * 0.1), C = S - pad * 2, k = C / 64;
|
|
24
|
+
const cv = new OffscreenCanvas(S, S), g = cv.getContext('2d');
|
|
25
|
+
const hex = ${JSON.stringify(color)}, r = parseInt(hex.slice(1, 3), 16), gr = parseInt(hex.slice(3, 5), 16), b = parseInt(hex.slice(5, 7), 16);
|
|
26
|
+
const rgba = (a) => 'rgba(' + r + ',' + gr + ',' + b + ',' + a + ')';
|
|
27
|
+
const rr = (x, y, w, h, rad) => { g.beginPath(); g.roundRect(x, y, w, h, rad); };
|
|
28
|
+
const radius = Math.round(11 * k), barH = Math.round(16 * k), pw = Math.max(1, Math.round(k));
|
|
29
|
+
g.save();
|
|
30
|
+
rr(pad, pad, C, C, radius); g.clip();
|
|
31
|
+
const grad = g.createLinearGradient(0, pad, 0, pad + C); grad.addColorStop(0, rgba(1)); grad.addColorStop(1, rgba(205 / 255));
|
|
32
|
+
g.fillStyle = grad; g.fillRect(pad, pad, C, C);
|
|
33
|
+
g.fillStyle = 'rgba(0,0,0,' + (85 / 255) + ')'; g.fillRect(pad, pad, C, barH);
|
|
34
|
+
g.fillStyle = 'rgba(255,255,255,' + (70 / 255) + ')'; g.fillRect(pad, pad + barH, C, pw);
|
|
35
|
+
g.restore();
|
|
36
|
+
const dotD = Math.max(2, Math.round(6.4 * k)), gap = Math.max(1, Math.round(2.6 * k)), dotX = pad + Math.max(1, Math.round(6.5 * k));
|
|
37
|
+
g.fillStyle = 'rgba(255,255,255,' + (245 / 255) + ')';
|
|
38
|
+
for (let i = 0; i < 3; i++) { g.beginPath(); g.arc(dotX + i * (dotD + gap) + dotD / 2, pad + barH / 2, dotD / 2, 0, Math.PI * 2); g.fill(); }
|
|
39
|
+
g.strokeStyle = 'rgba(255,255,255,' + (128 / 255) + ')'; g.lineWidth = pw;
|
|
40
|
+
rr(pad + pw / 2, pad + pw / 2, C - pw, C - pw, Math.max(1, radius - pw / 2)); g.stroke();
|
|
41
|
+
const text = ${JSON.stringify(String(slot))};
|
|
42
|
+
g.font = 'bold ' + Math.round(33 * k) + 'px -apple-system, "SF Pro Display", "Helvetica Neue", Arial, sans-serif';
|
|
43
|
+
g.textBaseline = 'alphabetic'; g.textAlign = 'left';
|
|
44
|
+
const m = g.measureText(text);
|
|
45
|
+
const inkW = m.actualBoundingBoxLeft + m.actualBoundingBoxRight, inkH = m.actualBoundingBoxAscent + m.actualBoundingBoxDescent;
|
|
46
|
+
const top = pad + barH + pw, bottom = pad + C - pw;
|
|
47
|
+
const x = pad + (C - inkW) / 2 + m.actualBoundingBoxLeft;
|
|
48
|
+
const y = top + (bottom - top - inkH) / 2 + m.actualBoundingBoxAscent;
|
|
49
|
+
g.fillStyle = '#fff'; g.fillText(text, x, y);
|
|
50
|
+
const bytes = new Uint8Array(await (await cv.convertToBlob({ type: 'image/png' })).arrayBuffer());
|
|
51
|
+
let bin = ''; for (let i = 0; i < bytes.length; i += 0x8000) bin += String.fromCharCode.apply(null, bytes.subarray(i, i + 0x8000));
|
|
52
|
+
return btoa(bin);
|
|
53
53
|
})()`;
|
|
54
54
|
}
|
|
55
55
|
/**
|