@slopit/adapter-osweb 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 +275 -0
- package/dist/index.js +309 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aaron Steven White
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { RecorderConfig, BaseRecorder, TrialConfig, CompletedTrial, AdapterData } from '@slopit/adapter-shared';
|
|
2
|
+
import { PlatformInfo, ResponseInfo } from '@slopit/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Type definitions for OSWeb adapter.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Configuration for OSWebRecorder.
|
|
12
|
+
*
|
|
13
|
+
* Extends the base RecorderConfig with OSWeb-specific options.
|
|
14
|
+
*/
|
|
15
|
+
interface OSWebRecorderConfig extends RecorderConfig {
|
|
16
|
+
/**
|
|
17
|
+
* OpenSesame vars object for storing data.
|
|
18
|
+
*
|
|
19
|
+
* When provided, the recorder can automatically extract participantId
|
|
20
|
+
* from `vars.subject_nr` and store session data in the vars object.
|
|
21
|
+
*/
|
|
22
|
+
varsObject?: OSWebVars;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* OpenSesame vars object interface.
|
|
26
|
+
*
|
|
27
|
+
* This object is available in OSWeb inline_javascript items as `vars`
|
|
28
|
+
* and contains experiment variables and participant data.
|
|
29
|
+
*/
|
|
30
|
+
interface OSWebVars {
|
|
31
|
+
/**
|
|
32
|
+
* Allow access to arbitrary experiment variables.
|
|
33
|
+
*/
|
|
34
|
+
[key: string]: unknown;
|
|
35
|
+
/**
|
|
36
|
+
* Participant number assigned by OpenSesame.
|
|
37
|
+
*/
|
|
38
|
+
subject_nr?: number;
|
|
39
|
+
/**
|
|
40
|
+
* Participant parity ("odd" or "even").
|
|
41
|
+
*/
|
|
42
|
+
subject_parity?: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* OSWeb runner interface.
|
|
46
|
+
*
|
|
47
|
+
* The runner object provides access to the experiment state in OSWeb.
|
|
48
|
+
* This interface documents the structure for type safety.
|
|
49
|
+
*/
|
|
50
|
+
interface OSWebRunner {
|
|
51
|
+
/**
|
|
52
|
+
* Current experiment state.
|
|
53
|
+
*/
|
|
54
|
+
experiment: {
|
|
55
|
+
/**
|
|
56
|
+
* Experiment variables object.
|
|
57
|
+
*/
|
|
58
|
+
vars: OSWebVars;
|
|
59
|
+
/**
|
|
60
|
+
* Experiment items (trials, sequences, etc.).
|
|
61
|
+
*/
|
|
62
|
+
items: Record<string, unknown>;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* OSWeb session recorder.
|
|
68
|
+
*
|
|
69
|
+
* @module
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* SlopitRecorder for OSWeb experiments.
|
|
74
|
+
*
|
|
75
|
+
* Due to OSWeb's limited JavaScript sandbox, this recorder uses a
|
|
76
|
+
* prepare/attach pattern that works within inline_javascript items.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```javascript
|
|
80
|
+
* // In prepare phase of inline_javascript
|
|
81
|
+
* const slopit = new SlopitRecorder({ varsObject: vars });
|
|
82
|
+
* slopit.prepareCapture();
|
|
83
|
+
*
|
|
84
|
+
* // In run phase
|
|
85
|
+
* await slopit.attachWhenReady("textarea");
|
|
86
|
+
* // ... wait for response ...
|
|
87
|
+
* slopit.endTrial(vars.response);
|
|
88
|
+
* vars.slopit_data = JSON.stringify(slopit.getTrialData());
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
declare class SlopitRecorder extends BaseRecorder {
|
|
92
|
+
/**
|
|
93
|
+
* Reference to OSWeb vars object for storing data.
|
|
94
|
+
*/
|
|
95
|
+
private varsObject;
|
|
96
|
+
/**
|
|
97
|
+
* Cleanup function for element discovery.
|
|
98
|
+
*/
|
|
99
|
+
private discoveryCleanup;
|
|
100
|
+
/**
|
|
101
|
+
* Whether prepareCapture() has been called for the current trial.
|
|
102
|
+
*/
|
|
103
|
+
private prepared;
|
|
104
|
+
/**
|
|
105
|
+
* Data from the most recently completed trial.
|
|
106
|
+
*/
|
|
107
|
+
private lastTrialData;
|
|
108
|
+
/**
|
|
109
|
+
* Creates a new SlopitRecorder.
|
|
110
|
+
*
|
|
111
|
+
* @param config - Recorder configuration
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```javascript
|
|
115
|
+
* const recorder = new SlopitRecorder({
|
|
116
|
+
* varsObject: vars,
|
|
117
|
+
* studyId: "my-study",
|
|
118
|
+
* });
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
constructor(config?: OSWebRecorderConfig);
|
|
122
|
+
/**
|
|
123
|
+
* Gets platform information for OSWeb.
|
|
124
|
+
*
|
|
125
|
+
* @returns Platform information
|
|
126
|
+
*/
|
|
127
|
+
protected getPlatformInfo(): PlatformInfo;
|
|
128
|
+
/**
|
|
129
|
+
* Attempts to detect the OSWeb version.
|
|
130
|
+
*
|
|
131
|
+
* @returns Version string or "unknown" if not detectable
|
|
132
|
+
*/
|
|
133
|
+
private detectOSWebVersion;
|
|
134
|
+
/**
|
|
135
|
+
* Prepares capture for the next trial.
|
|
136
|
+
*
|
|
137
|
+
* Call this in the prepare phase of an inline_javascript item.
|
|
138
|
+
* This resets the trial state and prepares for element attachment.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```javascript
|
|
142
|
+
* // In prepare phase
|
|
143
|
+
* slopit.prepareCapture();
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
prepareCapture(): void;
|
|
147
|
+
/**
|
|
148
|
+
* Attaches to an element when it becomes available.
|
|
149
|
+
*
|
|
150
|
+
* Call this in the run phase. Uses MutationObserver to wait for
|
|
151
|
+
* dynamically created elements.
|
|
152
|
+
*
|
|
153
|
+
* @param selector - CSS selector for target element
|
|
154
|
+
* @param timeout - Max time to wait in milliseconds (default: 10000)
|
|
155
|
+
* @returns Promise that resolves when attached
|
|
156
|
+
* @throws Error if prepareCapture() was not called first
|
|
157
|
+
* @throws Error if element not found within timeout
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```javascript
|
|
161
|
+
* // In run phase
|
|
162
|
+
* await slopit.attachWhenReady("textarea");
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
attachWhenReady(selector: string, timeout?: number): Promise<void>;
|
|
166
|
+
/**
|
|
167
|
+
* Starts a trial manually with a custom ID.
|
|
168
|
+
*
|
|
169
|
+
* Alternative to attachWhenReady() when you have direct access to
|
|
170
|
+
* the target element.
|
|
171
|
+
*
|
|
172
|
+
* @param trialId - Unique identifier for the trial
|
|
173
|
+
* @param element - Target DOM element for behavioral capture
|
|
174
|
+
* @param trialConfig - Additional trial configuration
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```javascript
|
|
178
|
+
* const textarea = document.querySelector("textarea");
|
|
179
|
+
* slopit.startTrialWithId("response-1", textarea);
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
startTrialWithId(trialId: string, element?: HTMLElement, trialConfig?: Partial<TrialConfig>): void;
|
|
183
|
+
/**
|
|
184
|
+
* Starts a trial manually.
|
|
185
|
+
*
|
|
186
|
+
* Convenience wrapper that uses startTrialWithId internally.
|
|
187
|
+
*
|
|
188
|
+
* @param trialId - Unique identifier for the trial
|
|
189
|
+
* @param element - Target DOM element for behavioral capture
|
|
190
|
+
* @param trialConfig - Additional trial configuration
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```javascript
|
|
194
|
+
* const textarea = document.querySelector("textarea");
|
|
195
|
+
* slopit.startTrial("response-1", textarea);
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
startTrial(trialId: string, element?: HTMLElement, trialConfig?: Partial<TrialConfig>): void;
|
|
199
|
+
startTrial(config: TrialConfig): void;
|
|
200
|
+
/**
|
|
201
|
+
* Ends the current trial and stores data.
|
|
202
|
+
*
|
|
203
|
+
* @param response - The participant's response (text or ResponseInfo object)
|
|
204
|
+
* @returns The completed trial data
|
|
205
|
+
* @throws Error if no trial is in progress
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```javascript
|
|
209
|
+
* // With text response
|
|
210
|
+
* slopit.endTrial(vars.response);
|
|
211
|
+
*
|
|
212
|
+
* // With ResponseInfo object
|
|
213
|
+
* slopit.endTrial({
|
|
214
|
+
* type: "text",
|
|
215
|
+
* value: vars.response,
|
|
216
|
+
* characterCount: vars.response.length,
|
|
217
|
+
* });
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
endTrial(response?: string | ResponseInfo): CompletedTrial;
|
|
221
|
+
/**
|
|
222
|
+
* Gets the last trial's behavioral data.
|
|
223
|
+
*
|
|
224
|
+
* Use this to access data for storage in OSWeb vars.
|
|
225
|
+
*
|
|
226
|
+
* @returns AdapterData or null if no trial completed
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```javascript
|
|
230
|
+
* const data = slopit.getTrialData();
|
|
231
|
+
* if (data) {
|
|
232
|
+
* vars.keystroke_count = data.behavioral.keystrokes.length;
|
|
233
|
+
* }
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
getTrialData(): AdapterData | null;
|
|
237
|
+
/**
|
|
238
|
+
* Gets the last trial's data as JSON string.
|
|
239
|
+
*
|
|
240
|
+
* Convenience method for storing in OSWeb vars.
|
|
241
|
+
*
|
|
242
|
+
* @returns JSON string of trial data
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* ```javascript
|
|
246
|
+
* vars.slopit_data = slopit.getTrialDataJSON();
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
getTrialDataJSON(): string;
|
|
250
|
+
/**
|
|
251
|
+
* Stores session data in OSWeb vars object.
|
|
252
|
+
*
|
|
253
|
+
* @param key - Variable name (default: "slopit_session")
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```javascript
|
|
257
|
+
* // At end of experiment
|
|
258
|
+
* slopit.storeInVars("slopit_session");
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
261
|
+
storeInVars(key?: string): void;
|
|
262
|
+
/**
|
|
263
|
+
* Exports session as JSON string.
|
|
264
|
+
*
|
|
265
|
+
* @returns JSON string of complete session data
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```javascript
|
|
269
|
+
* console.log(slopit.exportSessionJSON());
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
exportSessionJSON(): string;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export { SlopitRecorder as OSWebRecorder, type OSWebRecorderConfig, type OSWebRunner, type OSWebVars, SlopitRecorder };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { BaseRecorder, discoverElement } from '@slopit/adapter-shared';
|
|
2
|
+
|
|
3
|
+
// src/recorder.ts
|
|
4
|
+
var SlopitRecorder = class extends BaseRecorder {
|
|
5
|
+
/**
|
|
6
|
+
* Reference to OSWeb vars object for storing data.
|
|
7
|
+
*/
|
|
8
|
+
varsObject = null;
|
|
9
|
+
/**
|
|
10
|
+
* Cleanup function for element discovery.
|
|
11
|
+
*/
|
|
12
|
+
discoveryCleanup = null;
|
|
13
|
+
/**
|
|
14
|
+
* Whether prepareCapture() has been called for the current trial.
|
|
15
|
+
*/
|
|
16
|
+
prepared = false;
|
|
17
|
+
/**
|
|
18
|
+
* Data from the most recently completed trial.
|
|
19
|
+
*/
|
|
20
|
+
lastTrialData = null;
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new SlopitRecorder.
|
|
23
|
+
*
|
|
24
|
+
* @param config - Recorder configuration
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```javascript
|
|
28
|
+
* const recorder = new SlopitRecorder({
|
|
29
|
+
* varsObject: vars,
|
|
30
|
+
* studyId: "my-study",
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
constructor(config) {
|
|
35
|
+
const vars = config?.varsObject;
|
|
36
|
+
const participantId = config?.participantId ?? vars?.subject_nr?.toString();
|
|
37
|
+
const enhancedConfig = participantId !== void 0 ? {
|
|
38
|
+
...config,
|
|
39
|
+
participantId
|
|
40
|
+
} : config;
|
|
41
|
+
super(enhancedConfig);
|
|
42
|
+
this.varsObject = vars ?? null;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Gets platform information for OSWeb.
|
|
46
|
+
*
|
|
47
|
+
* @returns Platform information
|
|
48
|
+
*/
|
|
49
|
+
getPlatformInfo() {
|
|
50
|
+
return {
|
|
51
|
+
name: "osweb",
|
|
52
|
+
version: this.detectOSWebVersion(),
|
|
53
|
+
adapterVersion: "0.1.0"
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Attempts to detect the OSWeb version.
|
|
58
|
+
*
|
|
59
|
+
* @returns Version string or "unknown" if not detectable
|
|
60
|
+
*/
|
|
61
|
+
detectOSWebVersion() {
|
|
62
|
+
if (typeof window !== "undefined") {
|
|
63
|
+
const oswebGlobal = window["osweb"];
|
|
64
|
+
if (oswebGlobal?.version !== void 0) {
|
|
65
|
+
return oswebGlobal.version;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return "unknown";
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Prepares capture for the next trial.
|
|
72
|
+
*
|
|
73
|
+
* Call this in the prepare phase of an inline_javascript item.
|
|
74
|
+
* This resets the trial state and prepares for element attachment.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```javascript
|
|
78
|
+
* // In prepare phase
|
|
79
|
+
* slopit.prepareCapture();
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
prepareCapture() {
|
|
83
|
+
this.prepared = true;
|
|
84
|
+
this.lastTrialData = null;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Attaches to an element when it becomes available.
|
|
88
|
+
*
|
|
89
|
+
* Call this in the run phase. Uses MutationObserver to wait for
|
|
90
|
+
* dynamically created elements.
|
|
91
|
+
*
|
|
92
|
+
* @param selector - CSS selector for target element
|
|
93
|
+
* @param timeout - Max time to wait in milliseconds (default: 10000)
|
|
94
|
+
* @returns Promise that resolves when attached
|
|
95
|
+
* @throws Error if prepareCapture() was not called first
|
|
96
|
+
* @throws Error if element not found within timeout
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```javascript
|
|
100
|
+
* // In run phase
|
|
101
|
+
* await slopit.attachWhenReady("textarea");
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
attachWhenReady(selector, timeout = 1e4) {
|
|
105
|
+
return new Promise((resolve, reject) => {
|
|
106
|
+
if (!this.prepared) {
|
|
107
|
+
reject(new Error("Call prepareCapture() before attachWhenReady()"));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const trialId = `trial_${this.trials.length}`;
|
|
111
|
+
super.startTrial({ trialId });
|
|
112
|
+
const element = document.querySelector(selector);
|
|
113
|
+
if (element !== null) {
|
|
114
|
+
if (this.capture !== null) {
|
|
115
|
+
this.capture.attach(element, this.currentTrial?.startTime ?? performance.now());
|
|
116
|
+
} else {
|
|
117
|
+
this.attachCapture(element);
|
|
118
|
+
}
|
|
119
|
+
resolve();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
this.discoveryCleanup = discoverElement({
|
|
123
|
+
selector,
|
|
124
|
+
container: document.body,
|
|
125
|
+
timeout,
|
|
126
|
+
onFound: (el) => {
|
|
127
|
+
if (this.capture !== null) {
|
|
128
|
+
this.capture.attach(el, this.currentTrial?.startTime ?? performance.now());
|
|
129
|
+
} else {
|
|
130
|
+
this.attachCapture(el);
|
|
131
|
+
}
|
|
132
|
+
resolve();
|
|
133
|
+
},
|
|
134
|
+
onTimeout: () => {
|
|
135
|
+
reject(new Error(`Element "${selector}" not found within ${timeout}ms`));
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Starts a trial manually with a custom ID.
|
|
142
|
+
*
|
|
143
|
+
* Alternative to attachWhenReady() when you have direct access to
|
|
144
|
+
* the target element.
|
|
145
|
+
*
|
|
146
|
+
* @param trialId - Unique identifier for the trial
|
|
147
|
+
* @param element - Target DOM element for behavioral capture
|
|
148
|
+
* @param trialConfig - Additional trial configuration
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```javascript
|
|
152
|
+
* const textarea = document.querySelector("textarea");
|
|
153
|
+
* slopit.startTrialWithId("response-1", textarea);
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
startTrialWithId(trialId, element, trialConfig) {
|
|
157
|
+
const config = {
|
|
158
|
+
trialId,
|
|
159
|
+
...trialConfig
|
|
160
|
+
};
|
|
161
|
+
if (element !== void 0) {
|
|
162
|
+
config.targetElement = element;
|
|
163
|
+
}
|
|
164
|
+
super.startTrial(config);
|
|
165
|
+
}
|
|
166
|
+
startTrial(trialIdOrConfig, element, trialConfig) {
|
|
167
|
+
if (typeof trialIdOrConfig === "string") {
|
|
168
|
+
this.startTrialWithId(trialIdOrConfig, element, trialConfig);
|
|
169
|
+
} else {
|
|
170
|
+
super.startTrial(trialIdOrConfig);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Ends the current trial and stores data.
|
|
175
|
+
*
|
|
176
|
+
* @param response - The participant's response (text or ResponseInfo object)
|
|
177
|
+
* @returns The completed trial data
|
|
178
|
+
* @throws Error if no trial is in progress
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```javascript
|
|
182
|
+
* // With text response
|
|
183
|
+
* slopit.endTrial(vars.response);
|
|
184
|
+
*
|
|
185
|
+
* // With ResponseInfo object
|
|
186
|
+
* slopit.endTrial({
|
|
187
|
+
* type: "text",
|
|
188
|
+
* value: vars.response,
|
|
189
|
+
* characterCount: vars.response.length,
|
|
190
|
+
* });
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
endTrial(response) {
|
|
194
|
+
if (this.discoveryCleanup !== null) {
|
|
195
|
+
this.discoveryCleanup();
|
|
196
|
+
this.discoveryCleanup = null;
|
|
197
|
+
}
|
|
198
|
+
const responseInfo = typeof response === "string" ? {
|
|
199
|
+
type: "text",
|
|
200
|
+
value: response,
|
|
201
|
+
characterCount: response.length,
|
|
202
|
+
wordCount: response.trim().split(/\s+/).filter(Boolean).length
|
|
203
|
+
} : response;
|
|
204
|
+
const trial = super.endTrial(responseInfo);
|
|
205
|
+
this.lastTrialData = {
|
|
206
|
+
behavioral: trial.behavioral ?? {
|
|
207
|
+
keystrokes: [],
|
|
208
|
+
focus: [],
|
|
209
|
+
paste: [],
|
|
210
|
+
mouse: [],
|
|
211
|
+
scroll: [],
|
|
212
|
+
clipboard: [],
|
|
213
|
+
inputDuration: [],
|
|
214
|
+
metrics: {
|
|
215
|
+
keystroke: {
|
|
216
|
+
totalKeystrokes: 0,
|
|
217
|
+
printableKeystrokes: 0,
|
|
218
|
+
deletions: 0,
|
|
219
|
+
meanIKI: 0,
|
|
220
|
+
stdIKI: 0,
|
|
221
|
+
medianIKI: 0,
|
|
222
|
+
pauseCount: 0,
|
|
223
|
+
productProcessRatio: 0
|
|
224
|
+
},
|
|
225
|
+
focus: {
|
|
226
|
+
blurCount: 0,
|
|
227
|
+
totalBlurDuration: 0,
|
|
228
|
+
hiddenCount: 0,
|
|
229
|
+
totalHiddenDuration: 0
|
|
230
|
+
},
|
|
231
|
+
timing: {
|
|
232
|
+
firstKeystrokeLatency: 0,
|
|
233
|
+
totalResponseTime: 0,
|
|
234
|
+
charactersPerMinute: 0
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
},
|
|
238
|
+
flags: trial.captureFlags ?? []
|
|
239
|
+
};
|
|
240
|
+
this.prepared = false;
|
|
241
|
+
return trial;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Gets the last trial's behavioral data.
|
|
245
|
+
*
|
|
246
|
+
* Use this to access data for storage in OSWeb vars.
|
|
247
|
+
*
|
|
248
|
+
* @returns AdapterData or null if no trial completed
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```javascript
|
|
252
|
+
* const data = slopit.getTrialData();
|
|
253
|
+
* if (data) {
|
|
254
|
+
* vars.keystroke_count = data.behavioral.keystrokes.length;
|
|
255
|
+
* }
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
getTrialData() {
|
|
259
|
+
return this.lastTrialData;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Gets the last trial's data as JSON string.
|
|
263
|
+
*
|
|
264
|
+
* Convenience method for storing in OSWeb vars.
|
|
265
|
+
*
|
|
266
|
+
* @returns JSON string of trial data
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* ```javascript
|
|
270
|
+
* vars.slopit_data = slopit.getTrialDataJSON();
|
|
271
|
+
* ```
|
|
272
|
+
*/
|
|
273
|
+
getTrialDataJSON() {
|
|
274
|
+
return JSON.stringify(this.lastTrialData);
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Stores session data in OSWeb vars object.
|
|
278
|
+
*
|
|
279
|
+
* @param key - Variable name (default: "slopit_session")
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```javascript
|
|
283
|
+
* // At end of experiment
|
|
284
|
+
* slopit.storeInVars("slopit_session");
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
storeInVars(key = "slopit_session") {
|
|
288
|
+
if (this.varsObject !== null) {
|
|
289
|
+
this.varsObject[key] = JSON.stringify(this.exportSession());
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Exports session as JSON string.
|
|
294
|
+
*
|
|
295
|
+
* @returns JSON string of complete session data
|
|
296
|
+
*
|
|
297
|
+
* @example
|
|
298
|
+
* ```javascript
|
|
299
|
+
* console.log(slopit.exportSessionJSON());
|
|
300
|
+
* ```
|
|
301
|
+
*/
|
|
302
|
+
exportSessionJSON() {
|
|
303
|
+
return JSON.stringify(this.exportSession());
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
export { SlopitRecorder as OSWebRecorder, SlopitRecorder };
|
|
308
|
+
//# sourceMappingURL=index.js.map
|
|
309
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/recorder.ts"],"names":[],"mappings":";;;AAoCO,IAAM,cAAA,GAAN,cAA6B,YAAA,CAAa;AAAA;AAAA;AAAA;AAAA,EAIvC,UAAA,GAA+B,IAAA;AAAA;AAAA;AAAA;AAAA,EAK/B,gBAAA,GAAwC,IAAA;AAAA;AAAA;AAAA;AAAA,EAKxC,QAAA,GAAW,KAAA;AAAA;AAAA;AAAA;AAAA,EAKX,aAAA,GAAoC,IAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAe5C,YAAY,MAAA,EAA8B;AAExC,IAAA,MAAM,OAAO,MAAA,EAAQ,UAAA;AACrB,IAAA,MAAM,aAAA,GAAgB,MAAA,EAAQ,aAAA,IAAiB,IAAA,EAAM,YAAY,QAAA,EAAS;AAG1E,IAAA,MAAM,cAAA,GACJ,kBAAkB,MAAA,GACd;AAAA,MACE,GAAG,MAAA;AAAA,MACH;AAAA,KACF,GACA,MAAA;AAEN,IAAA,KAAA,CAAM,cAAc,CAAA;AACpB,IAAA,IAAA,CAAK,aAAa,IAAA,IAAQ,IAAA;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOmB,eAAA,GAAgC;AACjD,IAAA,OAAO;AAAA,MACL,IAAA,EAAM,OAAA;AAAA,MACN,OAAA,EAAS,KAAK,kBAAA,EAAmB;AAAA,MACjC,cAAA,EAAgB;AAAA,KAClB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOQ,kBAAA,GAA6B;AACnC,IAAA,IAAI,OAAO,WAAW,WAAA,EAAa;AACjC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K,aAAA,EAAe,CAAA;AAAA,EAC5C;AACF","file":"index.js","sourcesContent":["/**\n * OSWeb session recorder.\n *\n * @module\n */\n\nimport {\n type AdapterData,\n BaseRecorder,\n type CompletedTrial,\n type TrialConfig,\n discoverElement,\n} from \"@slopit/adapter-shared\";\nimport type { PlatformInfo, ResponseInfo } from \"@slopit/core\";\n\nimport type { OSWebRecorderConfig, OSWebVars } from \"./types.js\";\n\n/**\n * SlopitRecorder for OSWeb experiments.\n *\n * Due to OSWeb's limited JavaScript sandbox, this recorder uses a\n * prepare/attach pattern that works within inline_javascript items.\n *\n * @example\n * ```javascript\n * // In prepare phase of inline_javascript\n * const slopit = new SlopitRecorder({ varsObject: vars });\n * slopit.prepareCapture();\n *\n * // In run phase\n * await slopit.attachWhenReady(\"textarea\");\n * // ... wait for response ...\n * slopit.endTrial(vars.response);\n * vars.slopit_data = JSON.stringify(slopit.getTrialData());\n * ```\n */\nexport class SlopitRecorder extends BaseRecorder {\n /**\n * Reference to OSWeb vars object for storing data.\n */\n private varsObject: OSWebVars | null = null;\n\n /**\n * Cleanup function for element discovery.\n */\n private discoveryCleanup: (() => void) | null = null;\n\n /**\n * Whether prepareCapture() has been called for the current trial.\n */\n private prepared = false;\n\n /**\n * Data from the most recently completed trial.\n */\n private lastTrialData: AdapterData | null = null;\n\n /**\n * Creates a new SlopitRecorder.\n *\n * @param config - Recorder configuration\n *\n * @example\n * ```javascript\n * const recorder = new SlopitRecorder({\n * varsObject: vars,\n * studyId: \"my-study\",\n * });\n * ```\n */\n constructor(config?: OSWebRecorderConfig) {\n // Extract participant ID from OSWeb vars if available\n const vars = config?.varsObject;\n const participantId = config?.participantId ?? vars?.subject_nr?.toString();\n\n // Only pass participantId if it has a value\n const enhancedConfig =\n participantId !== undefined\n ? {\n ...config,\n participantId,\n }\n : config;\n\n super(enhancedConfig);\n this.varsObject = vars ?? null;\n }\n\n /**\n * Gets platform information for OSWeb.\n *\n * @returns Platform information\n */\n protected override getPlatformInfo(): PlatformInfo {\n return {\n name: \"osweb\",\n version: this.detectOSWebVersion(),\n adapterVersion: \"0.1.0\",\n };\n }\n\n /**\n * Attempts to detect the OSWeb version.\n *\n * @returns Version string or \"unknown\" if not detectable\n */\n private detectOSWebVersion(): string {\n if (typeof window !== \"undefined\") {\n const oswebGlobal = (window as unknown as Record<string, unknown>)[\"osweb\"] as\n | { version?: string }\n | undefined;\n if (oswebGlobal?.version !== undefined) {\n return oswebGlobal.version;\n }\n }\n return \"unknown\";\n }\n\n /**\n * Prepares capture for the next trial.\n *\n * Call this in the prepare phase of an inline_javascript item.\n * This resets the trial state and prepares for element attachment.\n *\n * @example\n * ```javascript\n * // In prepare phase\n * slopit.prepareCapture();\n * ```\n */\n prepareCapture(): void {\n this.prepared = true;\n this.lastTrialData = null;\n }\n\n /**\n * Attaches to an element when it becomes available.\n *\n * Call this in the run phase. Uses MutationObserver to wait for\n * dynamically created elements.\n *\n * @param selector - CSS selector for target element\n * @param timeout - Max time to wait in milliseconds (default: 10000)\n * @returns Promise that resolves when attached\n * @throws Error if prepareCapture() was not called first\n * @throws Error if element not found within timeout\n *\n * @example\n * ```javascript\n * // In run phase\n * await slopit.attachWhenReady(\"textarea\");\n * ```\n */\n attachWhenReady(selector: string, timeout = 10000): Promise<void> {\n return new Promise((resolve, reject) => {\n if (!this.prepared) {\n reject(new Error(\"Call prepareCapture() before attachWhenReady()\"));\n return;\n }\n\n // Start the trial first\n const trialId = `trial_${this.trials.length}`;\n super.startTrial({ trialId });\n\n // Try immediate discovery\n const element = document.querySelector(selector) as HTMLElement | null;\n if (element !== null) {\n if (this.capture !== null) {\n this.capture.attach(element, this.currentTrial?.startTime ?? performance.now());\n } else {\n this.attachCapture(element);\n }\n resolve();\n return;\n }\n\n // Use MutationObserver for dynamic elements\n this.discoveryCleanup = discoverElement({\n selector,\n container: document.body,\n timeout,\n onFound: (el) => {\n if (this.capture !== null) {\n this.capture.attach(el, this.currentTrial?.startTime ?? performance.now());\n } else {\n this.attachCapture(el);\n }\n resolve();\n },\n onTimeout: () => {\n reject(new Error(`Element \"${selector}\" not found within ${timeout}ms`));\n },\n });\n });\n }\n\n /**\n * Starts a trial manually with a custom ID.\n *\n * Alternative to attachWhenReady() when you have direct access to\n * the target element.\n *\n * @param trialId - Unique identifier for the trial\n * @param element - Target DOM element for behavioral capture\n * @param trialConfig - Additional trial configuration\n *\n * @example\n * ```javascript\n * const textarea = document.querySelector(\"textarea\");\n * slopit.startTrialWithId(\"response-1\", textarea);\n * ```\n */\n startTrialWithId(\n trialId: string,\n element?: HTMLElement,\n trialConfig?: Partial<TrialConfig>,\n ): void {\n const config: TrialConfig = {\n trialId,\n ...trialConfig,\n };\n\n if (element !== undefined) {\n config.targetElement = element;\n }\n\n super.startTrial(config);\n }\n\n /**\n * Starts a trial manually.\n *\n * Convenience wrapper that uses startTrialWithId internally.\n *\n * @param trialId - Unique identifier for the trial\n * @param element - Target DOM element for behavioral capture\n * @param trialConfig - Additional trial configuration\n *\n * @example\n * ```javascript\n * const textarea = document.querySelector(\"textarea\");\n * slopit.startTrial(\"response-1\", textarea);\n * ```\n */\n override startTrial(\n trialId: string,\n element?: HTMLElement,\n trialConfig?: Partial<TrialConfig>,\n ): void;\n override startTrial(config: TrialConfig): void;\n override startTrial(\n trialIdOrConfig: string | TrialConfig,\n element?: HTMLElement,\n trialConfig?: Partial<TrialConfig>,\n ): void {\n if (typeof trialIdOrConfig === \"string\") {\n this.startTrialWithId(trialIdOrConfig, element, trialConfig);\n } else {\n super.startTrial(trialIdOrConfig);\n }\n }\n\n /**\n * Ends the current trial and stores data.\n *\n * @param response - The participant's response (text or ResponseInfo object)\n * @returns The completed trial data\n * @throws Error if no trial is in progress\n *\n * @example\n * ```javascript\n * // With text response\n * slopit.endTrial(vars.response);\n *\n * // With ResponseInfo object\n * slopit.endTrial({\n * type: \"text\",\n * value: vars.response,\n * characterCount: vars.response.length,\n * });\n * ```\n */\n override endTrial(response?: string | ResponseInfo): CompletedTrial {\n // Clean up discovery observer if active\n if (this.discoveryCleanup !== null) {\n this.discoveryCleanup();\n this.discoveryCleanup = null;\n }\n\n // Convert string response to ResponseInfo\n const responseInfo: ResponseInfo | undefined =\n typeof response === \"string\"\n ? {\n type: \"text\",\n value: response,\n characterCount: response.length,\n wordCount: response.trim().split(/\\s+/).filter(Boolean).length,\n }\n : response;\n\n const trial = super.endTrial(responseInfo);\n\n // Store last trial data for easy access\n this.lastTrialData = {\n behavioral: trial.behavioral ?? {\n keystrokes: [],\n focus: [],\n paste: [],\n mouse: [],\n scroll: [],\n clipboard: [],\n inputDuration: [],\n metrics: {\n keystroke: {\n totalKeystrokes: 0,\n printableKeystrokes: 0,\n deletions: 0,\n meanIKI: 0,\n stdIKI: 0,\n medianIKI: 0,\n pauseCount: 0,\n productProcessRatio: 0,\n },\n focus: {\n blurCount: 0,\n totalBlurDuration: 0,\n hiddenCount: 0,\n totalHiddenDuration: 0,\n },\n timing: {\n firstKeystrokeLatency: 0,\n totalResponseTime: 0,\n charactersPerMinute: 0,\n },\n },\n },\n flags: trial.captureFlags ?? [],\n };\n\n // Reset prepared state\n this.prepared = false;\n\n return trial;\n }\n\n /**\n * Gets the last trial's behavioral data.\n *\n * Use this to access data for storage in OSWeb vars.\n *\n * @returns AdapterData or null if no trial completed\n *\n * @example\n * ```javascript\n * const data = slopit.getTrialData();\n * if (data) {\n * vars.keystroke_count = data.behavioral.keystrokes.length;\n * }\n * ```\n */\n getTrialData(): AdapterData | null {\n return this.lastTrialData;\n }\n\n /**\n * Gets the last trial's data as JSON string.\n *\n * Convenience method for storing in OSWeb vars.\n *\n * @returns JSON string of trial data\n *\n * @example\n * ```javascript\n * vars.slopit_data = slopit.getTrialDataJSON();\n * ```\n */\n getTrialDataJSON(): string {\n return JSON.stringify(this.lastTrialData);\n }\n\n /**\n * Stores session data in OSWeb vars object.\n *\n * @param key - Variable name (default: \"slopit_session\")\n *\n * @example\n * ```javascript\n * // At end of experiment\n * slopit.storeInVars(\"slopit_session\");\n * ```\n */\n storeInVars(key = \"slopit_session\"): void {\n if (this.varsObject !== null) {\n this.varsObject[key] = JSON.stringify(this.exportSession());\n }\n }\n\n /**\n * Exports session as JSON string.\n *\n * @returns JSON string of complete session data\n *\n * @example\n * ```javascript\n * console.log(slopit.exportSessionJSON());\n * ```\n */\n exportSessionJSON(): string {\n return JSON.stringify(this.exportSession());\n }\n}\n\n// Also export as OSWebRecorder for clarity\nexport { SlopitRecorder as OSWebRecorder };\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@slopit/adapter-osweb",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "OSWeb adapter for slopit behavioral capture",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "Aaron Steven White",
|
|
9
|
+
"email": "aaron.white@rochester.edu",
|
|
10
|
+
"url": "https://aaronstevenwhite.io"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://slopit.github.io",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/slopit/slopit.git",
|
|
16
|
+
"directory": "packages/adapter-osweb"
|
|
17
|
+
},
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/slopit/slopit/issues"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"osweb",
|
|
26
|
+
"opensesame",
|
|
27
|
+
"behavioral-research",
|
|
28
|
+
"experiment",
|
|
29
|
+
"psychology",
|
|
30
|
+
"ai-detection",
|
|
31
|
+
"crowdsourcing"
|
|
32
|
+
],
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"import": "./dist/index.js",
|
|
36
|
+
"types": "./dist/index.d.ts"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist"
|
|
41
|
+
],
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@slopit/adapter-shared": "0.1.0",
|
|
44
|
+
"@slopit/core": "0.1.0",
|
|
45
|
+
"@slopit/behavioral": "0.1.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"jsdom": "^25.0.1",
|
|
49
|
+
"tsup": "^8.3.5",
|
|
50
|
+
"typescript": "^5.7.2",
|
|
51
|
+
"vitest": "^2.1.8"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsup",
|
|
55
|
+
"typecheck": "tsc --noEmit",
|
|
56
|
+
"test": "vitest run",
|
|
57
|
+
"test:watch": "vitest",
|
|
58
|
+
"clean": "rm -rf dist"
|
|
59
|
+
}
|
|
60
|
+
}
|