如何在SpookyJS中注入脚本



我正试图在SpookyJS程序中注入punycode脚本。但它不起作用。

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);
});

我在控制台输出中看不到x值。

$ 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

知道为什么它不起作用吗?

clientScripts选项应该是casper哈希的一部分。

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

最新更新