我目前正在为我的web扩展使用markdown-it来显示可翻译的教程和变更日志。但是Mozilla有安全检查,告诉我使用innerHTML是不安全的。所以我想知道是否有一个JavaScript (理想情况下是TypeScript)标记库可以创建dom元素而不是呈现HTML文本?
发布于 2017-12-20 07:35:24
我为这个问题找到了一个不使用eval的解决方法:
// This was the old code:
container.innerHTML = md.render(value);
// This is the new code:
const domParser = new DOMParser();
const doc = domParser.parseFromString(md.render(value), 'text/html');
removeAllChildren(container);
for(let i=0; i<doc.body.childNodes.length; i++)
container.appendChild(doc.body.childNodes[i]);https://stackoverflow.com/questions/47878225
复制相似问题