簡體   English   中英

如何在firefox插件sdk 1.10 main.js中使用nsIParserUtils?

[英]How to use nsIParserUtils inside firefox addon sdk 1.10 main.js?

我最近提交的Firefox附加站點(基於Firefox Add-on SDK 1.10)被拒絕,因為我沒有清理我使用的輸入,並建議使用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:document not defined”錯誤。 有人可以幫助我如何調用此功能?

該函數返回一個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>

有人可以對此有所了解嗎?

由於某種原因,外部樣式的鏈接被刪除:外部樣式無法驗證,並且它們可能很危險(特別是, -moz-binding可用於運行代碼)。 此外,假設您可以將HTML代碼放入以下相對鏈接不安全的位置(例如Thunderbird中的郵件消息)。 絕對鏈接總是很好。

您可能想要做的是預處理HTML代碼以消除這些問題 - 解析相對鏈接和內部對外部樣式的引用。 像這樣的東西:

// Parse the HTML code into a temporary document
var doc = Cc["@mozilla.org/xmlextras/domparser;1"]
               .createInstance(Ci.nsIDOMParser)
               .parseFromString(html, "text/html");

// Make sure all links are absolute
for (var i = 0; i < doc.links.length; i++)
    doc.links[i].setAttribute("href", doc.links[i].href);

// Make sure all stylesheets are inlined
var stylesheets = doc.getElementsByTagName("link");
for (i = 0; i < stylesheets.length; i++)
{
    try
    {
        var request = new XMLHttpRequest();
        request.open("GET", stylesheets[i].href, false);
        request.send(null);
        var style = doc.createElement("style");
        style.setAttribute("type", "text/css");
        style.textContent = request.responseText;
        stylesheets[i].parentNode.replaceChild(style, stylesheets[i]);
        i--;
    }
    catch (e)
    {
        // Ignore download errors
    }
}

// Serialize the document into a string again
html = Cc["@mozilla.org/xmlextras/xmlserializer;1"]
         .createInstance(Ci.nsIDOMSerializer)
         .serializeToString(doc.documentElement);

// Now sanizite the HTML code
var parser = Cc["@mozilla.org/parserutils;1"].getService(Ci.nsIParserUtils);
var sanitizedHTML = parser.sanitize(html, parser.SanitizerAllowStyle);

請注意,我使用同步XMLHttpRequest來下載樣式表內容 - 這是為了簡單起見,您的最終代碼應該使用不會掛起用戶界面的異步下載(很可能通過request模塊)。

並且其中的第一個參數被稱為文檔元素。 我不知道那是什么意思?

你不需要那個。 只需使用nsIParserUtils.sanitize方法,只需獲取字符串作為輸入並返回已清理版本的輸出:

var parser = Cc["@mozilla.org/parserutils;1"].getService(Ci.nsIParserUtils);
var sanitizedHTML = parser.sanitize(html, flags);

檢查“常量”部分上方的鏈接,以查看您的方案中需要具有哪些標志。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM