mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
parent
ad8290ebcb
commit
8eebb6ea2d
@ -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
|
||||
|
@ -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(); }
|
||||
|
Loading…
Reference in New Issue
Block a user