@slopit/adapter-shared 0.1.0
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/dist/index.d.ts +880 -0
- package/dist/index.js +672 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
import { createBehavioralCapture } from '@slopit/behavioral';
|
|
2
|
+
import { generateSessionId, SCHEMA_VERSION, captureEnvironment, generateTrialId } from '@slopit/core';
|
|
3
|
+
export { captureEnvironment } from '@slopit/core';
|
|
4
|
+
|
|
5
|
+
// src/base-recorder.ts
|
|
6
|
+
function createSessionMetadata(platform, options) {
|
|
7
|
+
const sessionId = generateSessionId();
|
|
8
|
+
const startTime = options?.startTime ?? Date.now();
|
|
9
|
+
return {
|
|
10
|
+
schemaVersion: SCHEMA_VERSION,
|
|
11
|
+
sessionId,
|
|
12
|
+
...options?.participantId !== void 0 ? { participantId: options.participantId } : {},
|
|
13
|
+
...options?.studyId !== void 0 ? { studyId: options.studyId } : {},
|
|
14
|
+
platform,
|
|
15
|
+
environment: captureEnvironment(),
|
|
16
|
+
timing: {
|
|
17
|
+
startTime
|
|
18
|
+
},
|
|
19
|
+
...options?.metadata !== void 0 ? { metadata: options.metadata } : {}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function completeSession(sessionBase, trials, options) {
|
|
23
|
+
const endTime = options?.endTime ?? Date.now();
|
|
24
|
+
const duration = endTime - sessionBase.timing.startTime;
|
|
25
|
+
return {
|
|
26
|
+
...sessionBase,
|
|
27
|
+
timing: {
|
|
28
|
+
startTime: sessionBase.timing.startTime,
|
|
29
|
+
endTime,
|
|
30
|
+
duration
|
|
31
|
+
},
|
|
32
|
+
trials,
|
|
33
|
+
globalEvents: {
|
|
34
|
+
focus: [],
|
|
35
|
+
errors: []
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function convertTrialToSlopit(data) {
|
|
40
|
+
const trial = {
|
|
41
|
+
trialId: data.trialId,
|
|
42
|
+
trialIndex: data.trialIndex,
|
|
43
|
+
startTime: data.startTime,
|
|
44
|
+
endTime: data.endTime,
|
|
45
|
+
rt: data.endTime - data.startTime
|
|
46
|
+
};
|
|
47
|
+
if (data.trialType !== void 0) {
|
|
48
|
+
trial.trialType = data.trialType;
|
|
49
|
+
}
|
|
50
|
+
if (data.stimulus !== void 0) {
|
|
51
|
+
trial.stimulus = data.stimulus;
|
|
52
|
+
}
|
|
53
|
+
if (data.response !== void 0) {
|
|
54
|
+
trial.response = data.response;
|
|
55
|
+
}
|
|
56
|
+
if (data.behavioral !== void 0) {
|
|
57
|
+
trial.behavioral = data.behavioral;
|
|
58
|
+
}
|
|
59
|
+
if (data.flags !== void 0 && data.flags.length > 0) {
|
|
60
|
+
trial.captureFlags = data.flags;
|
|
61
|
+
}
|
|
62
|
+
if (data.platformData !== void 0) {
|
|
63
|
+
trial.platformData = data.platformData;
|
|
64
|
+
}
|
|
65
|
+
return trial;
|
|
66
|
+
}
|
|
67
|
+
function convertCompletedTrialToSlopit(trial) {
|
|
68
|
+
return convertTrialToSlopit({
|
|
69
|
+
trialId: trial.trialId,
|
|
70
|
+
trialIndex: trial.trialIndex,
|
|
71
|
+
trialType: trial.trialType,
|
|
72
|
+
startTime: trial.startTime,
|
|
73
|
+
endTime: trial.endTime,
|
|
74
|
+
stimulus: trial.stimulus,
|
|
75
|
+
response: trial.response,
|
|
76
|
+
behavioral: trial.behavioral,
|
|
77
|
+
flags: trial.captureFlags,
|
|
78
|
+
platformData: trial.platformData
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function createTextResponse(text) {
|
|
82
|
+
const words = text.trim().split(/\s+/).filter((w) => w.length > 0);
|
|
83
|
+
return {
|
|
84
|
+
type: "text",
|
|
85
|
+
value: text,
|
|
86
|
+
characterCount: text.length,
|
|
87
|
+
wordCount: words.length
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/base-recorder.ts
|
|
92
|
+
var BaseRecorder = class {
|
|
93
|
+
/**
|
|
94
|
+
* Unique session identifier.
|
|
95
|
+
*/
|
|
96
|
+
sessionId;
|
|
97
|
+
/**
|
|
98
|
+
* Participant identifier from recruitment platform.
|
|
99
|
+
*/
|
|
100
|
+
participantId;
|
|
101
|
+
/**
|
|
102
|
+
* Study identifier.
|
|
103
|
+
*/
|
|
104
|
+
studyId;
|
|
105
|
+
/**
|
|
106
|
+
* Session-level metadata.
|
|
107
|
+
*/
|
|
108
|
+
metadata;
|
|
109
|
+
/**
|
|
110
|
+
* Configuration for behavioral capture.
|
|
111
|
+
*/
|
|
112
|
+
behavioralConfig;
|
|
113
|
+
/**
|
|
114
|
+
* Completed trials.
|
|
115
|
+
*/
|
|
116
|
+
trials;
|
|
117
|
+
/**
|
|
118
|
+
* Current trial state, if a trial is in progress.
|
|
119
|
+
*/
|
|
120
|
+
currentTrial;
|
|
121
|
+
/**
|
|
122
|
+
* Behavioral capture instance for the current trial.
|
|
123
|
+
*/
|
|
124
|
+
capture;
|
|
125
|
+
/**
|
|
126
|
+
* Session start time in milliseconds (Unix timestamp).
|
|
127
|
+
*/
|
|
128
|
+
sessionStartTime;
|
|
129
|
+
/**
|
|
130
|
+
* Counter for auto-incrementing trial indices.
|
|
131
|
+
*/
|
|
132
|
+
trialCounter;
|
|
133
|
+
/**
|
|
134
|
+
* Creates a new BaseRecorder.
|
|
135
|
+
*
|
|
136
|
+
* @param config - Recorder configuration
|
|
137
|
+
*/
|
|
138
|
+
constructor(config) {
|
|
139
|
+
this.sessionId = crypto.randomUUID();
|
|
140
|
+
this.participantId = config?.participantId;
|
|
141
|
+
this.studyId = config?.studyId;
|
|
142
|
+
this.metadata = config?.metadata;
|
|
143
|
+
this.behavioralConfig = config?.behavioral ?? {};
|
|
144
|
+
this.trials = [];
|
|
145
|
+
this.currentTrial = null;
|
|
146
|
+
this.capture = null;
|
|
147
|
+
this.sessionStartTime = Date.now();
|
|
148
|
+
this.trialCounter = 0;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Starts a new trial.
|
|
152
|
+
*
|
|
153
|
+
* Creates a new trial state and optionally attaches behavioral capture
|
|
154
|
+
* to the target element.
|
|
155
|
+
*
|
|
156
|
+
* @param config - Trial configuration
|
|
157
|
+
* @throws Error if a trial is already in progress
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```typescript
|
|
161
|
+
* recorder.startTrial({
|
|
162
|
+
* trialType: "survey-text",
|
|
163
|
+
* targetElement: document.querySelector("textarea"),
|
|
164
|
+
* });
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
startTrial(config) {
|
|
168
|
+
if (this.currentTrial !== null) {
|
|
169
|
+
throw new Error("A trial is already in progress. Call endTrial() first.");
|
|
170
|
+
}
|
|
171
|
+
const trialId = config.trialId ?? generateTrialId();
|
|
172
|
+
const trialIndex = config.trialIndex ?? this.trialCounter++;
|
|
173
|
+
const startTime = performance.now();
|
|
174
|
+
this.currentTrial = {
|
|
175
|
+
trialId,
|
|
176
|
+
trialIndex,
|
|
177
|
+
trialType: config.trialType,
|
|
178
|
+
startTime,
|
|
179
|
+
stimulus: config.stimulus,
|
|
180
|
+
element: config.targetElement
|
|
181
|
+
};
|
|
182
|
+
if (config.targetElement !== void 0) {
|
|
183
|
+
this.attachCapture(config.targetElement, startTime);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Ends the current trial and returns the completed trial data.
|
|
188
|
+
*
|
|
189
|
+
* @param response - Optional response information
|
|
190
|
+
* @param platformData - Optional platform-specific trial data
|
|
191
|
+
* @returns The completed trial
|
|
192
|
+
* @throws Error if no trial is in progress
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```typescript
|
|
196
|
+
* const trial = recorder.endTrial({
|
|
197
|
+
* type: "text",
|
|
198
|
+
* value: "User response",
|
|
199
|
+
* characterCount: 13,
|
|
200
|
+
* wordCount: 2,
|
|
201
|
+
* });
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
endTrial(response, platformData) {
|
|
205
|
+
if (this.currentTrial === null) {
|
|
206
|
+
throw new Error("No trial in progress. Call startTrial() first.");
|
|
207
|
+
}
|
|
208
|
+
const endTime = performance.now();
|
|
209
|
+
const sessionStartPerf = performance.now() - (Date.now() - this.sessionStartTime);
|
|
210
|
+
const startTimeAbsolute = this.sessionStartTime + (this.currentTrial.startTime - sessionStartPerf);
|
|
211
|
+
const endTimeAbsolute = this.sessionStartTime + (endTime - sessionStartPerf);
|
|
212
|
+
const behavioral = this.capture?.getData();
|
|
213
|
+
const captureFlags = this.capture?.getFlags();
|
|
214
|
+
if (this.capture !== null) {
|
|
215
|
+
this.capture.detach();
|
|
216
|
+
this.capture = null;
|
|
217
|
+
}
|
|
218
|
+
const completedTrial = {
|
|
219
|
+
trialId: this.currentTrial.trialId,
|
|
220
|
+
trialIndex: this.currentTrial.trialIndex,
|
|
221
|
+
trialType: this.currentTrial.trialType,
|
|
222
|
+
startTime: startTimeAbsolute,
|
|
223
|
+
endTime: endTimeAbsolute,
|
|
224
|
+
stimulus: this.currentTrial.stimulus,
|
|
225
|
+
response,
|
|
226
|
+
behavioral,
|
|
227
|
+
captureFlags: captureFlags !== void 0 && captureFlags.length > 0 ? captureFlags : void 0,
|
|
228
|
+
platformData
|
|
229
|
+
};
|
|
230
|
+
this.trials.push(completedTrial);
|
|
231
|
+
this.currentTrial = null;
|
|
232
|
+
return completedTrial;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Attaches behavioral capture to an element for the current trial.
|
|
236
|
+
*
|
|
237
|
+
* This can be called after startTrial() if the target element becomes
|
|
238
|
+
* available later (e.g., dynamically created elements).
|
|
239
|
+
*
|
|
240
|
+
* @param element - The DOM element to attach to
|
|
241
|
+
* @throws Error if no trial is in progress
|
|
242
|
+
* @throws Error if capture is already attached
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* ```typescript
|
|
246
|
+
* recorder.startTrial({ trialType: "survey" });
|
|
247
|
+
* // Later, when the element is available:
|
|
248
|
+
* recorder.attachCapture(document.querySelector("textarea")!);
|
|
249
|
+
* ```
|
|
250
|
+
*/
|
|
251
|
+
attachCapture(element, startTime) {
|
|
252
|
+
if (this.currentTrial === null) {
|
|
253
|
+
throw new Error("No trial in progress. Call startTrial() first.");
|
|
254
|
+
}
|
|
255
|
+
if (this.capture !== null) {
|
|
256
|
+
throw new Error("Capture already attached. Call detachCapture() first.");
|
|
257
|
+
}
|
|
258
|
+
this.capture = createBehavioralCapture(this.behavioralConfig);
|
|
259
|
+
this.capture.attach(element, startTime ?? this.currentTrial.startTime);
|
|
260
|
+
this.currentTrial.element = element;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Detaches behavioral capture without ending the trial.
|
|
264
|
+
*
|
|
265
|
+
* Useful when the target element is removed before the trial ends.
|
|
266
|
+
*/
|
|
267
|
+
detachCapture() {
|
|
268
|
+
if (this.capture !== null) {
|
|
269
|
+
this.capture.detach();
|
|
270
|
+
this.capture = null;
|
|
271
|
+
}
|
|
272
|
+
if (this.currentTrial !== null) {
|
|
273
|
+
this.currentTrial.element = void 0;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Exports the session data in SlopitSession format.
|
|
278
|
+
*
|
|
279
|
+
* @returns Complete session data
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```typescript
|
|
283
|
+
* const session = recorder.exportSession();
|
|
284
|
+
* await fetch("/api/sessions", {
|
|
285
|
+
* method: "POST",
|
|
286
|
+
* body: JSON.stringify(session),
|
|
287
|
+
* });
|
|
288
|
+
* ```
|
|
289
|
+
*/
|
|
290
|
+
exportSession() {
|
|
291
|
+
const options = {
|
|
292
|
+
startTime: this.sessionStartTime
|
|
293
|
+
};
|
|
294
|
+
if (this.participantId !== void 0) {
|
|
295
|
+
options.participantId = this.participantId;
|
|
296
|
+
}
|
|
297
|
+
if (this.studyId !== void 0) {
|
|
298
|
+
options.studyId = this.studyId;
|
|
299
|
+
}
|
|
300
|
+
if (this.metadata !== void 0) {
|
|
301
|
+
options.metadata = this.metadata;
|
|
302
|
+
}
|
|
303
|
+
const sessionBase = createSessionMetadata(this.getPlatformInfo(), options);
|
|
304
|
+
const baseWithSessionId = {
|
|
305
|
+
...sessionBase,
|
|
306
|
+
sessionId: this.sessionId
|
|
307
|
+
};
|
|
308
|
+
const slopitTrials = this.trials.map(convertCompletedTrialToSlopit);
|
|
309
|
+
return completeSession(baseWithSessionId, slopitTrials);
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Gets the current trial state.
|
|
313
|
+
*
|
|
314
|
+
* @returns Current trial state or null if no trial is in progress
|
|
315
|
+
*/
|
|
316
|
+
getCurrentTrial() {
|
|
317
|
+
return this.currentTrial;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Gets all completed trials.
|
|
321
|
+
*
|
|
322
|
+
* @returns Array of completed trials
|
|
323
|
+
*/
|
|
324
|
+
getTrials() {
|
|
325
|
+
return [...this.trials];
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Checks if a trial is currently in progress.
|
|
329
|
+
*
|
|
330
|
+
* @returns True if a trial is in progress
|
|
331
|
+
*/
|
|
332
|
+
isTrialInProgress() {
|
|
333
|
+
return this.currentTrial !== null;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Gets the session ID.
|
|
337
|
+
*
|
|
338
|
+
* @returns The session identifier
|
|
339
|
+
*/
|
|
340
|
+
getSessionId() {
|
|
341
|
+
return this.sessionId;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Resets the recorder for a new session.
|
|
345
|
+
*
|
|
346
|
+
* Clears all trials and generates a new session ID.
|
|
347
|
+
*/
|
|
348
|
+
reset() {
|
|
349
|
+
if (this.capture !== null) {
|
|
350
|
+
this.capture.detach();
|
|
351
|
+
this.capture = null;
|
|
352
|
+
}
|
|
353
|
+
this.currentTrial = null;
|
|
354
|
+
this.trials = [];
|
|
355
|
+
this.sessionId = crypto.randomUUID();
|
|
356
|
+
this.sessionStartTime = Date.now();
|
|
357
|
+
this.trialCounter = 0;
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
// src/element-discovery.ts
|
|
362
|
+
function discoverElement(options) {
|
|
363
|
+
const { selector, container, timeout = 5e3, onFound, onTimeout } = options;
|
|
364
|
+
let observer = null;
|
|
365
|
+
let timeoutId = null;
|
|
366
|
+
let found = false;
|
|
367
|
+
const cleanup = () => {
|
|
368
|
+
if (observer !== null) {
|
|
369
|
+
observer.disconnect();
|
|
370
|
+
observer = null;
|
|
371
|
+
}
|
|
372
|
+
if (timeoutId !== null) {
|
|
373
|
+
clearTimeout(timeoutId);
|
|
374
|
+
timeoutId = null;
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
const root = container instanceof Document ? container : container;
|
|
378
|
+
const existing = root.querySelector(selector);
|
|
379
|
+
if (existing !== null) {
|
|
380
|
+
found = true;
|
|
381
|
+
onFound(existing);
|
|
382
|
+
return cleanup;
|
|
383
|
+
}
|
|
384
|
+
if (timeout > 0) {
|
|
385
|
+
timeoutId = setTimeout(() => {
|
|
386
|
+
cleanup();
|
|
387
|
+
if (!found && onTimeout !== void 0) {
|
|
388
|
+
onTimeout();
|
|
389
|
+
}
|
|
390
|
+
}, timeout);
|
|
391
|
+
}
|
|
392
|
+
observer = new MutationObserver(() => {
|
|
393
|
+
if (found) return;
|
|
394
|
+
const target = root.querySelector(selector);
|
|
395
|
+
if (target !== null) {
|
|
396
|
+
found = true;
|
|
397
|
+
cleanup();
|
|
398
|
+
onFound(target);
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
const observeTarget = container instanceof Document ? container.body : container;
|
|
402
|
+
observer.observe(observeTarget, {
|
|
403
|
+
childList: true,
|
|
404
|
+
subtree: true
|
|
405
|
+
});
|
|
406
|
+
return cleanup;
|
|
407
|
+
}
|
|
408
|
+
function discoverElements(options) {
|
|
409
|
+
const { selector, container, minCount = 1, timeout = 5e3, onFound, onTimeout } = options;
|
|
410
|
+
let observer = null;
|
|
411
|
+
let timeoutId = null;
|
|
412
|
+
let found = false;
|
|
413
|
+
const cleanup = () => {
|
|
414
|
+
if (observer !== null) {
|
|
415
|
+
observer.disconnect();
|
|
416
|
+
observer = null;
|
|
417
|
+
}
|
|
418
|
+
if (timeoutId !== null) {
|
|
419
|
+
clearTimeout(timeoutId);
|
|
420
|
+
timeoutId = null;
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
const root = container instanceof Document ? container : container;
|
|
424
|
+
const existing = Array.from(root.querySelectorAll(selector));
|
|
425
|
+
if (existing.length >= minCount) {
|
|
426
|
+
found = true;
|
|
427
|
+
onFound(existing);
|
|
428
|
+
return cleanup;
|
|
429
|
+
}
|
|
430
|
+
if (timeout > 0) {
|
|
431
|
+
timeoutId = setTimeout(() => {
|
|
432
|
+
cleanup();
|
|
433
|
+
if (!found) {
|
|
434
|
+
const partial = Array.from(root.querySelectorAll(selector));
|
|
435
|
+
if (onTimeout !== void 0) {
|
|
436
|
+
onTimeout(partial);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}, timeout);
|
|
440
|
+
}
|
|
441
|
+
observer = new MutationObserver(() => {
|
|
442
|
+
if (found) return;
|
|
443
|
+
const targets = Array.from(root.querySelectorAll(selector));
|
|
444
|
+
if (targets.length >= minCount) {
|
|
445
|
+
found = true;
|
|
446
|
+
cleanup();
|
|
447
|
+
onFound(targets);
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
const observeTarget = container instanceof Document ? container.body : container;
|
|
451
|
+
observer.observe(observeTarget, {
|
|
452
|
+
childList: true,
|
|
453
|
+
subtree: true
|
|
454
|
+
});
|
|
455
|
+
return cleanup;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// src/test-utils.ts
|
|
459
|
+
function createMockTextarea(id) {
|
|
460
|
+
const textarea = document.createElement("textarea");
|
|
461
|
+
if (id !== void 0) {
|
|
462
|
+
textarea.id = id;
|
|
463
|
+
}
|
|
464
|
+
textarea.style.width = "400px";
|
|
465
|
+
textarea.style.height = "200px";
|
|
466
|
+
return textarea;
|
|
467
|
+
}
|
|
468
|
+
function createMockInput(id, type = "text") {
|
|
469
|
+
const input = document.createElement("input");
|
|
470
|
+
input.type = type;
|
|
471
|
+
if (id !== void 0) {
|
|
472
|
+
input.id = id;
|
|
473
|
+
}
|
|
474
|
+
return input;
|
|
475
|
+
}
|
|
476
|
+
function simulateKeystroke(element, key, options) {
|
|
477
|
+
const code = options?.code ?? `Key${key.toUpperCase()}`;
|
|
478
|
+
const modifiers = options?.modifiers ?? {};
|
|
479
|
+
const includeKeyUp = options?.includeKeyUp ?? true;
|
|
480
|
+
const eventInit = {
|
|
481
|
+
key,
|
|
482
|
+
code,
|
|
483
|
+
bubbles: true,
|
|
484
|
+
cancelable: true,
|
|
485
|
+
ctrlKey: modifiers.ctrl ?? false,
|
|
486
|
+
altKey: modifiers.alt ?? false,
|
|
487
|
+
shiftKey: modifiers.shift ?? false,
|
|
488
|
+
metaKey: modifiers.meta ?? false
|
|
489
|
+
};
|
|
490
|
+
const keydownEvent = new KeyboardEvent("keydown", eventInit);
|
|
491
|
+
element.dispatchEvent(keydownEvent);
|
|
492
|
+
if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement) {
|
|
493
|
+
if (key.length === 1 && !modifiers.ctrl && !modifiers.meta) {
|
|
494
|
+
element.value += key;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
if (includeKeyUp) {
|
|
498
|
+
const keyupEvent = new KeyboardEvent("keyup", eventInit);
|
|
499
|
+
element.dispatchEvent(keyupEvent);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
async function simulateTyping(element, text, delayMs = 50) {
|
|
503
|
+
for (const char of text) {
|
|
504
|
+
simulateKeystroke(element, char);
|
|
505
|
+
await delay(delayMs);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
function simulatePaste(element, text, options) {
|
|
509
|
+
const blocked = options?.blocked ?? false;
|
|
510
|
+
const dataTransfer = new MockDataTransfer();
|
|
511
|
+
dataTransfer.setData("text/plain", text);
|
|
512
|
+
const pasteEvent = new MockClipboardEvent("paste", {
|
|
513
|
+
clipboardData: dataTransfer,
|
|
514
|
+
bubbles: true,
|
|
515
|
+
cancelable: true
|
|
516
|
+
});
|
|
517
|
+
if (blocked) {
|
|
518
|
+
element.addEventListener(
|
|
519
|
+
"paste",
|
|
520
|
+
(e) => {
|
|
521
|
+
e.preventDefault();
|
|
522
|
+
},
|
|
523
|
+
{ once: true }
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
element.dispatchEvent(pasteEvent);
|
|
527
|
+
if (!blocked && (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement)) {
|
|
528
|
+
element.value += text;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
function simulateCopy(element, text) {
|
|
532
|
+
const dataTransfer = new MockDataTransfer();
|
|
533
|
+
dataTransfer.setData("text/plain", text);
|
|
534
|
+
const copyEvent = new MockClipboardEvent("copy", {
|
|
535
|
+
clipboardData: dataTransfer,
|
|
536
|
+
bubbles: true,
|
|
537
|
+
cancelable: true
|
|
538
|
+
});
|
|
539
|
+
element.dispatchEvent(copyEvent);
|
|
540
|
+
}
|
|
541
|
+
function simulateFocusBlur(element) {
|
|
542
|
+
element.dispatchEvent(new FocusEvent("focus", { bubbles: true }));
|
|
543
|
+
element.dispatchEvent(new FocusEvent("blur", { bubbles: true }));
|
|
544
|
+
}
|
|
545
|
+
function simulateFocus(element) {
|
|
546
|
+
element.dispatchEvent(new FocusEvent("focus", { bubbles: true }));
|
|
547
|
+
}
|
|
548
|
+
function simulateBlur(element) {
|
|
549
|
+
element.dispatchEvent(new FocusEvent("blur", { bubbles: true }));
|
|
550
|
+
}
|
|
551
|
+
function simulateClick(element, x = 0, y = 0) {
|
|
552
|
+
const eventInit = {
|
|
553
|
+
bubbles: true,
|
|
554
|
+
cancelable: true,
|
|
555
|
+
clientX: x,
|
|
556
|
+
clientY: y
|
|
557
|
+
};
|
|
558
|
+
element.dispatchEvent(new MouseEvent("mousedown", eventInit));
|
|
559
|
+
element.dispatchEvent(new MouseEvent("mouseup", eventInit));
|
|
560
|
+
element.dispatchEvent(new MouseEvent("click", eventInit));
|
|
561
|
+
}
|
|
562
|
+
function simulateMouseMove(element, x, y) {
|
|
563
|
+
element.dispatchEvent(
|
|
564
|
+
new MouseEvent("mousemove", {
|
|
565
|
+
bubbles: true,
|
|
566
|
+
cancelable: true,
|
|
567
|
+
clientX: x,
|
|
568
|
+
clientY: y
|
|
569
|
+
})
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
function createMockBehavioralData() {
|
|
573
|
+
return {
|
|
574
|
+
keystrokes: [
|
|
575
|
+
{
|
|
576
|
+
key: "H",
|
|
577
|
+
code: "KeyH",
|
|
578
|
+
event: "keydown",
|
|
579
|
+
time: 100,
|
|
580
|
+
modifiers: { ctrl: false, alt: false, shift: true, meta: false }
|
|
581
|
+
},
|
|
582
|
+
{
|
|
583
|
+
key: "i",
|
|
584
|
+
code: "KeyI",
|
|
585
|
+
event: "keydown",
|
|
586
|
+
time: 200,
|
|
587
|
+
modifiers: { ctrl: false, alt: false, shift: false, meta: false }
|
|
588
|
+
},
|
|
589
|
+
{
|
|
590
|
+
key: "!",
|
|
591
|
+
code: "Digit1",
|
|
592
|
+
event: "keydown",
|
|
593
|
+
time: 300,
|
|
594
|
+
modifiers: { ctrl: false, alt: false, shift: true, meta: false }
|
|
595
|
+
}
|
|
596
|
+
],
|
|
597
|
+
focus: [
|
|
598
|
+
{ event: "focus", time: 50 },
|
|
599
|
+
{ event: "blur", time: 400 }
|
|
600
|
+
],
|
|
601
|
+
paste: [],
|
|
602
|
+
mouse: [],
|
|
603
|
+
scroll: [],
|
|
604
|
+
clipboard: [],
|
|
605
|
+
inputDuration: [],
|
|
606
|
+
metrics: {
|
|
607
|
+
keystroke: {
|
|
608
|
+
totalKeystrokes: 3,
|
|
609
|
+
printableKeystrokes: 3,
|
|
610
|
+
deletions: 0,
|
|
611
|
+
meanIKI: 100,
|
|
612
|
+
stdIKI: 0,
|
|
613
|
+
medianIKI: 100,
|
|
614
|
+
pauseCount: 0,
|
|
615
|
+
productProcessRatio: 1
|
|
616
|
+
},
|
|
617
|
+
focus: {
|
|
618
|
+
blurCount: 1,
|
|
619
|
+
totalBlurDuration: 0,
|
|
620
|
+
hiddenCount: 0,
|
|
621
|
+
totalHiddenDuration: 0
|
|
622
|
+
},
|
|
623
|
+
timing: {
|
|
624
|
+
firstKeystrokeLatency: 100,
|
|
625
|
+
totalResponseTime: 350,
|
|
626
|
+
charactersPerMinute: 514
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
var MockDataTransfer = class {
|
|
632
|
+
data = /* @__PURE__ */ new Map();
|
|
633
|
+
setData(format, data) {
|
|
634
|
+
this.data.set(format, data);
|
|
635
|
+
}
|
|
636
|
+
getData(format) {
|
|
637
|
+
return this.data.get(format) ?? "";
|
|
638
|
+
}
|
|
639
|
+
clearData(format) {
|
|
640
|
+
if (format !== void 0) {
|
|
641
|
+
this.data.delete(format);
|
|
642
|
+
} else {
|
|
643
|
+
this.data.clear();
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
get types() {
|
|
647
|
+
return Array.from(this.data.keys());
|
|
648
|
+
}
|
|
649
|
+
};
|
|
650
|
+
var MockClipboardEvent = class extends Event {
|
|
651
|
+
clipboardData;
|
|
652
|
+
constructor(type, init) {
|
|
653
|
+
super(type, { bubbles: init?.bubbles ?? true, cancelable: init?.cancelable ?? true });
|
|
654
|
+
this.clipboardData = init?.clipboardData ?? new MockDataTransfer();
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
function delay(ms) {
|
|
658
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
659
|
+
}
|
|
660
|
+
async function waitFor(condition, timeout = 5e3, interval = 50) {
|
|
661
|
+
const startTime = Date.now();
|
|
662
|
+
while (!condition()) {
|
|
663
|
+
if (Date.now() - startTime > timeout) {
|
|
664
|
+
throw new Error("waitFor timeout exceeded");
|
|
665
|
+
}
|
|
666
|
+
await delay(interval);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
export { BaseRecorder, MockClipboardEvent, MockDataTransfer, completeSession, convertCompletedTrialToSlopit, convertTrialToSlopit, createMockBehavioralData, createMockInput, createMockTextarea, createSessionMetadata, createTextResponse, delay, discoverElement, discoverElements, simulateBlur, simulateClick, simulateCopy, simulateFocus, simulateFocusBlur, simulateKeystroke, simulateMouseMove, simulatePaste, simulateTyping, waitFor };
|
|
671
|
+
//# sourceMappingURL=index.js.map
|
|
672
|
+
//# sourceMappingURL=index.js.map
|