【发布时间】:2012-10-06 07:15:36
【问题描述】:
我最近提交的 Firefox 插件网站(基于 Firefox 插件 SDK 1.10)被拒绝了,因为我没有对我使用的输入进行清理,并被推荐给use nsIParserUtils。
我在该页面中找到了函数parseHTML(doc, html, allowStyle, baseURI, isXML)。我把它改成:
function parseHTML(doc, html, allowStyle, baseURI, isXML) {
var parser = Cc["@mozilla.org/parserutils;1"].getService(Ci.nsIParserUtils);
var f = parser.parseFragment(html, allowStyle ? parser.SanitizerAllowStyle : 0,
!!isXML, baseURI, doc);
return f;
}
其中的第一个参数被称为文档元素。我不知道那应该是什么?我尝试了document.createDocumentFragment(),但出现“ReferenceError:文档未定义”错误。有人可以帮助我如何调用此函数吗?
该函数返回一个nsIDOMDocumentFragment。如何将其转换回字符串?
更新:
按照@zer0 的建议,我使用了:
var parser = Cc["@mozilla.org/parserutils;1"].getService(Ci.nsIParserUtils);
var sanitizedHTML = parser.sanitize(html, flags);
但这违背了我想做的事情的目的。例如:
<html><head><BASE href='http://localhost/t/h.html' />
<link rel="stylesheet" type="text/css" href="h.css">
<style type="text/css">
.b{
color:green;
}
</style>
<base href="http://foo.example.com/">
</head><body>Sample Text. No Style
<script>Hello malicious code</script>
<p class="a">External Style</p>
<p class="b">Internal Style</p>
<p style="color:blue">Inline Style</p>
<a href="sample.html">Link</a><br><br><div style='color: #666666; font-size: 12px'>Clipped on 6-October-2012, 07:37:39 PM from <a href='http://localhost/t/h.html'>http://localhost/t/h.html</a> </div></body></html>
转换为:
<html><head>
<style type="text/css">
.b{
color:green;
}
</style>
</head><body>Sample Text. No Style
<p class="a">External Style</p>
<p class="b">Internal Style</p>
<p style="color:blue">Inline Style</p>
<a>Link</a><br><br><div style="color: #666666; font-size: 12px">Clipped on 6-October-2012, 07:37:39 PM from <a href="http://localhost/t/h.html">http://localhost/t/h.html</a> </div></body></html>
由于这剥离了外部超链接和 CSS,它违背了插件本身的目的。我想要的只是删除脚本:
<html><head><BASE href='http://localhost/t/h.html' /> <BASE href='http://localhost/t/h.html' />
<link rel="stylesheet" type="text/css" href="h.css">
<style type="text/css">
.b{
color:green;
}
</style>
<base href="http://foo.example.com/">
</head><body>Sample Text. No Style
<p class="a">External Style</p>
<p class="b">Internal Style</p>
<p style="color:blue">Inline Style</p>
<a href="sample.html">Link</a><br><br><div style='color: #666666; font-size: 12px'>Clipped on 6-October-2012, 07:37:39 PM from <a href='http://localhost/t/h.html'>http://localhost/t/h.html</a> </div></body></html>
有人能解释一下吗?
【问题讨论】:
标签: javascript dom firefox-addon firefox-addon-sdk