An open API service indexing awesome lists of open source software.

https://github.com/shinnn/insert-html-content

Insert contents into an HTML of a response body
https://github.com/shinnn/insert-html-content

html html5 insertion javascript modification nodejs non-blocking response stream

Last synced: 2 months ago
JSON representation

Insert contents into an HTML of a response body

Awesome Lists containing this project

README

        

# insert-html-content

[![npm version](https://img.shields.io/npm/v/insert-html-content.svg)](https://www.npmjs.com/package/insert-html-content)
[![GitHub Actions](https://action-badges.now.sh/shinnn/insert-html-content)](https://wdp9fww0r9.execute-api.us-west-2.amazonaws.com/production/results/shinnn/insert-html-content)
[![codecov](https://codecov.io/gh/shinnn/insert-html-content/branch/master/graph/badge.svg)](https://codecov.io/gh/shinnn/insert-html-content)

Insert contents into an HTML of a response body

```javascript
const {createServer} = require('http');
const fetch = require('node-fetch');
const insertHtmlContent = require('insert-html-content');

createServer((req, res) => {
insertHtmlContent(res, 'Hello ');

res.setHeader('content-type', 'text/html');
res.end('World');
}).listen(3000, async () => {
await (await fetch('http://localhost:3000')).text(); //=> 'Hello, World'
});
```

## Installation

[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).

```
npm install insert-html-content
```

## API

```javascript
const insertHtmlContent = require('insert-html-content');
```

### insertHtmlContent(*response*, *content* [, *options*])

*response*: [`http.ServerResponse`](https://nodejs.org/api/http.html#http_class_http_serverresponse)
*content*: `string`
*options*: `Object`

If the media type of the response is `text/html`, it inserts a given content into the response body as the first child of `` tag once, with increasing the value of `content-length` header if necessary.

```javascript
const {createServer} = require('http');
const fetch = require('node-fetch');
const injectBody = require('insert-html-content');

const html = Buffer.from('

Hi

');
const inserted = '

🏄‍

';

createServer((req, res) => {
insertHtmlContent(res, inserted);

res.setHeader('content-type', 'text/html');
res.setHeader('content-length', 37/* html.length */);
res.end(html);
}).listen(3000, async () => {
const response = await fetch('http://localhost:3000');

Number(response.headers.get('content-length'));
//=> 53, html.length + Buffer.byteLength(inserted)

await response.text(); //=> '

🏄‍

Hi

'
});
```

If the media type is not `text/html`, or the response body has no `` tag, it does nothing.

### options.tagName

Type: `string`
Default: `'body'`

Change the insertion target to the given tag.

```javascript
createServer((req, res) => {
insertHtmlContent(res, '', {
tagName: 'head'
});

res.setHeader('content-type', 'text/html');
res.end('');
}).listen(3000, async () => {
await (await fetch('http://localhost:3000')).text(); //=> ''
});
```

### options.insertToEnd

Type: `boolean`
Default: `false`

When this option is `true`, it inserts a content to the last child of the target tag instead.

Default:

```html

existing content
inserted content

```

`insertToEnd: true`:

```html

inserted content
existing content

```

### class insertHtmlContent.InsertHtmlContent(*contents* [, *options*])

*content*: `string`
*options*: `Object`
Return: `Function`

Create a new `insertHtmlContent` function with the fixed `content` and `options`. Use this class if a server will insert the same contents into every HTML response many times.

```javascript
const {InsertHtmlContent} = require('insert-html-content');

const injectStyle = new InsertHtmlContent('body {color: red}');
```

## License

[ISC License](./LICENSE) © 2018 - 2019 Watanabe Shinnosuke