PhantomJS - injectJs()



injectJs 方法从指定文件向页面注入外部脚本。如果文件不在当前目录中,则它将使用 libraryPath 进一步搜索该文件。如果文件已注入,则返回 true,否则返回 false。

语法

其语法如下 −

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 
phantomjs_webpage_module_methods.htm
Advertisement