Node.js - 强大的HTML解析+访问HTML中的javascript函数

时间:2012-04-23 21:55:20

标签: javascript parsing node.js jsdom

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

contrived.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);
});

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

非常感谢。

0 个答案:

没有答案