console.html 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. {%- macro console_replacement(bl_alert) -%}
  2. <script type="text/javascript">
  3. var ar_logging = Array();
  4. // override console output etc.
  5. (function(){
  6. var _log = console.log;
  7. var _error = console.error;
  8. var _warning = console.warning;
  9. var _dir = console.dir;
  10. console.error = function(errMessage){
  11. ar_logging.push({type: "error", value: String(errMessage)});
  12. // $(s_div_id).text("Error! - " + String(errMessage));
  13. _error.apply(console,arguments);
  14. };
  15. console.log = function(logMessage){
  16. ar_logging.push({type: "log", value: String(logMessage)});
  17. // $(s_div_id).text("Log - " + String(logMessage));
  18. _log.apply(console,arguments);
  19. };
  20. console.dir = function(dirObject){
  21. // $(s_div_id).text("dir - " + JSON.stringify(Object.keys(dirObject)));
  22. _dir.apply(console,arguments);
  23. };
  24. console.warning = function(warnMessage){
  25. ar_logging.push({type: "warning", value: String(warnMessage)});
  26. // $(s_div_id).text("Warning - " + String(warnMessage));
  27. _warning.apply(console,arguments);
  28. };
  29. })();// end of overriding console methods
  30. var bl_alert = {% if bl_alert is sameas true %}true{% else %}false{% endif %};
  31. function show_logging() {
  32. if (ar_logging.length>0) {
  33. var s_logging = "";
  34. if (bl_alert==false) { s_logging = "<div>\n<ul>\n"; }
  35. for (let it in ar_logging) {
  36. var o_item = ar_logging[it];
  37. var s_type = o_item.type;
  38. var s_value = o_item.value;
  39. if (bl_alert==true) {
  40. s_logging = s_logging + String(s_type) + " - " + String(s_value) + "\n";
  41. } else {
  42. s_logging = s_logging + "<li><b>" + String(s_type) + "</b> - " + String(s_value) + "</li>\n";
  43. }
  44. }// end of looping through ar_logging items
  45. if (bl_alert==false) { s_logging = s_logging + "</ul>\n"; }
  46. if (bl_alert) {
  47. alert(s_logging);
  48. } else {
  49. //var w_console = window.open("", "console_output", "location=no,menubar=no,toolbars=no,scrollbars=yes,resizable=yes");
  50. //w_console.document.body.innerHTML = s_logging;
  51. var dlg = document.createElement("dialog");
  52. dlg.role = "dialog";
  53. dlg.style.backgroundColor = "silver";
  54. dlg.style.color = "black";
  55. dlg.style.backdropFilter = "blur(10px)";
  56. var inner_div = document.createElement("div");
  57. inner_div.innerHTML = "<form method='dialog'><button autofocus>close</button></form>\n" + s_logging;
  58. dlg.appendChild(inner_div);
  59. var body = document.getElementsByTagName("body")[0];
  60. body.appendChild(dlg);
  61. dlg.showModal();
  62. }// end of checking if should just alert or not
  63. // if (confirm("clear log?")) { ar_logging = Array(); }
  64. } else {
  65. alert("No logging records currently captured");
  66. }// end of length check on global ar_logging array
  67. }// end of show_logging function
  68. </script>
  69. <a href="#" onclick="show_logging();">output console</a><br>
  70. {% endmacro -%}