Coder Social home page Coder Social logo

connect-inject's Introduction

connect-inject

connect middleware for adding any script to the response, this is a forked version of connect-livereload.

This is a slightly modified version of connect-livereload all the credits go to the author.

For further documentation refer to the author's repo connect-livereload.

install

npm install connect-inject --save-dev
git clone https://github.com/danielhq/connect-inject.git

use

this middleware can be used to inject any sort of content into the webpage e.g. [Livereload, Weinre etc]

  snippet: string | Array

snippet now accepts either string or an array.

connect/express example

  app.use(require('connect-inject')({
    snippet: "<script>alert('hello world');</script>"
  }));

options

Options are not mandatory: app.use(require('connect-inject')()); The Options have to be provided when the middleware is loaded:

e.g.:

  app.use(require('connect-inject')({
    snippet: "<script>alert('hello world');</script>",
    ignore: ['.js', '.svg']
  }));

These are the available options with the following defaults:

  // these files will be ignored
  ignore: ['.js', '.css', '.svg', '.ico', '.woff', '.png', '.jpg', '.jpeg'],

  // this function is used to determine if the content of `res.write` or `res.end` is html.
  html: function (str) {
    return /<[:_-\w\s\!\/\=\"\']+>/i.test(str);
  },

  // rules are provided to find the place where the snippet should be inserted.
  // the main problem is that on the server side it can be tricky to determine if a string will be valid html on the client.
  // the function `fn` of the first `match` is executed like this `body.replace(rule.match, rule.fn);`
  // the function `fn` has got the arguments `fn(w, s)` where `w` is the matches string and `s` is the snippet.
  rules: [{
    match: /<\/body>/,
    fn: prepend
  }, {
    match: /<\/html>/,
    fn: prepend
  }, {
    match: /<\!DOCTYPE.+>/,
    fn: append
  }],


  // snippet taks a string argument which can be anything you want, and will be appended (by default) before </body> tag
  snippet: "<script>alert('hello world');</script>"

grunt example

The following example is from an actual Gruntfile that uses grunt-contrib-connect

connect: {
  options: {
    port: 3000,
    hostname: 'localhost'
  },
  dev: {
    options: {
      middleware: function (connect) {
        return [
          require('connect-inject')({ snippet: "<script>alert('hello world');</script>"}),
          mountFolder(connect, '.tmp'),
          mountFolder(connect, 'app')
        ];
      }
    }
  }
}

For use as middleware in grunt simply add the following to the top of your array of middleware.

  require('connect-inject')(),

You can pass in options to this call if you do not want the defaults.

dev is simply the name of the server being used with the task grunt connect:dev. The other items in the middleware array are all functions that either are of the form function (req, res, next) like checkForDownload or return that like mountFolder(connect, 'something').

credits

This is a slightly modified version of connect-livereload all the credits go to the author.

license

MIT License

Bitdeli Badge

connect-inject's People

Contributors

agrberg avatar andineck avatar bitdeli-chef avatar danyeah avatar petehunt avatar sheenobu avatar sindresorhus avatar

Watchers

 avatar  avatar  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.