Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposing variables from PhantomJS call to injectJS

Tags:

phantomjs

I've followed examples for injecting jQuery from the getting started page and that works just fine. I have a local copy of jQuery in the same directory, and do something like...

if(page.injectJs('jquery.min.js')) {
  page.evaluate(function(){
    //Use jQuery or $ 
  }
}

When I try to inject my own script(s), none of the functions are available to me. Say I have a script called myScript.js that just has

function doSomething() {
  // doing something...
}

I cannot then use doSomething like...

if(page.injectJs('myScript.js')) {
  console.log('myScript injected... I think');
  page.evaluate(function() {
    doSomething();
  });
} else { 
  console.log('Failed to inject myScript'); 
}

I've tried

window.doSomething = function() {};

and

document.doSomething = function() {};

as well with no luck, as well as trying to call them with window.doSomething() or document.doSomething() in the subsequent page.evaluate().

like image 483
Felix Avatar asked Apr 14 '12 18:04

Felix


1 Answers

The following works for me, maybe some other part of your app logic is wrong:

inject.coffee

page = require('webpage').create()

page.onConsoleMessage = (msg) -> console.log msg

page.open "http://www.phantomjs.org", (status) ->
  if status is "success"
    page.includeJs "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js", ->
      if page.injectJs "do.js"
        page.evaluate ->
          title = echoAndReturnTitle('hello')
          console.log title
        phantom.exit()

do.coffee:

window.echoAndReturnTitle = (arg) ->
  console.log "echoing '#{arg}'"
  console.log $(".explanation").text()
  return document.title

Result:

> phantomjs inject.coffee
echoing 'hello'

            PhantomJS is a headless WebKit with JavaScript API.
            It has fast and native support for various web standards: 
            DOM handling, CSS selector, JSON, Canvas, and SVG.
            PhantomJS is created by Ariya Hidayat.

PhantomJS: Headless WebKit with JavaScript API

or if you prefer JavaScript (they're auto-generated and a little ugly):

`inject.js':

// Generated by CoffeeScript 1.3.1
(function() {
  var page;

  page = require('webpage').create();

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

  page.open("http://www.phantomjs.org", function(status) {
    if (status === "success") {
      return page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js", function() {
        if (page.injectJs("do.js")) {
          page.evaluate(function() {
            var title;
            title = echoAndReturnTitle('hello');
            return console.log(title);
          });
          return phantom.exit();
        }
      });
    }
  });

}).call(this);

do.js:

// Generated by CoffeeScript 1.3.1
(function() {

  window.echoAndReturnTitle = function(arg) {
    console.log("echoing '" + arg + "'");
    console.log($(".explanation").text());
    return document.title;
  };

}).call(this);
like image 144
Pooria Azimi Avatar answered Oct 24 '22 05:10

Pooria Azimi