Skip to content

Instantly share code, notes, and snippets.

@fzzzy
Created December 13, 2011 22:21
Show Gist options
  • Save fzzzy/1474178 to your computer and use it in GitHub Desktop.
Save fzzzy/1474178 to your computer and use it in GitHub Desktop.
index 4a142fa..ad34145 100644
--- a/node/bin/domrepl.js
+++ b/node/bin/domrepl.js
@@ -3,10 +3,11 @@
var vm = require('vm')
, repl = require('repl')
, fs = require('fs')
+ , url = require('url')
+ , http = require('http')
var domjs = require('../')
-
function stdout(output){
// workaround for allowing the REPL to return text
if (stdout.silent) {
@@ -53,17 +54,13 @@ function writeHTML(html){
// create a window
var window = domjs.bootContext({
+ console: console,
stdout: function(output){ stdout(output) },
stderr: function(output){ stdout(output.toString()) }
});
// start the repl
-var domREPL = repl.start('dom.js> ');
-
-// set its context
-domREPL.context = window;
-
-
+var domREPL;
function zeropad(number, count){
@@ -79,4 +76,42 @@ function date(){
zeropad(d.getMinutes()), '.',
zeropad(d.getSeconds()),
d.getHours() > 12 ? 'pm':'am' ].join('');
-}
\ No newline at end of file
+}
+
+// parse command line arguments
+
+process.argv.forEach(function (val, index, array) {
+ if (index < 2) return;
+
+ var req = http.request(url.parse(val), function handleurl(res) {
+ console.log(val);
+ domREPL = repl.start('dom.js> ');
+ // set its context
+ domREPL.context = window;
+ var setup = ('var parser = window.document.implementation.mozHTMLParser
+ 'var doc = parser.document();' +
+ 'doc._setMutationHandler(function(msg) { stdout([,JSON.stringify(ms
+
+ //stdout.silent = true;
+ vm.runInContext(setup, window);
+ //stdout.silent = false;
+
+ res.setEncoding('utf8');
+ res.on('data', function ondata(chunk) {
+ //stdout.silent = true;
+ vm.runInContext('parser.parse(' + JSON.stringify(chunk.toString())
+ //stdout.silent = false;
+ });
+ res.on('end', function onend(chunk) {
+ //stdout.silent = true;
+ vm.runInContext('parser.parse("", true);', window);
+ //stdout.silent = false;
+ });
+ });
+
+ req.on('error', function(e) {
+ console.log('problem with request: ' + e.message);
+ });
+
+ req.end();
+});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment