Synthetic Agent: Add injectScript command
21st September 2023 (UTC)
The injectScript
command queues a JS snippet that's executed shortly after the next page in the script starts to navigate.
Using this synthetic script as an example:
// Script assumes URL specified is https://www.speedcurve.com
// disable logging
logData 0
// navigate to the intial URL
navigate %URL%
// Enable logging
logData 1
// Queue two scripts one blurs p elements and the other h1 elements
injectscript (function () { style = document.createElement('style'); style.innerHTML = "h1 {filter: blur(5px) !important}"; document.head.appendChild(style); })();
injectscript (function () { style = document.createElement('style'); style.innerHTML = "p {filter: blur(5px) !important}"; document.head.appendChild(style); })();
// navigate to the final URL where the p and h1 elements will be blurred
navigate %ORIGIN%/features/performance-monitoring/
The above example blurs the h1
and p
elements on the page but other use cases include setting localStorage
entries to bypass CMPs etc.
When there is more than one 'injectScript' command the scripts are executed in the order they're listed.
Each script is executed only once, but the command can be repeated before later navigation steps too.
If the script contains variables, consider wrapping it in an IIFE (Immediately Invoked Function Expression) to prevent these variables polluting the global namespace i.e. (function () { insert js code here })();
Currently this is a Chrome only feature. The scripts execute when the agent receives the frameNavigated
event which occurs early in the page lifecycle.