How would you decode HTML entities inside the msg.payload in Node-Red with javascript?

1.8k views Asked by At

As part of an experiment, I'm working with Node-Red. I'm creating a simple workflow that, on a click of a button, embeds (downloads html content) from a diversity of websites onto it's own page.

However, the HTML that's extracted, the msg.payload, will automatically be encoded to HTML. Thus, instead of regularly <div>, I'd get &lt;div class=&quot;shot&quot;&gt;. And that wrecks the idea.

With a 'function-node' I could apply any Javascript to the msg.payload, thus the extracted HTML. I need to find a way that decoded the HTML entities back to good ol' HTML.

msg.payload already contains, by default, the extracted content, so you could use it in a Javascript var, if needed;

var x = msg.payload

So tl;dr: How do I decode HTML entities with just Javascript?


If you want to 'fiddle' with my workflow, copy and paste the below code.

[{"id":"b05c9e5a.4fa36","type":"http request","name":"Get to Dribbble","method":"GET","ret":"obj","url":"https://dribbble.com/search?utf8=%E2%9C%93&q=flat+design&s=latest","x":181,"y":169,"z":"9b321f64.64cde","wires":[["93dfb5e5.6c2048"]]},{"id":"c3be5c2.f3c41a","type":"inject","name":"","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":156,"y":106,"z":"9b321f64.64cde","wires":[["b05c9e5a.4fa36"]]},{"id":"93dfb5e5.6c2048","type":"html","name":"Get each Dribbble","tag":".dribbble","ret":"html","as":"multi","x":188,"y":257,"z":"9b321f64.64cde","wires":[["ef6b650e.109498"]]},{"id":"cea8d694.315728","type":"file","name":"","filename":"dribbble/dribbbleFile.html","appendNewline":true,"overwriteFile":"true","x":895,"y":302,"z":"9b321f64.64cde","wires":[]},{"id":"6a37543c.95c8ac","type":"template","name":"","field":"payload","format":"handlebars","template":"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n\t<meta charset=\"UTF-8\">\n\t    <title>Document</title>\n    <link href=\"http://iscs.nl/dribbble.css\"></link>\n</head>\n<body>\n\nThis is the payload: {{payload}} !\n\t\n</body>\n</html>\n\n\n\n\n\n","x":676,"y":303,"z":"9b321f64.64cde","wires":[["cea8d694.315728"]]},{"id":"ef6b650e.109498","type":"function","name":"","func":"return msg;","outputs":1,"noerr":0,"x":492,"y":303,"z":"9b321f64.64cde","wires":[["6a37543c.95c8ac"]]}]
1

There are 1 answers

1
knolleary On BEST ANSWER

It is the Template node in your flow that is escaping the HTML entities. In the mustache sytanx used by the Template node, to stop it escaping HTML you must use triple braces:

{{{payload}}}