我正在編寫一個Chrome擴展,它應該將XSLT轉換應用於某些XML文檔。只是爲了測試我使用下面的XML和XSL文件:Chrome擴展中的XSLT:不安全地嘗試加載URL
XML:
<?xml version="1.0" encoding="utf-8" ?>
<WebServiceMessage>
<status>timeout</status>
<message>Nameserver%2520not%2520registered.</message>
<stepName>Finish</stepName>
<stepNumber>11</stepNumber>
<maxStepNumber>11</maxStepNumber>
<percent>100</percent>
<session>2fc0f139b88a800151e5f21b9d747919</session>
</WebServiceMessage>
XSL:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html><head></head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="*">
<xsl:for-each select="*">
<p><b><xsl:value-of select ="name(.)"/></b>:
<span><xsl:attribute name="id"><xsl:value-of select ="name(.)"/></xsl:attribute><xsl:value-of select="."/></span></p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
如果它是一個測試XML文件中鏈接的tranfromation正常工作本身,即通過:
<?xml-stylesheet type="text/xsl" href="message.xsl"?>
該擴展應該將相同的xsl鏈接注入到XML文件中。
的manifest.json:
{
"permissions": ["tabs", "<all_urls>"],
"content_scripts":
[
{
"matches": ["<all_urls>"],
"js" : ["contentscript.js"]
}
],
"web_accessible_resources":
[
"message.xsl"
],
"manifest_version": 2
}
contentscript.js:
(function()
{
if(document.xmlVersion != null)
{
var e = document.createProcessingInstruction(
"xml-stylesheet",
"type='text/xsl' href='" + chrome.extension.getURL("message.xsl") + "'");
document.insertBefore(e, document.firstChild);
}
})();
鉻輸出下列錯誤入控制檯的問題:
不安全嘗試從URL中加載URL chrome-extension://ladfinoepkgipbeooknnklpakoknohjh/message.xsl
URL http://localhost/out.xml
。域,協議和端口必須匹配。
如何解決這個問題?我在互聯網上看到了一些與類似錯誤有關的報告,這似乎是Chrome中的一個漏洞。
我也將xsl文件放在了web服務器上,並且更改了樣式錶鏈接到web服務器。仍然是相同的錯誤:
不安全的嘗試從URL加載URL http://localhost/message.xsl
URL http://localhost/out.xml
。域,協議和端口必須匹配。
顯然域名,協議和端口確實匹配。
我想這是一個[chrome的安全特性](http://blog.chromium.org/2008/12/security-in-depth-local-web-pages.html)。 – Sudarshan
我用這段代碼沒有任何問題,也許只有當你更新'window.document'時,它纔會這樣做:var documentString = document.firstChild.innerHTML; var docu = new DOMParser()。parseFromString(''+ documentString +' ',application/xml「); var e = docu.createProcessingInstruction(」xml-stylesheet「,」type ='text/xsl'href ='「 + chrome.extension.getURL(「message.xsl」)+「'」); docu.insertBefore(e,docu.firstChild);' –
jjperezaguinaga
@jjperezaguinaga您已經創建了一個新文檔而不是現有文檔。這與我在下面發佈的解決方法相同 – Stan