2014-10-22 85 views
0

這是我的Nodejs Express應用程序的一個頁面,routers/index.js頁面 我想解析一些網站 - 這裏有兩段代碼:一個例子。 獲取頁面後,我使用jsdom模塊處理數據。但有一個問題 - othersite.com在這個例子中工作得很好,而site.com一個錯誤解析和渲染頁面(因爲它在我看來)後崩潰:nodejs jsdom TypeError:Object [object global] has no method'postMessage'

http://s7.addthis.com/static/r07/core159.js:4 
){!_ate.bro.ie6&&!_ate.bro.ie7&&W&&W.contentWindow&&W.contentWindow.postMessag 
                    ^
TypeError: Object [object global] has no method 'postMessage' 
    at Object.r [as msg] (http://s7.addthis.com/static/r07/core159.js:4:6291) 
    at Object.window._ate.m.initPostMessage (http://s7.addthis.com/static/r07/core159.js:7:11410) 
    at null._onTimeout (http://s7.addthis.com/static/r07/core159.js:7:7874) 
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15) 

還有其他的條件塊會產生相同的錯誤,儘管這裏沒有太多的代碼。
誰能告訴我有什麼問題?

router.get('/', function(req, res) { 
var url_parts = url.parse(req.url, true), 
    query = url_parts.query, 
    video_url, 
    file_url, 
    videoTitle, 
    embedCode, 
    remoteAddr = req.connection.remoteAddress, 
    base64_encode = require('base64').encode; 
if (query.link) { 
     var pathObj = url.parse(query.link, true); 
     var options = { 
      host: pathObj.host, 
      port: 80, 
      path: pathObj.path, 
      headers: { 
       "X-forwarder-for" : "90.90.90.90" // just sample ip 
      } 
     }; 
     http.get(options, function(response){ 

      var allChunk = ''; 
      response.on('data', function(chunk) { 
        allChunk += chunk; 
      }); 
      response.on('end', function(){ 
       var doc = jsdom.jsdom(allChunk); 
       var serializedChunk = doc.documentElement.outerHTML; 
       jsdom.env(
        serializedChunk, 
        ["http://code.jquery.com/jquery.js"], 
        function (errors, window) { 
         remoteAddr = remoteAddr; 
         videoTitle = window.$('title').text(); 
         var rawHtml = window.$('body').html(); 
         var handler = new htmlparser.DefaultHandler(function (error, dom) {}); 
         var parser = new htmlparser.Parser(handler); 
         parser.parseComplete(rawHtml); 
         var objectDom = handler.dom; 
         if (query.link.indexOf('site.com') != -1) { 
          var blockHTML = window.$('#player embed').attr('flashvars'); 
          var flash_vars_url = url.parse('/?' + blockHTML, true); 
          var queryFlashVars = flash_vars_url.query; 
          file_url = queryFlashVars.flv_url; 
          embedCode = window.$('input[name="media_embed_code"]').val(); 
          res.render('link', { title: 'source link', link: file_url, videoTitle: videoTitle, embedCode: embedCode, sourceLink: query.link }); 
         } else if (query.link.indexOf('othersite.com') != -1) { 
          file_url = window.$('.downloadList li a').attr('href'); 
          var embedObject = /\$\('#clip_text'\).val\(*"([^"]*)/g.exec(rawHtml); 
          embedCode = embedObject[1]; 
          res.render('link', { title: 'source link', link: file_url, videoTitle: videoTitle, embedCode: embedCode, sourceLink: query.link }); 

         } else { 
          res.status(404); 
          res.render('error', { 
           message: 'Not found', 
           error: {} 
          }); 
         } 

        } 
       ); 

      }); 
     }).on("error", function(e){ 
      console.log("Got error: " + e.message); 
     }).end(); 
} else { 
    res.render('index', { title: 'title' }); 
} 

});

我將不勝感激任何幫助。

回答

0

以及可以從錯誤描述看:

您想ate.bro.ie6 & &調用_ate.bro.ie7 & &W¯¯& & W.contentWindow & & W.contentWindow.postMessage

但是內容Window沒有這樣的方法。可能的原因是你加載一些東西並覆蓋該模塊。

在提供的源代碼(並從http://s7.addthis.com/static/r07/core159.js去混淆)我看到這是它在服務器端的一些庫似乎模擬「瀏覽器」窗口對象的行爲......但你銷燬了窗口上下文,所以它無法工作。

我覺得here類似的問題

0

它看起來對我來說,有試圖執行window.postMessage代碼,並且此方法不存在jsdom。此代碼測試是否window.postMessage可以在全稱爲:

var jsdom = require("jsdom"); 

var document = jsdom.env({ 
    html: "<html><head><script>window.postMessage();</script></head><body></body></html>", 
    features: { 
     FetchExternalResources: ["script"], 
     ProcessExternalResources: ["script"] 
    }, 
    done: function (errors, w) { 
     console.log(errors); 
    } 
}); 

產生這樣的輸出:

[ { type: 'error', 
    message: 'Running file:///tmp/t2/test.js:undefined:undefined<script> failed.', 
    data: 
    { error: [TypeError: Object [object global] has no method 'postMessage'], 
     filename: 'file:///tmp/t2/test.js:undefined:undefined<script>' } } ] 

我也檢查了代碼:

$ npm install jsdom 
$ cd node_modules/jsdom 
$ grep postMessage `find . -type f` 
[no results] 
相關問題