Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alessioalex/inject-html
Node module for injecting HTML code before or after the `body` tag into the response.
https://github.com/alessioalex/inject-html
html inject inject-script nodejs
Last synced: 22 days ago
JSON representation
Node module for injecting HTML code before or after the `body` tag into the response.
- Host: GitHub
- URL: https://github.com/alessioalex/inject-html
- Owner: alessioalex
- Created: 2015-06-04T15:28:33.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-08-01T15:14:13.000Z (over 7 years ago)
- Last Synced: 2024-10-03T10:41:33.240Z (about 1 month ago)
- Topics: html, inject, inject-script, nodejs
- Language: JavaScript
- Size: 10.7 KB
- Stars: 6
- Watchers: 5
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# inject-html
Node module for injecting HTML code before or after the `body` tag into the response.
[![Build Status](https://travis-ci.org/alessioalex/inject-html.svg)](https://travis-ci.org/alessioalex/inject-html)
## usage
Installable via npm: `npm i inject-html`.
```js
var inject = injectCode({
// type: 'append', // default 'prepend'
code: "alert('injected!!')" // HTML code
});http.createServer(function(req, res) {
inject(req, res, function() {
res.setHeader('Content-Type', 'text/html');if (req.url === '/') {
fs.createReadStream(__dirname + '/page.html').pipe(res);
} else {
res.statusCode = 404;
res.end('Page Not Found\n');
}
});
}).listen(1337);
```Sample output:
before injecting:
```html
inject html example
inject-html example
content
```
after injecting:
```html
inject html examplealert('injected!!')
inject-html example
content
```
## caveats
If the content doesn't have a `` tag, this module __will not function properly__.
This module will delete the `content-length` and `content-encoding` (if gzip/inflate) headers.## license
[MIT](http://alessioalex.mit-license.org/)