2013-03-08 51 views
9

我正在使用試圖在服務器端爲我們的網站的搜索引擎優化使用nodejs和phantomjs。雖然Ajax工作正常,但我無法執行我在代碼中使用的自定義承諾。我如何讓幻影等待承諾解決。 以下是我編碼的內容。如何在phantomJS中執行jQuery承諾?

$('body').addClass('before-dom-ready'); 

$(function() { 
    $('body').addClass('after-dom-ready'); 

    var dfrd = $.Deferred(), 
      promise = dfrd.promise(); 

    setTimeout(function() { 
     dfrd.resolve(); 
    }, 5000); 

    promise.done(function() { 
     $('body').addClass('promise-executed'); 
    }); 

}); 

phantomJS增加了「前-DOM就緒」和階級「-DOM就緒後」,但我無法得到「的承諾,執行」類的身體。

+0

是否有可能看到您的完整phantomJS腳本(以及它依賴的任何其他文件的完整代碼)。即所以我們可以很容易地重現這個問題。 – 2013-05-28 00:24:42

+0

在什麼時間點以及如何檢查'promise-executed'類是否已添加?你做了什麼樣的調試,例如,你是否試過在'dfrd.resolve()'之後放置'console.log'調用?您是否在普通瀏覽器中試過這些代碼,換句話說,是什麼讓您認爲這是一個PhantomJS問題? – 2013-05-28 17:05:16

回答

4

PhantomJs不會自動等待所有待處理腳本的結束。在onload事件上調用WebPage#onLoadFinished。

至於大部分腳本,這裏的想法是等到「某件事」完成或者真實。 我強烈建議你測試waitfor.js。在PhantomJs中理解這個例子非常重要。

我想你的例子是一個例子,但讓我提出一個答案。

HTML頁面

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script> 
    <title>Test</title> 
</head> 
<body id="body"> 

    <script type="text/javascript"> 
     //alert('hello'); 
     $('body').addClass('before-dom-ready'); 

     $(function() { 
      $('body').addClass('after-dom-ready'); 

      var dfrd = $.Deferred(), 
        promise = dfrd.promise(); 

      setTimeout(function() { 
       dfrd.resolve(); 
      }, 5000); 

      promise.done(function() { 
       $('body').addClass('promise-executed'); 
       $('body').text('Hello World !'); 
      }); 

     }); 
    </script> 
</body> 
</html> 

PhantomJs腳本

var page = require('webpage').create(); 
var system = require('system'); 

function waitFor(testFx, onReady, timeOutMillis) { 
    var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 10000, //< Default Max Timout is 10s 
     start = new Date().getTime(), 
     condition = false, 
     interval = setInterval(function() { 
      if ((new Date().getTime() - start < maxtimeOutMillis) && !condition) { 
       // If not time-out yet and condition not yet fulfilled 
       condition = (typeof (testFx) === "string" ? eval(testFx) : testFx()); //< defensive code 
      } else { 
       if (!condition) { 
        // If condition still not fulfilled (timeout but condition is 'false') 
        //console.log("'waitFor()' timeout"); 
        typeof (onReady) === "string" ? eval(onReady) : onReady(); 
        clearInterval(interval); 
        //phantom.exit(1); 
       } else { 
        // Condition fulfilled (timeout and/or condition is 'true') 
        console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms."); 
        typeof (onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled 
        clearInterval(interval); //< Stop this interval 
       } 
      } 
     }, 500); //< repeat check every 500ms 
}; 

if (system.args.length != 1) { 
    console.log('invalid call'); 
    phantom.exit(1); 
} else { 
    //adapt url to your context 
    page.open('http://localhost:9231/demo.html', function (status) { 
     if (status !== 'success') { 
      console.log('Unable to load the address!'); 
      phantom.exit(); 
     } else { 
      waitFor(
       function() { 
        return page.evaluate(function() { 
         return $('body').hasClass('promise-executed'); 
        }) > 0; 
       }, 
       function() { 
        page.render('page.png'); 
        phantom.exit(); 
       }, 10000); 
     } 
    }); 
} 

基本上,waitFor將檢查每500毫秒,如果身體有一個名爲'promise-executed'類。

+0

感謝您的詳細回覆..我將不得不做更多的閱讀之前納入到我的測試,但這似乎是合理的和有據可查的.. – 2013-05-29 15:14:21

+0

是的,這對我很有用。希望這也能幫助你。 – Cybermaxs 2013-05-30 14:07:33