PhantomJS - injectJs() 方法
injectJs 方法将指定文件中的外部脚本包含到页面中。如果当前目录中没有该文件,则使用 libraryPath 对文件进行额外搜索。如果文件被注入,则返回 true,否则返回 false。
语法
其语法如下 −
wpage.injectJs(filename);
示例
以下示例显示如何使用 injectionJs() 方法。
var wpage = require('webpage').create(); wpage.open('http://localhost/tasks/a.html', function(status) { if (wpage.injectJs('tscript1.js')) { var msg = wpage.evaluate(function() { return testcode(); }); console.log(msg); phantom.exit(); } });
tscript1.js
function testcode () { return "welcome to phantomjs"; }
上述程序生成以下输出。
welcome to phantomjs
phantomjs_webpage_module_methods.html