Disable energy saving (?) in Safari when executing a script?

I have a script that scrolls down a JavaScript heavy web page with “infinite scroll” in Safari and extracts some data. When I execute this on the machine I am physically using it works fine but takes 1-2 hours to finish. I therefore want to execute it on a machine I am not using at the moment. I do that using something like

% ssh machine “osascript ~/bin/myscript.scpt”

but this times out. Not the regular AS timeout but timeouts I have included in the script.

There is no programmatic way to determine when I the script have reached the end of page so I a couple of short and long delays in the script and check if new data has loaded (if the page height is the same as before the delay) (before such a check I extract the data available, which in itself takes 10-15 minutes when the page is fully loaded).

Anyway, the problem is that Safari doesn’t execute at normal speed when the screen is locked. Quite the opposite, it executes at some minimal speed. A “jump to the end of the page and wait for more content to be loaded” (which is how my script goes all the way to the end of the page) manoeuvrer when the page is just loaded takes 2-3 seconds if done manually or running the script from the machine I physically using but takes minutes if I do it on a remote machine using ssh and osascript.

I assume this is because Safari thinks it is in the background (it isn’t, the first thing I do in the script is to activate Safari) and therefore down prioritize the tab I am using in my script.

Is there a way around this?

Tell Safari to disable energy saving features? Unlock the screen? Anything else?

I don’t know what happens with Safari in your case, but my suggestion is to try caffeinate. It’s a build-in command line utility that prevents sleeping while a command is running and can tell the OS that the user is active.
https://ss64.com/mac/caffeinate.html

I already use caffeinate, sleeping is not the problem, but (I think) that Safari down prioritizes background windows.