node.jscasperjsspookyjs

How to inject script in SpookyJS?


I am trying to inject punycode script in my SpookyJS program. But it is not working.

try {
    var Spooky = require('spooky');
} catch (e) {
    var Spooky = require('../lib/spooky');
}

var spooky = new Spooky({
    child: {
        transport: 'http'
    },
    casper: {
        logLevel: 'debug',
        verbose: true,
        options: {
            clientScripts: ["punycode.js"]
        }
    }
}, function (err) {
    if (err) {
        e = new Error('Failed to initialize SpookyJS');
        e.details = err;
        throw e;
    }

    spooky.start("http://www.google.com");

    spooky.then(function(){
        this.evaluate(function() {
            console.log("testing");
            var x = punycode.encode("hi");
            console.log("x: "+x);
        });
    });

    spooky.run();
});

spooky.on('error', function (e, stack) {
    console.error(e);
    if (stack) {
        console.log(stack);
    }
});

spooky.on('console', function (line) {
    console.log(line);
});

spooky.on('remote.message', function(message) {
    console.log('[Inside Evaluate] ' + message);
});

I don't see x value in console output.

$ node test.js 
[info] [phantom] Starting...
[info] [phantom] Running suite: 3 steps
[debug] [phantom] opening url: http://www.google.com/, HTTP GET
[debug] [phantom] Navigation requested: url=http://www.google.com/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=http://www.google.co.kr/?gfe_rd=cr&ei=y1EKVPjzK6eL8Qfl4oDoBA, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://www.google.co.kr/?gfe_rd=cr&ei=y1EKVPjzK6eL8Qfl4oDoBA"
[debug] [phantom] Successfully injected Casper client-side utilities
[debug] [phantom] start page is loaded
[info] [phantom] Step anonymous 3/3 http://www.google.co.kr/?gfe_rd=cr&ei=y1EKVPjzK6eL8Qfl4oDoBA (HTTP 200)
[Inside Evaluate] testing
[info] [phantom] Step anonymous 3/3: done in 1293ms.
[info] [phantom] Done 3 steps in 1311ms

Any idea why it isn't working?


Solution

  • The clientScripts option should be part of casper hash.

    casper: {
        logLevel: 'debug',
        verbose: true,
        clientScripts: ["punycode.js"]
    }