Skip to content

Instantly share code, notes, and snippets.

@matthewhudson
Created December 2, 2012 22:56
Show Gist options
  • Save matthewhudson/4191461 to your computer and use it in GitHub Desktop.
Save matthewhudson/4191461 to your computer and use it in GitHub Desktop.
DOM Selector WebPipe Block
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