Created
December 2, 2012 22:56
-
-
Save matthewhudson/4191461 to your computer and use it in GitHub Desktop.
DOM Selector WebPipe Block
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
var jsdom = require('jsdom'); | |
var fs = require('fs'); | |
var jquery = fs.readFileSync("./jquery.min.js").toString(); | |
var Block = require('node-webpipe').Block; | |
var block = new Block() | |
.name("DOM Selector") | |
.description("Outputs selected content from a HTML DOM.") | |
.input("url", "string", "The location of the document to inspect.") | |
.input("selector", "string", "A jQuery-compatible DOM selector.") | |
.input("attribute", "string", "A HTML element attribute.") | |
.output("content", "string", "The selected content from the document.") | |
block.handle(function(inputs, callback) { | |
getContent(inputs, function (content) { | |
callback(false, { | |
content: content | |
}); | |
}); | |
}); | |
block.listen(); | |
var getContent = function (inputs, callback) { | |
jsdom.env({ | |
html: inputs.url, | |
src: [jquery], | |
done: function (errors, window) { | |
var $ = window.$; | |
var $element = null; | |
if (errors) { | |
callback(""); | |
} | |
$element = $(inputs.selector) | |
if (inputs.attribute) { | |
callback($element.attr(inputs.attribute)); | |
} else { | |
callback($element.html()) | |
} | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment