1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

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"> <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>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> <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> 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 <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) { function CheerioNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.tag = n.tag || "h1"; this.tag = n.tag;
this.ret = n.ret || "html"; this.ret = n.ret || "html";
this.as = n.as || "single"; this.as = n.as || "single";
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg.hasOwnProperty("payload")) { if (msg.hasOwnProperty("payload")) {
var tag = node.tag;
if (msg.hasOwnProperty("select")) { tag = node.tag || msg.select; }
try { try {
var $ = cheerio.load(msg.payload); var $ = cheerio.load(msg.payload);
var pay = []; var pay = [];
$(node.tag).each(function() { $(tag).each(function() {
if (node.as === "multi") { if (node.as === "multi") {
var pay2 = null; var pay2 = null;
if (node.ret === "html") { pay2 = cheerio.load($(this).html().trim()).xml(); } if (node.ret === "html") { pay2 = cheerio.load($(this).html().trim()).xml(); }