简体   繁体   English

读取Chrome扩展程序中选项卡的控制台输出

[英]Read A tab's console output from Chrome Extension

I need to develop a simple Chrome Extension for work that inspects Chrome's JS console for a certain value. 我需要开发用于工作的简单Chrome扩展程序,以检查Chrome的JS控制台的值。

Further Explanation: 进一步说明:

Basically, the need for this, is I need to know when a certain JS event has completed. 基本上,我需要知道某个JS事件何时完成。 I have placed a console.log("complete!") in my code to inform me when it is done. 我已经在代码中放置了console.log(“ complete!”)来通知我何时完成。

The problem I am facing, I cannot seem to find a way for a chrome extension to read output from the JS Console. 我面临的问题是, 我似乎找不到chrome扩展读取JS控制台输出的方法。

As far as I know you can't read the console output from JS, not even on a regular webpage. 据我所知,即使在常规网页上,您也无法读取JS的控制台输出。 You would have to hijack the console methods and save what is passed to them, and then do searches on the saved data. 您将不得不劫持console方法并保存传递给它们的内容,然后对保存的数据进行搜索。

From your goal it seems like you simply want to trigger a new event. 从您的目标看来,您似乎只是想触发一个新事件。 Which you can do by creating a Event / CustomEvent , setting up a listener for it, and firing off the event whenever you need it to be triggered. 您可以通过创建Event / CustomEvent ,为其设置侦听器并在需要触发该事件时触发该事件来执行此操作。

document.addEventListener("eventdone",function(e){
   console.log("The events done, now do work here");
});
//then whereever you had console.log("complete!")
var event = new CustomEvent('eventdone', { 'detail': 'Extra data' });
document.dispatchEvent(event);

Demo 演示

 document.addEventListener("eventdone",function(e){ document.body.innerHTML = "Event done"; }); document.querySelector("button").addEventListener("click",function(e){ setTimeout(function(){ var event = new CustomEvent('eventdone', { 'detail': 'Extra data' }); document.dispatchEvent(event); },2000); }); 
 <button>Click me</button> 

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

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