Giter VIP home page Giter VIP logo

inject-html's Introduction

inject-html

Node module for injecting HTML code before or after the body tag into the response.

Build Status

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.

license

MIT

inject-html's People

Contributors

alessioalex avatar

Stargazers

 avatar 5l1v3r1 avatar Michael Scott Hertzberg avatar Steve avatar Matt McFarland avatar Rick Blundell avatar  avatar

Watchers

 avatar James Cloos avatar Rick Blundell avatar  avatar  avatar

inject-html's Issues

Reduce amount of dependencies

Hi @alessioalex! Thank you for the incredible work with inject-html.

I have a little suggestion, could we reduce amount of dependencies? It feels like the feature could be implemented without any dependencies at all and without a significant source code lines increase. What do you think?

For example BundlePhobia says that mime-db module is in 65 times larger in size than inject-html but you are not actually using it.

Dependencies

Cannot get req.url

Could you please help me to figure out what can be the problem?

var mimeType = mime.lookup(req.url);
^
TypeError: Cannot read property 'url' of undefined

app.listen(port, function(res, req){
inject(req, res, function() {
res.setHeader('Content-Type', 'text/html');
if (req.url === '/') {
fs.createReadStream.pipe(res);
} else {
res.statusCode = 404;
res.end('Page Not Found');
}
});
});

Actually i would like to inject html to every page while server runs, so it wasn't directly in the file and when compiling to html with gulp or grunt there was no injected html.

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.