PhantomJS injectJs() 脚本注入方法


发布日期 : 2021-08-12 09:35:28 UTC

访问量: 10 次浏览

PhantomJS injectJs()

injectJs 方法将指定文件中的外部脚本包含在页面中。如果当前目录中没有该文件,它将使用libraryPath来进行进一步搜索文件。如果成功注入文件,则返回true,否则返回false。

语法

injectJs 的语法如下:

wpage.injectJs(filename);

示例

下面的示例展示了如何使用injectJs()方法。

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