npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

connect-inject

v0.4.0

Published

connect middleware for adding any script to the response

Downloads

65,229

Readme

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').

multiple injections

You can also do multiple injections by defining a snippet inside of a rule and setting the runAll option to true

{
    runAll: true,
    rules: [
        {
            match: /<head>/ig,
            snippet: '<script src="/top_file.js"></script>',
            fn: function(w, s) {
                return w + s;
            }
        },
        {
            match: /<script .* src=".*\.test\.js"><\/script>/ig,
            snippet: [
              '<script src="/bottomFile1.js"></script>',
              '<script src="/bottomFile2.js"></script>',
              '<script src="/src-test/testUtils.js"></script>'
            ]
            fn: function(w, s) {
                return s + w;
            }
        }
    ]
}

credits

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

license

MIT License

Bitdeli Badge