3

在这里跟进我的问题 -如何使用 Greasemonkey 脚本使用 XSLT 转换 XML 文件?- 我面临另一个问题:

我想在我的 XSL 模板中使用一些基本的 javascript 函数来控制某些 div 的显示。但是,无论我如何包含这些 javascript 函数,它们似乎都无法被识别。我已经调查了很多,但我似乎无法绕过它。

我尝试了两件事:

  • <script>在标签中添加 XSL 模板中的 javascript
  • <script>在 Greasemonkey 脚本本身中添加新标签

我宁愿不使用 jQuery 或外部 JS 文件(我也尝试过)以使其尽可能简单,但如果这样可以解决问题,我愿意改变整个事情!

无论哪种情况,当我调用该函数时,我都会得到一个ReferenceError: x is not defined. 我确实看到 javascript 代码很好地位于最终的 HTML 结果中。当我使用 Firebug 附加一个<script>带有简单功能的新标签时,该标签会向纯 html 页面发出“hello”警报,然后它就可以完美运行了。只有当这在 XSLT 转换之上完成时才会出错(为了简单起见,我只是使用一个简单的函数来显示一个警告框)。

这是我的示例数据:

  <?xml version="1.0" encoding="utf-8"?>
  <Results>
      <Result>
          <Listings total="2">
              <Res>
                  <Result index="0">
                      <id>123456</id>
                      <name>My Business</name>
                      <category>Restaurants</category>
                      <phone>9872365</phone>
                  </Result>
              </Res>
              <Res>
                  <Result index="1">
                      <id>876553</id>
                      <name>Some Other Business</name>
                      <category>Restaurants</category>
                      <phone>9834756</phone>
                  </Result>
              </Res>
          </Listings>
      </Result>
  </Results>

这是我刚刚在标签中添加<script>标签的第一次尝试<head>

// ==UserScript==
// @name        _Test XML Renderer
// @description stylesheet for xml results
// @include     *
// @grant       none
// ==/UserScript==

var xsl_str = '<?xml version="1.0" encoding="utf-8"?>\n\
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">\n\
    <xsl:output method="html"/>\n\
    <xsl:template match="/">\n\
        <html>\n\
            <head><script type="text/javascript">function hello() {alert("hello")};</script></head>\n\
            <body>\n\
                <table id="results" border="1" cellspacing="0" cellpadding="0">\n\
                    <thead>\n\
                        <tr>\n\
                            <th class="name">id</th>\n\
                            <th class="name">category ID</th>\n\
                            <th class="name">name</th>\n\
                            <th class="name">phone</th>\n\
                        </tr>\n\
                    </thead>\n\
                    <tbody>\n\
                        <xsl:for-each select="Results/Result/Listings/Res">\n\
                            <tr>\n\
                                <td class="small" width="120">\n\
                                    <a href="#" onclick="hello()"><xsl:value-of select="Result/id"/></a>\n\
                                </td>\n\
                                <td class="small" width="120">\n\
                                    <xsl:value-of select="Result/category"/>\n\
                                </td>\n\
                                <td class="small" width="120">\n\
                                    <xsl:value-of select="Result/name"/>\n\
                                </td>\n\
                                <td class="small" width="120">\n\
                                    <xsl:value-of select="Result/phone"/>\n\
                                </td>\n\
                            </tr>\n\
                        </xsl:for-each>\n\
                    </tbody>\n\
                </table>\n\
            </body>\n\
        </html>\n\
    </xsl:template>\n\
</xsl:stylesheet>\n\
';

var processor   = new XSLTProcessor ();
var dataXSL     = new DOMParser ().parseFromString (xsl_str, "text/xml");

processor.importStylesheet (dataXSL);

var newDoc      = processor.transformToDocument (document);

//-- These next lines swap the new, processed doc in for the old one...
window.content  = newDoc;

document.replaceChild (
    document.importNode (newDoc.documentElement, true),
    document.documentElement
);

这是我在 XSL 模板之外添加“hello”函数的另一个尝试:

// ==UserScript==
// @name        _Test XML Renderer
// @description stylesheet for xml results
// @include     *
// @grant       none
// ==/UserScript==

