You understand that in order to execute JS code in PHP, you need a JS engine written in either PHP or embedded in it. Version of the JS engine written entirely in PHP I'm not considering at all, since it would be complete insanity, and from the point of view of effort and from the point of view of performance and end result. Accordingly, there is the option to use the solution, which was built in PHP... but, such a solution with 99% probability will not be available on shared hosting, but it can be installed on a local VPS or dedicated server.
One of the most popular JS-engine is a V8, carved out of Google Chrome, in particular, is its implementation for PHP
But I would probably try to start from something more simple (in implementation) of the options, in particular, would look the commands and/or plugins for the browser that would allow to keep the code of the entire page to HTML file, running at the same time from the command line, or tried to write a script for the browser (JS), which would be inserted into the end of the page (for example, using a simple proxy that you can write on the same PHP or fiddle with Nginx/Apache) would take current HTML code directly from your browser, after it changes and send it in finished form to a PHP script, which after parsing, gave the address of the next page in the same script, and so on ad infinitum (or until the desired page is not going to end).