您将如何使用 javascript 解码 Node-Red 中 msg.payload 内的 HTML 实体?

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

作为实验的一部分,我正在使用 Node-Red。我正在创建一个简单的工作流程,只需单击一个按钮,即可将来自不同网站的内容嵌入(下载 html 内容)到它自己的页面上。

但是,提取的 HTML msg.payload 将自动编码为 HTML。因此,我不会定期 <div>,而是 &lt;div class=&quot;shot&quot;&gt;。这破坏了这个想法。

有了 'function-node',我可以将任何 Javascript 应用于 msg.payload,从而提取出 HTML。我需要找到一种方法将 HTML 实体解码回好的 ol' HTML.

默认情况下,

msg.payload 已经包含提取的内容,因此如果需要,您可以在 Javascript var 中使用它;

var x = msg.payload

所以 tl;dr:我如何仅使用 Javascript 解码 HTML 个实体?


如果您想 'fiddle' 使用我的工作流程,请复制并粘贴以下代码。

[{"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"]]}]

您的流程中的模板节点正在转义 HTML 实体。在 Template 节点使用的 mustache sytanx 中,要阻止它转义 HTML,您必须使用三个大括号:

{{{payload}}}