sweetbean.protection.protection
Base class for experiment-level protections.
A Protection is a self-contained bundle of JS that the experiment
compilation embeds at well-defined points. The contract is intentionally
small so new protections (CAPTCHA, attention checks, fingerprinting, ...)
can be added without touching Experiment or stimulus internals.
Hook points, in the order they fire in the generated experiment:
- :meth:
head_assets— extra HTML for<head>(only used byto_html). - :meth:
runtime_js— JS embedded BEFOREinitJsPsych. Use to inline a library (e.g.BotDetectionIIFE) so it is in scope by the time the experiment starts. - :meth:
init_js— JS run AFTERinitJsPsychexists but before trials start. Typical use: callLib.initialize()and register global handlers. - :meth:
on_load_for_stimulus/ :meth:on_finish_for_stimulus— per-trial JS injected into each stimulus'son_load/on_finishcallbacks.datais in scope insideon_finish(it's the trial row). - :meth:
summary_js— JS expression returning a summary object. The experiment wraps it injsPsych.data.addProperties({<name>: <expr>})so the result lands on every saved row.
Subclasses override only the hooks they need. The default implementations
return empty strings (no-op), so a minimal Protection adds nothing.
Protection
Base class — see module docstring for the hook contract.
Source code in sweetbean/protection/protection.py
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 | |
head_assets()
Extra HTML injected into <head>. Used by Experiment.to_html.
Source code in sweetbean/protection/protection.py
36 37 38 | |
init_js()
JS source run after initJsPsych but before trials run. Typical
use: call the library's initialize and register global handlers.
Source code in sweetbean/protection/protection.py
45 46 47 48 | |
on_finish_for_stimulus(stimulus)
Per-trial JS injected into the stimulus's on_finish callback.
data is in scope and refers to the trial's data row — mutate it
directly to attach per-trial signals.
Source code in sweetbean/protection/protection.py
64 65 66 67 68 69 70 | |
on_load_for_stimulus(stimulus)
Per-trial JS injected into the stimulus's on_load callback.
Return "" to no-op for this stimulus type. Implementations may
inspect stimulus (e.g. isinstance) to specialize.
Source code in sweetbean/protection/protection.py
56 57 58 59 60 61 62 | |
runtime_js()
JS source emitted before initJsPsych. Typically the protection
library (IIFE) being inlined into the page.
Source code in sweetbean/protection/protection.py
40 41 42 43 | |
summary_js()
JS expression that evaluates to a summary object. Empty disables.
Wired into the experiment's on_finish so it lands on every row of
saved data via jsPsych.data.addProperties.
Source code in sweetbean/protection/protection.py
50 51 52 53 54 | |