Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding all the external resources of an HTML page into a single file using javascript in the browser

As you all know, external resources, like images, can be embedded into the html file using base64 encoding:

<img src="data:image/png;base64,iVBORw0KGgoAAAANS..." />

I'm looking for a pure browser-based javascript way to traverse an html page and embed all the external resources into the file so when I say $("html").html(), it returns all the page's contents. Even including its external resources.

Just so it makes sense, I'm trying to download web pages into single files using a headless browser on my server.

like image 433
Mehran Avatar asked Oct 27 '14 19:10

Mehran


People also ask

How do you embed HTML into JavaScript?

Using the innerHTML attribute: To append using the innerHTML attribute, first select the element (div) where you want to append the code. Then, add the code enclosed as strings using the += operator on innerHTML.

What is embed in JavaScript?

JavaScript is embedded into HTML and XHTML documents using the <script> element. This element can be used to embed the JavaScript directly into the web page (also known as inline), or to specify an external file that contains the JavaScript.

Is it possible to display the entire contents of another web page in your HTML page how?

An iframe or inline frame is used to display external objects including other web pages within a web page.


1 Answers

There are tools out there to do that. Examples:

  • https://github.com/remy/inliner
  • https://github.com/jgallen23/grunt-inline-css
  • https://github.com/ceee/grunt-datauri

While there are benefits to this approach, remember that a page visited more than once, or site with multiple pages with same JS/CSS files will enjoy client (browser) side caching.

like image 170
JAR.JAR.beans Avatar answered Oct 15 '22 00:10

JAR.JAR.beans