Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dlmanning committed May 1, 2013
0 parents commit 50cb269
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
@@ -0,0 +1,17 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

.DS_Store

npm-debug.log
node_modules
26 changes: 26 additions & 0 deletions README.md
@@ -0,0 +1,26 @@
#script-injector

1. provides a Transform stream that allows you to inject inline javascript into an html text stream.
2. Uses Stream.Transform, but shims in [`readable-stream`](https://github.com/isaacs/readable-stream) if you're on node 0.8
3. Should only be used for good, never for evil

## Installation

`npm install script-injector`


## How to use

Just pipe a stream of html through script-injector. You can pass in either some stringified or a function object. What could be easier?

```javascript
scriptInjector = require('script-injector);
\\ Then do something like this somewhere else
fs.createReadStream('anHTMLFile')
.pipe(scriptInjector(aFunction))
.pipe(someOtherPlace);
```
`script-injector` will insert the provided code *before* your first script tags, or just before `</body>` if you don't have any other scripts.
1 change: 1 addition & 0 deletions index.js
@@ -0,0 +1 @@
module.exports = require('./lib/script-injector');
62 changes: 62 additions & 0 deletions lib/script-injector.js
@@ -0,0 +1,62 @@
module.exports = ScriptInjectorStream;

var fs = require('fs')
, Transform = require('stream').Transform;

if(!Transform) { // shim for node 0.8
Transform = require('readable-stream/transform');
}

var hp = require('htmlparser2');

function ScriptInjectorStream(script, options) {
var self = this;
if(!(self instanceof ScriptInjectorStream)) {
return new ScriptInjectorStream(script, options);
}

Transform.call(self, options);

self._script = script ? script.toString()
: ';(' + function () { console.log("You didn't provide a script to inject") } + ')()';
self.needToAddScript = true;
self.htmlParser = new hp.Parser({
onprocessinginstruction: function (name, data) {
self.push('<' + data + '>');
},
onopentag: function (name, attribs) {
var output = '';
if(name === 'script' && self.needToAddScript) {
self.needToAddScript = false;
output += '<script type=\"text/javascript\">\n;(' + self._script + ')()\n<\/script>\n';
}
output += '<' + name;
for(var key in attribs) {
output += ' ' + key + '=\"' + attribs[key] + '\"';
}
output += '>';
self.push(output);
},
ontext: function (text) {
self.push(text);
},
onclosetag: function (name) {
if(name === 'body' && self.needToAddScript) {
self.needToAddScript = false;
self.push('<script type=\"text/javascript\">\n;(' + self._script + ')()\n<\/script>\n')
}
self.push('<\/' + name + '>');
}
});

self.on('end', function () { self.htmlParser.parseComplete() });

}

ScriptInjectorStream.prototype = Object.create(
Transform.prototype, { constructor: { value: ScriptInjectorStream }});

ScriptInjectorStream.prototype._transform = function(chunk, encoding, done) {
this.htmlParser.write(chunk);
done();
}
25 changes: 25 additions & 0 deletions package.json
@@ -0,0 +1,25 @@
{
"name": "script-injector",
"version": "0.0.1",
"description": "Inject inline javascript into an HTML stream.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": "",
"keywords": [
"html",
"stream",
"script",
"inject",
"injector",
"injection",
"parser"
],
"author": "David Manning",
"license": "MIT",
"dependencies": {
"readable-stream": "~1.0.2",
"htmlparser2": "~3.0.5"
}
}
8 changes: 8 additions & 0 deletions test/index.html
@@ -0,0 +1,8 @@
<html>
<head>
<title>Testing script-injector</title>
</head>
<body>
<p>The script is inserted at end of the body, because there are no other scripts on this page</p>
</body>
</html>
10 changes: 10 additions & 0 deletions test/test.js
@@ -0,0 +1,10 @@
var fs = require('fs')
, scriptInjector = require('../lib/script-injector');

fs.createReadStream('./index.html')
.pipe(scriptInjector(someCode))
.pipe(process.stdout);

function someCode () {
console.log("I'm some code to be inserted inline");
}

0 comments on commit 50cb269

Please sign in to comment.