Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture or save a current state (HTML elements) of a dynamic page from a SPA

It's possible to retrieve current state from a SPA (made on frameworks like Angular, React, etc)?

By current sate I meant a snapshot/save/export of all current HTML elements, as well styles/images and the data being shown at the moment? So can be [statically] consumed afterwards with a browser and file://?

Example:

<html>
    <head />
    <body>
        <h1>Welcome, <span ng-model="userController.username">John Doe</span></h1>
        <!-- etc -->
    </body>
</html>

Being the John Doe the current data shown by that controller.

As I tried, Save As... on browsers does not work. Save the HTML with CSS but full of tags {{variableName}}. And I assume that depending on the SPA was developed not even saves the desired page, instead saves the master/root/main page of the SPA.

There are other tools HTTrack Website Copier but from the usage I had on past this works best for static pages, I think.

Any suggestion of tools, browser extensions or even techniques that allow me to develop the tool or extension to achieve this?

like image 904
Paulo Oliveira Avatar asked Sep 11 '19 10:09

Paulo Oliveira


1 Answers

The "save as" functionality saves the source code (will save a blank page for a SPA).

I think your best option is to use the following command copy(document.documentElement.innerHTML) and then paste the code into an empty HTML file.

The problem is that all of the external resources (loaded via src attribute) won't be embedded so you have to do the job yourself with a script before using the copy command. Use const documentClone = document.documentElement.cloneNode(true); and then:

  • Parse all of the <style> tags and build a single inline <style> tag
  • Remove <script> tags?
  • For other tags <img>, <video>, etc... fetch them and replace them with inline Data URLs
like image 94
Guerric P Avatar answered Sep 21 '22 02:09

Guerric P