diff --git a/plugins/client-scripts-api.md b/plugins/client-scripts-api.md index 552c601..e5ce6d2 100644 --- a/plugins/client-scripts-api.md +++ b/plugins/client-scripts-api.md @@ -9,6 +9,11 @@ - [Setting up a development environment](./script-development.md) - [Setting Up Hooks](#setting-up-hooks) - [Jobs](#jobs) + - [context](#context) + - [channel](#channel) + - [state](#state) + - [render](#render) + - [utils](#utils) - [Event Objects](#event-objects) - [Filtering Events](#filtering-events) - [Event List](#event-list) @@ -72,11 +77,63 @@ A job is just a function that receives a single argument: window.setTaskHook('recording', ({ context, channel, state }) => {}); ``` -The argument consists of the following fields +The argument consists of the following fields: -- `context` is an immutable object that holds the current task and session configuration. +### context +An immutable object containing the complete configuration and state for the current task and session. It is composed of the following nested objects: + - `session`: Contains live data about the user's current session. + - `project`: The configuration object for the entire project, as defined during setup. + - `subjectGroup`: The configuration for the subject group the user belongs to. + - `task`: The configuration for the currently active task. + - `options`: An object containing custom options for the script. If a script is configured at both the subject group and task level, the options from the subject group will take precedence. -- `channel` is used to send and receive messages using an interface similar to a [broadcast channel](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API): + Here is an example of how you might use the `context` object: + + ```javascript + window.setTaskHook('preparation', ({ context }) => { + // Log the current session ID + console.log('Current session ID:', context.session.id); + + // Access the name of the current task + console.log('Current task name:', context.task.name); + + // Use a custom option defined for the script + if (context.options.enableHighlighting) { + // custom logic to highlight elements + } + }); + ``` + + The `session` object provides real-time information about the user's interaction state: + + ```javascript + { + id, + projectId, + groupId, + subjectId, + origin, + params, + randomization, + referer, + result, + stage, + taskIndex + } + ``` + + - **`id`**: A unique identifier for the current session. + - **`projectId`**, **`groupId`**, **`subjectId`**: Identifiers for the project, subject group, and the specific subject. + - **`origin`**: The origin URL where the session was initiated. + - **`params`**: An object containing the query parameters from the URL that started the session. This object can be modified during the session using `utils.setSessionParam()`. At the end of the session, these parameters are appended to the return URL. + - **`randomization`**: If task randomization is enabled, this array holds the randomized sequence of task indexes. + - **`referer`**: The referrer URL. + - **`result`**: The current result state of the session. + - **`stage`**: The current lifecycle stage of the task (e.g., `preparation`, `recording`, `completion`). + - **`taskIndex`**: The index of the current task being executed. + +### channel +Used to send and receive messages using an interface similar to a [broadcast channel](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API): ```javascript window.setTaskHook('recording', ({ channel }) => { @@ -89,7 +146,8 @@ The argument consists of the following fields For more info on the events received and ways to filter for them, see the [event objects section](#event-objects). -- `state` is used to share state between jobs. All jobs can return an object that will be merged with the current state and available for all subsequent jobs. Scripts are free to use the global namespace, but we recommend using the `state` object to avoid conflicts and keep things tidy. +### state +Used to share state between jobs. All jobs can return an object that will be merged with the current state and available for all subsequent jobs. Scripts are free to use the global namespace, but we recommend using the `state` object to avoid conflicts and keep things tidy. ```javascript window.setInitHook(({ state }) => ({ taskCount: 0 })); @@ -110,7 +168,8 @@ The argument consists of the following fields tasks completed: 2 ``` -- `render` is a utility function for rendering custom content. It's meant as a way to help hide and manage the rendered content after each stage +### render +A utility function for rendering custom content. It's meant as a way to help hide and manage the rendered content after each stage ```javascript window.setTaskHook( @@ -127,52 +186,85 @@ The argument consists of the following fields ); ``` -- `utils` commonly used utility functions. +### utils +An object containing commonly used utility functions to simplify script development. The `content window` referenced in some of the functions is the window where the content is displayed, which is typically the top window but can also be an iframe. - ```javascript - /* - - sleep - - waitFor, waitForElement, waitForElements - - createScript, createStyles, injectStyles - - showMessage - */ - window.setTaskHook('preparation', async ({ utils }) => { - // wait half a second - await utils.sleep(500); - - // create a script tag from a source url + - **`sleep(milliseconds)`** + Pauses the execution for a specified amount of time. + ```javascript + await utils.sleep(500); // waits for half a second + ``` + + - **`createScript(url)`** + Creates a `