Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 50cb269
Showing
7 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
lib-cov | ||
*.seed | ||
*.log | ||
*.csv | ||
*.dat | ||
*.out | ||
*.pid | ||
*.gz | ||
|
||
pids | ||
logs | ||
results | ||
|
||
.DS_Store | ||
|
||
npm-debug.log | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib/script-injector'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} |