Skip to content

Commit

Permalink
Fixes #99: logging from phantomas scope is broken
Browse files Browse the repository at this point in the history
  • Loading branch information
macbre committed Oct 3, 2013
1 parent 8360a5f commit c5efc9e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
49 changes: 31 additions & 18 deletions core/phantomas.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,27 +538,40 @@ phantomas.prototype = {
this.emit('prompt', msg);
},

onConsoleMessage: function(args) {
// console.log arguments are passed as JSON-encoded array
var data = JSON.parse(args),
msg = data[0];

// parse JSON-encoded messages from browser's scope sendMsg()
if (msg.indexOf('msg:{"') === 0) {
msg = decodeURI(msg.substring(4)); // strip the prefix
try {
this.onCallback(JSON.parse(msg));
}
catch(ex) {
this.log('Unable to parse JSON message from browser: ' + msg + '!');
}
return;
onConsoleMessage: function(msg) {
var prefix, data;

// split "foo:content"
prefix = msg.substr(0,3);
data = msg.substr(4);

try {
data = JSON.parse(data);
}
catch(ex) {
// fallback to plain log
prefix = false;
}

msg = this.util.format.apply(this, data);
//console.log(JSON.stringify([prefix, data]));

switch(prefix) {
// handle JSON-encoded messages from browser's scope sendMsg()
case 'msg':
this.onCallback(data);
break;

// console.log arguments are passed as JSON-encoded array
case 'log':
msg = this.util.format.apply(this, data);

this.log('console.log: ' + msg);
this.emit('consoleLog', msg, data);
this.log('console.log: ' + msg);
this.emit('consoleLog', msg, data);
break;

default:
this.log(msg);
}
},

// https://github.com/ariya/phantomjs/wiki/API-Reference-WebPage#oncallback
Expand Down
4 changes: 2 additions & 2 deletions core/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
console.log = function() {
// pass all arguments as an array, let phantomas format them
// @see https://developer.mozilla.org/en-US/docs/Web/API/console
origConsoleLog.call(console, JSON.stringify(Array.prototype.slice.call(arguments)));
origConsoleLog.call(console, 'log:' + stringify(Array.prototype.slice.call(arguments)));
};

function sendMsg(type, data) {
Expand All @@ -120,7 +120,7 @@
}
**/

console.log('msg:' + stringify({type: type || false, data: data || false}));
origConsoleLog.call(console, 'msg:' + stringify({type: type || false, data: data || false}));
}

function log(msg) {
Expand Down

0 comments on commit c5efc9e

Please sign in to comment.