Reputation: 709
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 :)
Upvotes: 3
Views: 10944
Reputation: 471
Directly there's no way to access console outputs, but what you can do is that you can override console.log in a way where it checks for you condition first and then outputs the content, below is a sample code for that
console.stdlog = console.log.bind(console);
console.log = function(msg){
if(msg == "something I want"){
...
}
console.stdlog.apply(console, arguments);
}
Although you'll need to be very careful with this since if you add any console.log in that condition, it will create an infinite loop.
Upvotes: 1
Reputation: 362
Even if it were possible, that would be bad practice. The console is for human eyes to see, when debugging. It usually takes several milliseconds to execute console.log
, so executing it in production code is not a good idea.
I would say programming in booleans (like ifDidX
and ifDidY
) would be better. But if you really had to do this message-history thing, a better alternative would be to store messages in some other array. Here is an example:
var messagesLog = [];
//Since console.log takes several milliseconds to execute, you should tick this to false in production code.
const logToConsole = true;
function logMessage( message ){
//adds message to JS log.
messagesLog.push(message);
//performs console.log if not production code
if(logToConsole){
console.log(message);
}
}
logMessage("pizza");
logMessage("second to last message");
logMessage("last message");
//checks last message and this will be true
if( messagesLog[messagesLog.length - 1] == "last message" ){
logMessage("The last message in the log was 'last message'");
}
//checks last message and this will be false
if( messagesLog[messagesLog.length - 1] == "pizza" ){
logMessage("This will not occur because the last message was 'last message'");
}
//searches through log to find certain message
for(let i = 0; i < messagesLog.length; i++){
if(messagesLog[i] == "pizza"){
logMessage("one of the messages in the log was pizza");
}
}
Upvotes: 2
Reputation: 1618
Redefine console.log and storage messages on a array:
var consoleStorage = [];
console.log = function(msg){
consoleStorage.push(msg);
console.warn(msg); // if you need to print the output
}
Now you can read all logs:
function readConsole(){
//read console record by record
consoleStorage.forEach(msg => {
doSomething(msg);
});
}
Upvotes: 1
Reputation: 22921
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
Upvotes: 2