Giter Club home page Giter Club logo

mhtml2html's Introduction

mhtml2html.js

npm version circleci

mhtml2html converts MHTML files to a single HTML file using javascript.

Usage | API | Development

Usage

mhtml2html is compatible with Node >= v10.

It has only been tested with MHTML files built and used with the latest versions of Chrome

Node.js Example

mhtml2html can be used via the command line (use the --help flag to view all options):

$ mhtml2html <input.mhtml> <output.html>

For programmatic usage, mhtml2html can be used provided a WHATWG DOM parser implementation (e.g jsdom):

const mhtml2html = require('mhtml2html');
const { JSDOM } = require('jsdom');

const mhtml = '<your MHTML string>';
const htmlDoc = mhtml2html.convert(mhtml, { parseDOM: (html) => new JSDOM(html) });
console.log(htmlDoc);

Browser Example

To get started, import mhtml2html:

import mhtml2html from 'mhtml2html';

Or include it as a script in your webpage:

<script src="https://unpkg.com/mhtml2html@<VERSION>" type="javascript" />

By default, mhtml2html will use the DOMParser available in most browsers:

import mhtml2html from 'mhtml2html';

const mhtml = '<your MHTML string>';
const html = mhtml2html.convert(mhtml);
console.log(html);

API

parse

mhtml2html.parse(mhtml, { htmlOnly = false, parseDOM = <function> });

  • mhtml: An MHTML String.
  • options.htmlOnly: If set to true, returns the html document without resources.
  • options.parseDOM: A callback that accepts a DOM string and returns a window object (defaults to DOMParser only available in browsers).
  • Returns an html document without resources if htmlOnly is set to true. Otherwise it returns an MHTML parsed object:
{
    "index" : "<html-index-url>",
    "media": {
        "<asset-url>" : {
            "data" : "<resource-string>",
            "id": "<frame-id>",
            "type": "<resource-type>",
            "encoding": "<resource-encoding>"
        }
    },
    "frames": {
        "<frame-id>": {
            "data": "<resource-string>",
            "id": "<frame-id>",
            "type:": "<resource-type>",
            "encoding": "<resource-encoding>"
        }
    }
}

convert

mhtml2html.convert(mhtml, { convertIframes = false, parseDOM = <function> });

  • mhtml: An MHTML String or MHTML parsed object.
  • options.convertIframes: Whether or not to include iframes in the converted response (defaults to false).
  • options.parseDOM: A callback that accepts a DOM string and returns a window object (defaults to DOMParser only available in browsers).
  • Returns an html window element.

Development

Requirements

  • Node.js >= 10
  • Yarn

To build and test mhtml2html:

  1. If node_modules haven't been installed already, run yarn install from the root directory.
  2. Run yarn test to build and test the source code.

License

Released under the MIT License

mhtml2html's People

Contributors

dependabot[bot] avatar msindwan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.