简体   繁体   English

覆盖 console.log 等以显示在 HTML 视图中

[英]Overriding console.log, etc to display inside HTML view

So, I'm working on a project in which the actual production environment has access to APIs that I can't get except by loading the page inside the production environment (they are using angular.$window.external ), but loading the page inside production means I can't access developer tools, console, etc. because it runs in an in-application browser instance.因此,我正在开发一个项目,其中实际生产环境可以访问我无法获得的 API,除非在生产环境中加载页面(他们使用angular.$window.external ),但加载页面内部生产意味着我无法访问开发人员工具、控制台等,因为它在应用程序内浏览器实例中运行。 So I am trying to output all console log/etc statements into a div in my document to try to troubleshoot when my API calls do not work as expected.因此,我尝试将 output 所有控制台日志/等语句放入文档中的 div 中,以尝试在我的 API 调用无法按预期工作时进行故障排除。

I got it sort of working, but it misses some things and I am not sure why.我得到了它的工作,但它错过了一些东西,我不知道为什么。

The code I am including as consoledebug.js :我作为consoledebug.js包含的代码:

// Code adapted from https://stackoverflow.com/a/5677825/12869266
// Include consoledebug.js at the end of a file, and create a div with
// the id "logs" in your html file where you want output.

var logDiv = document.getElementById('logs')

var addToLogDiv = function (type, text) {
  var node = document.createElement('p')
  node.className = type
  var content = document.createTextNode(type + ': ' + text)
  node.appendChild(content)
  logDiv.appendChild(node)
}

// define a new console
var console = (function (oldCons) {
  return {
    log: function (text) {
      oldCons.log(text)
      addToLogDiv('log', text)
    },
    info: function (text) {
      oldCons.info(text)
      addToLogDiv('info', text)
    },
    warn: function (text) {
      oldCons.warn(text)
      addToLogDiv('warn', text)
    },
    error: function (text) {
      oldCons.error(text)
      addToLogDiv('error', text)
    }
  }
})(window.console)

//Then redefine the old console
window.console = console

I put the <script src="../common/consoledebug.js"></script> tag at the end of the HTML document, right before the </body> tag.我将<script src="../common/consoledebug.js"></script>标签放在 HTML 文档的末尾,就在</body>标签之前。

When I run inside a regular chrome window, I get these lines in my HTML body:当我在常规 chrome window 中运行时,我在 HTML 正文中得到这些行:

log: test

error: TypeError: $window.external.SomeProprietaryAPI is not a function

But Chrome's log shows an additional error myhtml.html:35 Uncaught ReferenceError: someOtherProprietaryAPI is not defined at myhtml.html:35但是 Chrome 的日志显示了一个额外的错误myhtml.html:35 Uncaught ReferenceError: someOtherProprietaryAPI is not defined at myhtml.html:35

Any suggestions as to why this does not get captured by my function or what I can do about it?关于为什么这没有被我的 function 捕获的任何建议或我能做些什么? Or an alternative way to output all console output into the HTML document itself?或者 output所有控制台 output 到 HTML 文档本身的替代方法?

I tried including the script at the top of the file, but all that did was give me 2 errors on not being able to append to null (for the 2 log entries it's capturing, but not for the third one that it isn't).我尝试在文件顶部包含脚本,但所做的只是给了我 2 个错误,因为无法将 append 到 null (对于它正在捕获的 2 个日志条目,但不是对于它不是的第三个日志条目) .

I'm pretty new to Javascript and trying to get a base of existing AngularJS code working on a new version of the production environment that has switched from using IE to Chromium for HTML display.我对 Javascript 很陌生,并试图获取现有 AngularJS 代码的基础,该代码在新版本的生产环境中工作,该生产环境已从使用 IE 切换到用于 Z4C4AD5FCA2E7A3F74DBB1CED00381AAZ4 显示的 Chromium。

Edit: Put enough code to be relevant in a Plunker .编辑:在Plunker中放置足够的代码以使其相关。 As you can see the console.log('test') in the controller gets logged to the document, but the uncaught reference error in the body of the html document does not.如您所见,controller 中的console.log('test')被记录到文档中,但 html 文档正文中的未捕获参考错误没有。

Some enhancements:一些增强功能:

  1. Create a message queue array for errors that occur before full dom has loaded.为在完整 dom 加载之前发生的错误创建一个消息队列数组。
  2. Use window.addEventListener('error', callback) to listen for execution errors after new console defined定义新控制台后,使用window.addEventListener('error', callback)监听执行错误
  3. Wait for dom to load to query for the logDiv .等待 dom 加载以查询logDiv Before it is defined push messages into the message queue and then when dom loads check for anything in that queue在定义之前将消息推送到消息队列中,然后在 dom 加载时检查该队列中的任何内容
let logDiv, messageQueue =[];

window.addEventListener('load', function(){
    // assign the log element
    logDiv = document.getElementById('logs');
    if(messageQueue.length){
        // print your preload errors
        messageQueue.forEach(([type,text])=>addToLogDiv(type,text))
    }
})

var addToLogDiv = function (type, text) {
  if(logDiv){
    var node = document.createElement('p')
    node.className = type
    var content = document.createTextNode(type + ': ' + text)
    node.appendChild(content)
    logDiv.appendChild(node)
  }else{
    // before logDiv defined store any errors
    messageQueue.push([type, text]) 
  } 
}

// define a new console
var console = (function (oldCons) {
  return {
    // same as before
  }
})(window.console)

//Then redefine the old console
window.console = console
// after new console created
window.addEventListener('error', function(e){
  console.log(e.message)
})

Demo in head 头部演示

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

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