Skip to content

Commit 50cb269

Browse files
committed
Initial commit
0 parents  commit 50cb269

File tree

7 files changed

+149
-0
lines changed

7 files changed

+149
-0
lines changed

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
lib-cov
2+
*.seed
3+
*.log
4+
*.csv
5+
*.dat
6+
*.out
7+
*.pid
8+
*.gz
9+
10+
pids
11+
logs
12+
results
13+
14+
.DS_Store
15+
16+
npm-debug.log
17+
node_modules

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#script-injector
2+
3+
1. provides a Transform stream that allows you to inject inline javascript into an html text stream.
4+
2. Uses Stream.Transform, but shims in [`readable-stream`](https://github.com/isaacs/readable-stream) if you're on node 0.8
5+
3. Should only be used for good, never for evil
6+
7+
## Installation
8+
9+
`npm install script-injector`
10+
11+
12+
## How to use
13+
14+
Just pipe a stream of html through script-injector. You can pass in either some stringified or a function object. What could be easier?
15+
16+
```javascript
17+
scriptInjector = require('script-injector);
18+
19+
\\ Then do something like this somewhere else
20+
21+
fs.createReadStream('anHTMLFile')
22+
.pipe(scriptInjector(aFunction))
23+
.pipe(someOtherPlace);
24+
```
25+
26+
`script-injector` will insert the provided code *before* your first script tags, or just before `</body>` if you don't have any other scripts.

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib/script-injector');

lib/script-injector.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module.exports = ScriptInjectorStream;
2+
3+
var fs = require('fs')
4+
, Transform = require('stream').Transform;
5+
6+
if(!Transform) { // shim for node 0.8
7+
Transform = require('readable-stream/transform');
8+
}
9+
10+
var hp = require('htmlparser2');
11+
12+
function ScriptInjectorStream(script, options) {
13+
var self = this;
14+
if(!(self instanceof ScriptInjectorStream)) {
15+
return new ScriptInjectorStream(script, options);
16+
}
17+
18+
Transform.call(self, options);
19+
20+
self._script = script ? script.toString()
21+
: ';(' + function () { console.log("You didn't provide a script to inject") } + ')()';
22+
self.needToAddScript = true;
23+
self.htmlParser = new hp.Parser({
24+
onprocessinginstruction: function (name, data) {
25+
self.push('<' + data + '>');
26+
},
27+
onopentag: function (name, attribs) {
28+
var output = '';
29+
if(name === 'script' && self.needToAddScript) {
30+
self.needToAddScript = false;
31+
output += '<script type=\"text/javascript\">\n;(' + self._script + ')()\n<\/script>\n';
32+
}
33+
output += '<' + name;
34+
for(var key in attribs) {
35+
output += ' ' + key + '=\"' + attribs[key] + '\"';
36+
}
37+
output += '>';
38+
self.push(output);
39+
},
40+
ontext: function (text) {
41+
self.push(text);
42+
},
43+
onclosetag: function (name) {
44+
if(name === 'body' && self.needToAddScript) {
45+
self.needToAddScript = false;
46+
self.push('<script type=\"text/javascript\">\n;(' + self._script + ')()\n<\/script>\n')
47+
}
48+
self.push('<\/' + name + '>');
49+
}
50+
});
51+
52+
self.on('end', function () { self.htmlParser.parseComplete() });
53+
54+
}
55+
56+
ScriptInjectorStream.prototype = Object.create(
57+
Transform.prototype, { constructor: { value: ScriptInjectorStream }});
58+
59+
ScriptInjectorStream.prototype._transform = function(chunk, encoding, done) {
60+
this.htmlParser.write(chunk);
61+
done();
62+
}

package.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "script-injector",
3+
"version": "0.0.1",
4+
"description": "Inject inline javascript into an HTML stream.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": "",
10+
"keywords": [
11+
"html",
12+
"stream",
13+
"script",
14+
"inject",
15+
"injector",
16+
"injection",
17+
"parser"
18+
],
19+
"author": "David Manning",
20+
"license": "MIT",
21+
"dependencies": {
22+
"readable-stream": "~1.0.2",
23+
"htmlparser2": "~3.0.5"
24+
}
25+
}

test/index.html

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<title>Testing script-injector</title>
4+
</head>
5+
<body>
6+
<p>The script is inserted at end of the body, because there are no other scripts on this page</p>
7+
</body>
8+
</html>

test/test.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var fs = require('fs')
2+
, scriptInjector = require('../lib/script-injector');
3+
4+
fs.createReadStream('./index.html')
5+
.pipe(scriptInjector(someCode))
6+
.pipe(process.stdout);
7+
8+
function someCode () {
9+
console.log("I'm some code to be inserted inline");
10+
}

0 commit comments

Comments
 (0)