1

我是节点新手,希望从以下示例页面中提取 javascript 信息:

做作的.html:

<html>
    <head>
        <title>
            This is a contrived example          
        </title>
        <script type="text/javascript">
    var filenames = new Array()
    filenames[filenames.length] = "http://domainwhatever.s3.amazonaws.com/780BONNYVILLECOLDLAKECHRYSLER/4431716.jpg";
    filenames[filenames.length] = "http://domainwhatever.s3.amazonaws.com/780BONNYVILLECOLDLAKECHRYSLER/4431716_1.jpg";
    filenames[filenames.length] = "http://domainwhatever.s3.amazonaws.com/780BONNYVILLECOLDLAKECHRYSLER/4431716_2.jpg";
    filenames[filenames.length] = "http://domainwhatever.s3.amazonaws.com/780BONNYVILLECOLDLAKECHRYSLER/4431716_3.jpg";
    filenames[filenames.length] = "http://domainwhatever.s3.amazonaws.com/780BONNYVILLECOLDLAKECHRYSLER/4431716_4.jpg";

      function pixplosion_Content()
      {
        var eElement = document.getElementById('idLoading');
        if( eElement ) eElement.style.display = 'none';

        return "<pixplosion test=\"test\" flashGasket=\"http://www.realelivepeople.com/pixplosion/assets/flashGasket.swf?contentPath=\" ytBridge=\"/images/image.php?pixplosion=ytbridge\"><tab test=\"test\" label=\"Photos (%1)\" icon=\"Image\" autoIterate=\"false\"   ><tab test=\"test\" label=\"Vehicle Photos (%1)\" icon=\"\" autoIterate=\"true\" startFocused=\"true\"  >
    <image>http://s3.domainwhatever_2.com/vehicles/photos/3726/1300025.jpg</image>
    <image>http://s3.domainwhatever_2.com/vehicles/photos/3726/1300025/35102537.jpg</image>
    <image>http://s3.domainwhatever_2.com/vehicles/photos/3726/1300025/35102538.jpg</image>
    <image>http://s3.domainwhatever_2.com/vehicles/photos/3726/1300025/35102539.jpg</image>
    <image>http://s3.domainwhatever_2.com/vehicles/photos/3726/1300025/35102540.jpg</image>
    <image>http://s3.domainwhatever_2.com/vehicles/photos/3726/1300025/35102541.jpg</image>
    <image>http://s3.domainwhatever_2.com/vehicles/photos/3726/1300025/35102542.jpg</image>
    <image>http://s3.domainwhatever_2.com/vehicles/photos/3726/1300025/35102543.jpg</image><image>http://s3.domainwhatever_2.com/vehicles/photos/3726/1300025/35102544.jpg</image><image>http://s3.domainwhatever_2.com/vehicles/photos/3726/1300025/35102545.jpg</image><image>http://s3.domainwhatever_2.com/vehicles/photos/3726/1300025/35102546.jpg</image></tab></tab></pixplosion>";
      }

        </script>

    </head>
    <body>
    </body>
</html>

Jsdom 使用它的默认解析器阻塞了这个 HTML,所以我使用了来自 github 的 aredridel/html5 解析器。它似乎适用于通过 jQuery 读取 HTML,但我无法访问函数定义,就像我使用 jsdom 及其默认解析器所做的那样。

例如,以下内容:

console.log(window.filenames);

使用默认解析器给我一个数组。

使用 HTML5 解析器,它给了我:

undefined

这是我的代码:

var jsdom = require("jsdom"),
    fs = require('fs'),
    HTML5 = require('html5');

fs.readFile('contrived.html', 'utf-8', function(err, data) {
  if (err) {
    throw err;
  }
  var document = jsdom.jsdom(data, null, {parser: HTML5});

  // HTML data should be in document creation call
  var script = document.createElement("script");
  // HTML data SHOULD NOT be in window creation call
  var window = document.createWindow();
  var parser = new HTML5.Parser({document: window.document});
  parser.parse(data);

  script.src = 'http://code.jquery.com/jquery-1.4.2.js';
  script.onload = function(window) {
                      console.log('this is a test');
                      console.log(window.filenames);
                      console.log(window.pixplosion_Content);
  }
  document.head.appendChild(script);
});

我错过了什么,还是这个功能不可用?

非常感谢。

4

0 回答 0