简体   繁体   English

阅读console.log()输出形式的javascript

[英]Read console.log() output form javascript

I am writing an overlay UI for one application using Greasemonkey, injecting JS to a page. 我正在使用Greasemonkey为一个应用程序编写覆盖UI,将JS注入页面。

Now I have issue to hook on some ajax calls that runs on the webpage. 现在,我有一个问题要挂在网页上运行的一些ajax调用上。 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? 用普通的javascript,是否有可能读取完全不同的脚本生成的控制台输出?

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 :) 提前致谢 :)

As others have stated, you can override the console.log() function with your own, and implement your own implementation: 正如其他人所述,您可以使用自己的命令覆盖console.log()函数,并实现自己的实现:

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

Redefine console.log and storage messages on a array: 重新定义console.log并在阵列上存储消息:

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

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. 执行console.log通常需要几毫秒,因此在生产代码中执行它不是一个好主意。

I would say programming in booleans (like ifDidX and ifDidY ) would be better. 我会说用boolean编程(例如ifDidXifDidY )会更好。 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"); } } 

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.log,首先检查您的状况,然后输出内容,下面是该示例代码

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. 尽管您需要非常小心,因为如果在这种情况下添加任何console.log,它将创建一个无限循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM