Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read console.log() output form javascript

Tags:

javascript

I am writing an overlay UI for one application using Greasemonkey, injecting JS to a page.

Now I have issue to hook on some ajax calls that runs on the webpage. However, the page is generating console logs, informing that content is fully loaded. My question is:

In plain javascript, is there a possibility of reading console output generated by completely different script?

function blackBox(){
   //this is generating come console output.
   //this runs on code layer I have no access to.
}

function readConsole(){
   //read console record by record
   if(console.msg == "something I want"){
      doSomething();
   }

}

Thanks in advance :)

like image 510
Zorak Avatar asked Nov 23 '25 12:11

Zorak


1 Answers

As others have stated, you can override the console.log() function with your own, and implement your own implementation:

var oldLog = unsafeWindow.console.log;
var messages = [];

unsafeWindow.console.log = function(msg) {
    messages.push(msg);
    oldLog.apply(null, arguments);
}

// Access the entire console.log history in messages
like image 178
ugh StackExchange Avatar answered Nov 25 '25 00:11

ugh StackExchange