Phantomjs-injectjs-method

提供:Dev Guides
移動先:案内検索

PhantomJS-injectJs()

injectJsメソッドには、指定されたファイルからページへの外部スクリプトが含まれます。 ファイルが現在のディレクトリで使用できない場合、libraryPathを使用してファイルをさらに検索します。 ファイルが挿入された場合はtrue、それ以外の場合はfalseを返します。

構文

その構文は次のとおりです-

wpage.injectJs(filename);

次の例は、injectJs()メソッドの使用方法を示しています。

var wpage = require('webpage').create();
wpage.open('http://localhost/tasks/al', 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";
}

上記のプログラムは、次の output を生成します。

welcome to phantomjs