console.log from injected JS not captured by onConsoleMessage

718 views
Skip to first unread message

Jordan Feldstein

unread,
Nov 25, 2012, 7:48:43 PM11/25/12
to phan...@googlegroups.com
Hi all,

At the top of my phantom script, I set:

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

And later on, inject JS with:

page.injectJs("./contentscript.js") 

and inside contentscript.js, I send some messages with (Trying to debug some other strange behavior): 

if(controller)
{
  console.log("Controller exists.")
}
console.log("hello")
console.log(document.body.innerHTML) 

None of these console.log's output anything. (Controller does exist, btw) 


Is there something special that would stop console.log from working in this situation? What am I missing? 


Thanks,
~Jordan Feldstein

James Greene

unread,
Nov 26, 2012, 10:38:55 AM11/26/12
to phan...@googlegroups.com
PhantomJS version? Downloaded binaries or compiled from source? Operating system?

Page URL, if you can share?  If not, does this page happen to have frames and/or iframes on it?

~~James

James Greene

unread,
Nov 26, 2012, 11:02:05 AM11/26/12
to phan...@googlegroups.com
FYI, it worked just fine for me!

Test Script:

(function(window) {
var scriptToInjectFilename = './writeConsoleMessages.js';
var fs = require('fs');
var createScriptToInject = function() {
var scriptContents = [
'console.log("[console.log] Hello from injected script on page: " + window.location.href);',
'window.console.log("[window.console.log] Hello from injected script on page: " + window.location.href);'
].join('\n');
fs.write(scriptToInjectFilename, scriptContents, 'w');
};
var deleteScriptToInject = function() {
fs.remove(scriptToInjectFilename);
};
var page = require('webpage').create();
page.onConsoleMessage = function(msg) {
console.log("CONSOLE MSG: " + msg);
};
page.open('http://www.google.com/', function(status) {
createScriptToInject();
var injected = page.injectJs(scriptToInjectFilename);
console.log('Injected? ' + injected);
deleteScriptToInject();
phantom.exit(status === 'fail' ? 1 : 0);
});
})(this);


Output:
CONSOLE MSG: [console.log] Hello from injected script on page: http://www.google.com/
CONSOLE MSG: [window.console.log] Hello from injected script on page: http://www.google.com/
Injected? true


Specs:
  • PhantomJS 1.7.0
  • Downloaded binary
  • Windows 7 (64-bit)

~~James

James Greene

unread,
Nov 26, 2012, 11:03:03 AM11/26/12
to phan...@googlegroups.com
You may also want to verify that your script injection didn't fail, using the same technique that I used in my test script.
~~James

match

unread,
Nov 26, 2012, 12:22:10 PM11/26/12
to phan...@googlegroups.com
Page url is sometimes important. Sometimes sites will set:  window.console.log = function() {};
Which means you can't use it for sending messages from page.  At this point try the experimental window.callPhantom() which you can listen for from phantom's outer space with phantom.onCallback

match

unread,
Nov 26, 2012, 12:23:56 PM11/26/12
to phan...@googlegroups.com
phantom.onCallback should read page.onCallback

Jordan Feldstein

unread,
Nov 27, 2012, 12:50:06 PM11/27/12
to phan...@googlegroups.com
I was working with 8tracks.com, which is one of those sites that cancels out console.log 

Going to work around it with onCallback

Thanks!




~ J



phantom.onCallback should read page.onCallback
--
You received this message because you are subscribed to the Google Groups "phantomjs" group.
Visit this group at http://groups.google.com/group/phantomjs?hl=en.
 
 

Reply all
Reply to author
Forward
0 new messages