var xsl_str = '<?xml version="1.0" encoding="utf-8"?>\n\
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">\n\
    <xsl:output method="html"/>\n\
    <xsl:template match="/">\n\
        <html>\n\
            <head></head>\n\
            <body>\n\
                <table id="results" border="1" cellspacing="0" cellpadding="0">\n\
                    <thead>\n\
                        <tr>\n\
                            <th class="name">id</th>\n\
                            <th class="name">category ID</th>\n\
                            <th class="name">name</th>\n\
                            <th class="name">phone</th>\n\
                        </tr>\n\
                    </thead>\n\
                    <tbody>\n\
                        <xsl:for-each select="Results/Result/Listings/Res">\n\
                            <tr>\n\
                                <td class="small" width="120">\n\
                                    <a href="#" onclick="hello()"><xsl:value-of select="Result/id"/></a>\n\
                                </td>\n\
                                <td class="small" width="120">\n\
                                    <xsl:value-of select="Result/category"/>\n\
                                </td>\n\
                                <td class="small" width="120">\n\
                                    <xsl:value-of select="Result/name"/>\n\
                                </td>\n\
                                <td class="small" width="120">\n\
                                    <xsl:value-of select="Result/phone"/>\n\
                                </td>\n\
                            </tr>\n\
                        </xsl:for-each>\n\
                    </tbody>\n\
                </table>\n\
            </body>\n\
        </html>\n\
    </xsl:template>\n\
</xsl:stylesheet>\n\
';

var processor   = new XSLTProcessor ();
var dataXSL     = new DOMParser ().parseFromString (xsl_str, "text/xml");

processor.importStylesheet (dataXSL);

var newDoc      = processor.transformToDocument (document);

var script = "function hello() {alert('hello')};";
var newElem = newDoc.createElement('script');
newElem.type = 'text/javascript';
newElem.appendChild(newDoc.createTextNode(script));
newDoc.getElementsByTagName('head').item(0).appendChild(newElem);

//-- These next lines swap the new, processed doc in for the old one...
window.content  = newDoc;

document.replaceChild (
    document.importNode (newDoc.documentElement, true),
    document.documentElement
);
4

1 回答 1

3

不要使用onclick. 这对于用户脚本来说是三倍,因为存在额外的范围和/或沙箱冲突。

此外,尝试将 JS 添加到 XSLT 文件/文本中是一个糟糕的主意,在这种情况下也不需要脚本注入。

使用该脚本来执行您想到的任何 JS 操作。例如:

// ==UserScript==
// @name        _XML Renderer with javascript functionality
// @description Stylesheet and javascript for xml results
// @include     http://YOUR_SERVER.COM/YOUR_PATH/*.xml
// @resource    xslFile  Q_17998446_transform.xsl
// @grant       GM_getResourceText
// ==/UserScript==

var xsl_str     = GM_getResourceText ("xslFile");
var processor   = new XSLTProcessor ();
var dataXSL     = new DOMParser ().parseFromString (xsl_str, "text/xml");

processor.importStylesheet (dataXSL);

var newDoc      = processor.transformToDocument (document);

//-- These next lines swap the new, processed doc in for the old one...
window.content  = newDoc;

document.replaceChild (
    document.importNode (newDoc.documentElement, true),
    document.documentElement
);

//-- Use JS to smarten-up the new document.
var firstCols   = document.querySelectorAll ("#results td:first-child");

for (var J = firstCols.length - 1;  J >= 0;  --J) {
    var tdNode = firstCols[J];
    tdNode.style.cursor = "pointer";
    tdNode.addEventListener ("click", clickCellHandler, false);
}

function clickCellHandler (zEvent) {
    var cellContents = zEvent.target.textContent.trim ();

    alert ('The clicked cell contains "' + cellContents + '".');
}

保存在安装脚本的同一文件夹中的文件在哪里Q_17998446_transform.xsl(您可能需要卸载并重新安装脚本)。

Q_17998446_transform.xsl包含这个,确切地说:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
            <head></head>
            <body>
                <table id="results" border="1" cellspacing="0" cellpadding="0">
                    <thead>
                        <tr>
                            <th class="name">id</th>
                            <th class="name">category ID</th>
                            <th class="name">name</th>
                            <th class="name">phone</th>
                        </tr>
                    </thead>
                    <tbody>
                        <xsl:for-each select="Results/Result/Listings/Res">
                            <tr>
                                <td class="small" width="120">
                                    <xsl:value-of select="Result/id"/>
                                </td>
                                <td class="small" width="120">
                                    <xsl:value-of select="Result/category"/>
                                </td>
                                <td class="small" width="120">
                                    <xsl:value-of select="Result/name"/>
                                </td>
                                <td class="small" width="120">
                                    <xsl:value-of select="Result/phone"/>
                                </td>
                            </tr>
                        </xsl:for-each>
                    </tbody>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>


当您在适当的 XML 文件上运行该脚本时,它会向第一个表列(无标题)添加一个单击处理程序——“Web 2.0”方式。

当单击第一列单元格中的一个时,它会发出警报,例如:

单击的单元格包含“876553”。

于 2013-08-05T04:55:09.370 回答