2017-09-05 67 views
0

在許多許多鏈接,他們sugest加載jquery爲了做在頁面dom刮。後跟的常見模式是:Phantom.js:本地使用,npm安裝,jquery

var page = require('webpage').create(); 
var url = 'http://example.com'; 

page.onConsoleMessage = function(msg) { 
    console.log(msg); 
}; 

page.open(url, function (status) { 
    if(status==='success') { 
    page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() { 
     //Do stuff there 
     phantom.exit(); 
    }); 
    } else { 
    phantom.exit(1); 
    } 
}); 

但有時這將是迄今爲止網絡wize健全有通過NPM安裝在本地的jQuery:

npm install jquery

但是我怎麼包括本地安裝jQuery?

回答

1

請參閱page.injectJs方法:http://phantomjs.org/api/webpage/method/inject-js.html

page.open('http://www.phantomjs.org', function(status) { 
    if (status === "success") 
    { 
     if (page.injectJs('jquery.js')) { 
     var title = page.evaluate(function() { 
      // returnTitle is a function loaded from our do.js file - see below 
      return returnTitle(); 
     }); 
     console.log(title); 
     phantom.exit(); 
     } 
    } 
}); 

您還可以使用page.libraryPath屬性來設置的地方找到一個腳本。