-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecorder.html
More file actions
424 lines (387 loc) · 15.9 KB
/
Copy pathrecorder.html
File metadata and controls
424 lines (387 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>SmartMouse Recorder</title>
<style>
body { font-family: Arial, sans-serif; margin: 0; background: #f3f4f6; color: #111827; }
.container { max-width: 1100px; margin: 24px auto; background: #fff; border-radius: 10px; padding: 20px; box-shadow: 0 8px 20px rgba(0,0,0,.08); }
h1 { margin: 0 0 12px; }
.muted { color: #6b7280; font-size: 13px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; margin: 10px 0; }
input, select, textarea, button { border: 1px solid #d1d5db; border-radius: 8px; padding: 8px 10px; font-size: 14px; }
input, select, textarea { background: #fff; }
button { background: #111827; color: #fff; cursor: pointer; }
button.secondary { background: #fff; color: #111827; }
.card { border: 1px solid #e5e7eb; border-radius: 10px; padding: 14px; margin-top: 14px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { border-bottom: 1px solid #e5e7eb; text-align: left; padding: 8px; vertical-align: top; }
.mono { font-family: Consolas, Menlo, monospace; }
.pill { display: inline-block; border: 1px solid #d1d5db; border-radius: 999px; padding: 2px 8px; font-size: 12px; }
.grow { flex: 1; min-width: 220px; }
</style>
</head>
<body>
<div class="container">
<h1>🎬 SmartMouse Recorder (Hardcoded Workflow Mode)</h1>
<div class="muted">Use this page to record deterministic steps (click/type/wait), then replay them with Chrome-first hardcoded and optional variables like <span class="mono">{{comment_text}}</span>.</div>
<div class="card">
<h3 style="margin-top:0">1) Template Info</h3>
<div class="row">
<input id="templateTitle" class="grow" placeholder="Template title (e.g., Comment on YouTube video)" />
<input id="templateName" placeholder="template-name (auto if empty)" />
<input id="startDelay" type="number" min="0" value="3" style="width:120px" title="Start delay seconds" />
<input id="defaultStepDelay" type="number" min="0" value="20000" style="width:160px" title="Default step delay ms" />
</div>
<div class="row">
<label><input type="checkbox" id="openChromeFirst" checked /> Open Chrome first (hardcoded)</label>
<label><input type="checkbox" id="stopOnError" checked /> Stop on first error</label>
</div>
</div>
<div class="card">
<h3 style="margin-top:0">2) Live Capture (Automatic)</h3>
<div class="row">
<button id="startLive" type="button">Start Live Capture</button>
<button id="stopLive" type="button" class="secondary">Stop & Save</button>
<span id="liveStatus" class="pill">idle</span>
<span id="liveCount" class="muted">events: 0</span>
</div>
<div class="muted">Click <b>Start Live Capture</b>, do your real mouse/keyboard steps anywhere, then press <b>F8</b> to stop and save without returning here. (<b>Stop & Save</b> also works, but returning can add extra clicks.)</div>
</div>
<div class="card">
<h3 style="margin-top:0">3) Add Step (Manual)</h3>
<div class="row">
<select id="actionType">
<option value="click">click</option>
<option value="double_click">double_click</option>
<option value="right_click">right_click</option>
<option value="type">type</option>
<option value="press">press</option>
<option value="hotkey">hotkey</option>
<option value="scroll">scroll</option>
<option value="wait">wait</option>
<option value="open_url">open_url</option>
<option value="open_app">open_app</option>
<option value="move">move</option>
</select>
<input id="x" type="number" placeholder="x" style="width:90px" />
<input id="y" type="number" placeholder="y" style="width:90px" />
<button id="captureCursor" type="button" class="secondary">Capture Cursor</button>
<input id="text" class="grow" placeholder="text / url / app / key / combo (comma for hotkey)" />
<input id="delayBefore" type="number" min="0" placeholder="delay_before_ms" style="width:140px" />
<button id="addStep" type="button">Add Step</button>
</div>
<div class="muted">Tip: For comments use text like <span class="mono">{{comment_text}}</span>. For waits use action <span class="mono">wait</span> + text field seconds value.</div>
</div>
<div class="card">
<h3 style="margin-top:0">4) Steps</h3>
<table>
<thead>
<tr>
<th>#</th>
<th>Action</th>
<th>Payload</th>
<th>Delay (ms)</th>
<th></th>
</tr>
</thead>
<tbody id="stepsBody"></tbody>
</table>
<div class="row" style="margin-top:10px">
<button id="saveTemplate" type="button">Save Template</button>
<button id="clearSteps" type="button" class="secondary">Clear Steps</button>
<button id="refreshTemplates" type="button" class="secondary">Refresh Templates</button>
</div>
</div>
<div class="card">
<h3 style="margin-top:0">5) Run Template</h3>
<div class="row">
<select id="templateSelect" class="grow"></select>
<input id="varComment" class="grow" placeholder="comment_text variable" />
<input id="varKeyword" placeholder="keyword variable" />
<input id="runStartDelay" type="number" min="0" value="3" style="width:120px" />
<button id="runTemplate" type="button">Run</button>
</div>
<div class="muted">Playback uses your saved hardcoded steps. Start delay gives you time to prepare Chrome/page.</div>
</div>
<div class="card">
<h3 style="margin-top:0">Logs</h3>
<div id="logs" class="mono" style="white-space:pre-wrap; max-height:260px; overflow:auto"></div>
</div>
</div>
<script>
const API = window.location.origin;
const steps = [];
const byId = (id) => document.getElementById(id);
const logs = byId('logs');
function log(msg, isError = false) {
const time = new Date().toLocaleTimeString();
logs.textContent += `[${time}] ${msg}\n`;
logs.scrollTop = logs.scrollHeight;
if (isError) console.error(msg); else console.log(msg);
}
async function apiJson(url, options) {
const res = await fetch(url, options);
const contentType = (res.headers.get('content-type') || '').toLowerCase();
const text = await res.text();
if (!contentType.includes('application/json')) {
throw new Error(`Expected JSON but got ${contentType || 'unknown content type'}. Likely old SmartMouse port. Open recorder from active updated port.`);
}
let data;
try {
data = JSON.parse(text);
} catch {
throw new Error('Response was not valid JSON. Check SmartMouse instance/port.');
}
if (!res.ok) {
throw new Error(data.error || data.message || `HTTP ${res.status}`);
}
return data;
}
function setLiveStatus(active, eventCount) {
const status = byId('liveStatus');
const count = byId('liveCount');
status.textContent = active ? 'recording' : 'idle';
status.style.borderColor = active ? '#059669' : '#d1d5db';
status.style.color = active ? '#059669' : '#111827';
count.textContent = `events: ${eventCount || 0}`;
}
async function pollLiveStatus() {
try {
const data = await apiJson(`${API}/recordings/live/status`);
setLiveStatus(Boolean(data.active), Number(data.event_count || 0));
} catch {
setLiveStatus(false, 0);
}
}
function renderSteps() {
const body = byId('stepsBody');
body.innerHTML = '';
steps.forEach((step, index) => {
const tr = document.createElement('tr');
const payload = JSON.stringify(step, null, 0);
tr.innerHTML = `
<td>${index + 1}</td>
<td><span class="pill">${step.action}</span></td>
<td class="mono">${payload}</td>
<td>${step.delay_before_ms || 0}</td>
<td><button data-i="${index}" class="secondary remove-step" type="button">Remove</button></td>
`;
body.appendChild(tr);
});
document.querySelectorAll('.remove-step').forEach((btn) => {
btn.onclick = () => {
const i = Number(btn.dataset.i);
steps.splice(i, 1);
renderSteps();
};
});
}
function getNameFromTitle(title) {
return String(title || '')
.trim().toLowerCase()
.replace(/[^a-z0-9-_]+/g, '-')
.replace(/-{2,}/g, '-')
.replace(/^-|-$/g, '')
.slice(0, 80);
}
async function refreshTemplates() {
try {
const data = await apiJson(`${API}/recordings`);
const select = byId('templateSelect');
select.innerHTML = '';
(data.templates || []).forEach((tpl) => {
const option = document.createElement('option');
option.value = tpl.name;
option.textContent = `${tpl.title} (${tpl.step_count} steps)`;
select.appendChild(option);
});
log(`Loaded ${data.count || 0} templates.`);
} catch (e) {
log(`Failed to load templates: ${e.message}`, true);
}
}
byId('captureCursor').onclick = async () => {
try {
const pos = await apiJson(`${API}/cursor`);
if (!pos.success) {
log(`Cursor capture failed: ${pos.message}`, true);
return;
}
byId('x').value = pos.x;
byId('y').value = pos.y;
log(`Captured cursor (${pos.x}, ${pos.y}).`);
} catch (e) {
log(`Cursor capture error: ${e.message}`, true);
}
};
byId('addStep').onclick = () => {
const action = byId('actionType').value;
const x = byId('x').value;
const y = byId('y').value;
const textRaw = byId('text').value;
const delayRaw = byId('delayBefore').value;
const defaultDelay = Number(byId('defaultStepDelay').value || 0);
const step = { action };
if (x !== '') step.x = Number(x);
if (y !== '') step.y = Number(y);
if (action === 'type') step.text = textRaw;
else if (action === 'press') step.key = textRaw || 'enter';
else if (action === 'hotkey') step.combo = textRaw.split(',').map(s => s.trim()).filter(Boolean);
else if (action === 'scroll') {
const low = (textRaw || '').toLowerCase();
step.direction = low === 'up' ? 'up' : 'down';
step.amount = 3;
} else if (action === 'wait') step.seconds = Number(textRaw || 2);
else if (action === 'open_url') step.url = textRaw;
else if (action === 'open_app') step.app_name = textRaw || 'chrome';
else if (textRaw) step.note = textRaw;
const delay = delayRaw === '' ? defaultDelay : Number(delayRaw);
if (Number.isFinite(delay) && delay > 0) step.delay_before_ms = delay;
steps.push(step);
renderSteps();
log(`Added step: ${action}`);
};
byId('clearSteps').onclick = () => {
steps.length = 0;
renderSteps();
log('Cleared all steps.');
};
byId('saveTemplate').onclick = async () => {
const title = byId('templateTitle').value.trim();
const explicitName = byId('templateName').value.trim();
const name = explicitName || getNameFromTitle(title);
if (!title) {
log('Template title is required.', true);
return;
}
if (!name) {
log('Template name is empty after normalization.', true);
return;
}
if (steps.length === 0) {
log('Add at least one step before saving.', true);
return;
}
const payload = {
name,
title,
open_chrome_first: byId('openChromeFirst').checked,
stop_on_error: byId('stopOnError').checked,
start_delay_seconds: Number(byId('startDelay').value || 3),
steps
};
try {
const data = await apiJson(`${API}/recordings`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
if (!data.success) {
log(`Save failed: ${data.error || 'unknown error'}`, true);
return;
}
log(`Saved template: ${data.template.name}`);
await refreshTemplates();
} catch (e) {
log(`Save error: ${e.message}`, true);
}
};
byId('refreshTemplates').onclick = refreshTemplates;
byId('startLive').onclick = async () => {
const title = byId('templateTitle').value.trim();
const explicitName = byId('templateName').value.trim();
const name = explicitName || getNameFromTitle(title);
if (!title) {
log('Template title is required before live capture.', true);
return;
}
try {
const payload = {
title,
name,
open_chrome_first: byId('openChromeFirst').checked,
stop_on_error: byId('stopOnError').checked,
start_delay_seconds: Number(byId('startDelay').value || 3),
default_delay_ms: Number(byId('defaultStepDelay').value || 20000),
initial_ignore_ms: 3000
};
const data = await apiJson(`${API}/recordings/live/start`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
if (!data.success) {
log(`Live start failed: ${data.error || 'unknown error'}`, true);
return;
}
log('Live capture started. Chrome opened first. Perform your task, then press F8 anywhere to stop & save.');
await pollLiveStatus();
} catch (e) {
log(`Live start error: ${e.message}`, true);
}
};
byId('stopLive').onclick = async () => {
try {
const data = await apiJson(`${API}/recordings/live/stop`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ save: true })
});
if (!data.success) {
log(`Live stop failed: ${data.error || 'unknown error'}`, true);
return;
}
const tpl = data.template;
if (tpl && Array.isArray(tpl.steps)) {
steps.length = 0;
tpl.steps.forEach((step) => steps.push(step));
renderSteps();
byId('templateName').value = tpl.name;
byId('templateTitle').value = tpl.title;
log(`Live capture saved: ${tpl.name} (${tpl.steps.length} steps).`);
} else {
log('Live capture stopped, but no template returned.', true);
}
await refreshTemplates();
await pollLiveStatus();
} catch (e) {
log(`Live stop error: ${e.message}`, true);
}
};
byId('runTemplate').onclick = async () => {
const templateName = byId('templateSelect').value;
if (!templateName) {
log('Select a template first.', true);
return;
}
const vars = {
comment_text: byId('varComment').value,
keyword: byId('varKeyword').value
};
const startDelay = Number(byId('runStartDelay').value || 3);
try {
log(`Running template ${templateName} with ${startDelay}s countdown...`);
const data = await apiJson(`${API}/recordings/${encodeURIComponent(templateName)}/run`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ vars, start_delay_seconds: startDelay })
});
if (!data.success) {
log(`Run failed: ${data.error || data.reason || 'unknown error'}`, true);
return;
}
log(`Run completed: status=${data.status} url=${data.final_url || 'n/a'}`);
} catch (e) {
log(`Run error: ${e.message}`, true);
}
};
refreshTemplates();
renderSteps();
setInterval(pollLiveStatus, 1500);
pollLiveStatus();
log('Recorder ready.');
</script>
</body>
</html>