let HTML parse node allow msg.select set select

to close #943
This commit is contained in:
Dave Conway-Jones 2016-07-20 20:08:40 +01:00
parent ad8290ebcb
commit 8eebb6ea2d
2 changed files with 5 additions and 2 deletions

View File

@ -45,6 +45,7 @@
<script type="text/x-red" data-help-name="html">
<p>Extracts elements from an html document held in <code>msg.payload</code> using a selector.</p>
<p>If left blank the selector may be set dynamically by passing in <code>msg.select</code> along with the <code>msg.payload</code>.
<p>The selector uses <a href=="https://github.com/cheeriojs/cheerio/blob/master/Readme.md" target="_new">Cheerio</a>
which uses the <a href="https://github.com/fb55/CSSselect#user-content-supported-selectors" target="_new">CSS selector</a> syntax.</p>
<p>The result can be either a single message with a payload containing an array of the matched elements, or multiple

View File

@ -20,16 +20,18 @@ module.exports = function(RED) {
function CheerioNode(n) {
RED.nodes.createNode(this,n);
this.tag = n.tag || "h1";
this.tag = n.tag;
this.ret = n.ret || "html";
this.as = n.as || "single";
var node = this;
this.on("input", function(msg) {
if (msg.hasOwnProperty("payload")) {
var tag = node.tag;
if (msg.hasOwnProperty("select")) { tag = node.tag || msg.select; }
try {
var $ = cheerio.load(msg.payload);
var pay = [];
$(node.tag).each(function() {
$(tag).each(function() {
if (node.as === "multi") {
var pay2 = null;
if (node.ret === "html") { pay2 = cheerio.load($(this).html().trim()).xml(); }