Skip to content

Commit 67c178d

Browse files
author
Matthew Hudson
committed
Initial.
0 parents  commit 67c178d

4 files changed

Lines changed: 58 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
node_modules

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# DOM Selector Action Block
2+
3+
## Example Usage
4+
5+
curl -v -X OPTIONS http://block-dom-selector.herokuapp.com
6+
7+
curl -i -X POST -d '{"inputs":[{"attribute":"href","selector":".entry .title a","url":"http://www.reddit.com/r/malefashionadvice/comments/142js9/first_time_wearing_a_sweaterare_these_jeans_too/"}]}' -H "Content-Type: application/json" http://block-dom-selector.herokuapp.com

index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var fs = require('fs');
2+
var jquery = fs.readFileSync('./vendor/jquery.min.js').toString();
3+
var jsdom = require('jsdom');
4+
var Block = require('node-webpipe').Block;
5+
6+
var block = new Block()
7+
.name('DOM Selector')
8+
.description('Outputs selected content from a HTML DOM.')
9+
.input('url', 'string', 'The location of the document to inspect.')
10+
.input('selector', 'string', 'A jQuery-compatible DOM selector.')
11+
.input('attribute', 'string', 'A HTML element attribute.')
12+
.output('content', 'string', 'The selected content from the document.')
13+
14+
block.handle(function(inputs, callback) {
15+
getContent(inputs, function (content) {
16+
callback(false, {
17+
content: content
18+
});
19+
});
20+
});
21+
22+
block.listen();
23+
24+
var getContent = function (inputs, callback) {
25+
jsdom.env({
26+
html: inputs.url,
27+
src: [jquery],
28+
done: function (errors, window) {
29+
var $ = window.$;
30+
var $element = null;
31+
32+
if (errors) {
33+
callback('');
34+
}
35+
36+
$element = $(inputs.selector)
37+
38+
if (inputs.attribute) {
39+
callback($element.attr(inputs.attribute));
40+
} else {
41+
callback($element.html())
42+
}
43+
}
44+
});
45+
};

vendor/jquery.min.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)