inject-html
v1.1.2
Published
Node module for injecting HTML code before or after the `body` tag into the response.
Downloads
413
Readme
inject-html
Node module for injecting HTML code before or after the body
tag into the response.
usage
Installable via npm: npm i inject-html
.
var inject = injectCode({
// type: 'append', // default 'prepend'
code: "<script>alert('injected!!')</script>" // 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:
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>inject html example</title>
</head>
<body>
<h3>inject-html example</h3>
<p>content</p>
</body>
</html>
after injecting:
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>inject html example</title>
</head>
<body><script>alert('injected!!')</script>
<h3>inject-html example</h3>
<p>content</p>
</body>
</html>
caveats
If the content doesn't have a <body>
tag, this module will not function properly.
This module will delete the content-length
and content-encoding
(if gzip/inflate) headers.