1
0

betterLogging.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. rewireLoggingToElement(
  2. () => document.getElementById('log'),
  3. () => document.getElementById('log-container'),
  4. true,
  5. )
  6. function rewireLoggingToElement(eleLocator, eleOverflowLocator, autoScroll) {
  7. fixLoggingFunc('log')
  8. function fixLoggingFunc(name) {
  9. console.old = console.log
  10. console.log = function(...arg) {
  11. const output = produceOutput(name, arg)
  12. const eleLog = eleLocator()
  13. if (autoScroll) {
  14. const eleContainerLog = eleOverflowLocator()
  15. const isScrolledToBottom =
  16. eleContainerLog.scrollHeight - eleContainerLog.clientHeight <=
  17. eleContainerLog.scrollTop + 1
  18. eleLog.innerHTML += output + '<br>'
  19. if (isScrolledToBottom) {
  20. eleContainerLog.scrollTop =
  21. eleContainerLog.scrollHeight - eleContainerLog.clientHeight
  22. }
  23. } else {
  24. eleLog.innerHTML += output + '<br>'
  25. }
  26. }
  27. }
  28. function produceOutput(name, args) {
  29. arg = args[0].replace('%c','')
  30. return '<span style="'+ args[1]+'">'+arg+'</span>&nbsp;'
  31. }
  32. }