browserconsole.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. unit browserconsole;
  2. {$mode objfpc}
  3. interface
  4. uses
  5. js,web, sysutils;
  6. Const
  7. BrowserLineBreak = #10;
  8. DefaultMaxConsoleLines = 25;
  9. DefaultConsoleStyle = '.pasconsole { '+BrowserLineBreak+
  10. 'font-family: courier;'+BrowserLineBreak+
  11. 'font-size: 14px;'+BrowserLineBreak+
  12. 'background: #FFFFFF;'+BrowserLineBreak+
  13. 'color: #000000;'+BrowserLineBreak+
  14. 'display: block;'+BrowserLineBreak+
  15. '}';
  16. DefaultCRTConsoleStyle = '.pasconsole { '+BrowserLineBreak+
  17. 'font-family: courier;'+BrowserLineBreak+
  18. 'font-size: 14px;'+BrowserLineBreak+
  19. 'background: #000;'+BrowserLineBreak+
  20. 'color: #14fdce;'+BrowserLineBreak+
  21. 'display: block;'+BrowserLineBreak+
  22. '}';
  23. Var
  24. // Element ID for console output. If this is not set, body is used.
  25. // If you cange this, call HookConsole.
  26. ConsoleElementID : String;
  27. // Style to use for console lines. If you change this, call initconsole
  28. ConsoleStyle : String;
  29. // Style to use for console lines. You can change this at any time.
  30. MaxConsoleLines : Integer;
  31. // Copy console lines on newline to browser console log
  32. ConsoleLinesToBrowserLog : Boolean;
  33. // Clear console content
  34. Procedure ResetConsole;
  35. // Re-initialize console (style)
  36. Procedure InitConsole;
  37. // Re-hook console
  38. Procedure HookConsole;
  39. implementation
  40. Var
  41. LastLine,
  42. StyleElement,
  43. LinesParent,
  44. ConsoleElement : TJSElement;
  45. Procedure AppendLine;
  46. Var
  47. CurrentCount : Integer;
  48. S : TJSNode;
  49. begin
  50. CurrentCount:=0;
  51. S:=LinesParent.firstChild;
  52. While Assigned(S) do
  53. begin
  54. Inc(CurrentCount);
  55. S:=S.nextSibling;
  56. end;
  57. While CurrentCount>MaxConsoleLines do
  58. begin
  59. Dec(CurrentCount);
  60. LinesParent.removeChild(LinesParent.firstChild);
  61. end;
  62. LastLine:=Document.createElement('div');
  63. LastLine.className:='pasconsole';
  64. LinesParent.AppendChild(LastLine);
  65. end;
  66. Procedure WriteConsole(S : JSValue; NewLine : Boolean);
  67. Var
  68. CL: String;
  69. begin
  70. CL:=LastLine.InnerHtml;
  71. CL:=CL+String(S);
  72. cl:=StringReplace(cl,'<','&lt;',[rfReplaceAll]);
  73. cl:=StringReplace(cl,'>','&gt;',[rfReplaceAll]);
  74. cl:=StringReplace(cl,' ','&nbsp;',[rfReplaceAll]);
  75. cl:=StringReplace(cl,#13#10,'<br>',[rfReplaceAll]);
  76. cl:=StringReplace(cl,#10,'<br>',[rfReplaceAll]);
  77. cl:=StringReplace(cl,#13,'<br>',[rfReplaceAll]);
  78. LastLine.InnerHtml:=CL;
  79. if NewLine then
  80. begin
  81. if ConsoleLinesToBrowserLog then
  82. console.log(LastLine.InnerText);
  83. AppendLine;
  84. end;
  85. end;
  86. Procedure ResetConsole;
  87. begin
  88. if LinesParent=Nil then exit;
  89. While LinesParent.firstElementChild<>Nil do
  90. LinesParent.removeChild(LinesParent.firstElementChild);
  91. AppendLine;
  92. end;
  93. Procedure InitConsole;
  94. begin
  95. if ConsoleElement=Nil then
  96. exit;
  97. if (TJSString(ConsoleElement.nodeName).toLowerCase<>'body') then
  98. begin
  99. While ConsoleElement.firstElementChild<>Nil do
  100. ConsoleElement.removeChild(ConsoleElement.firstElementChild);
  101. end;
  102. StyleElement:=Document.createElement('style');
  103. StyleElement.innerText:=ConsoleStyle;
  104. ConsoleElement.appendChild(StyleElement);
  105. LinesParent:=Document.createElement('div');
  106. ConsoleElement.appendChild(LinesParent);
  107. end;
  108. Procedure HookConsole;
  109. begin
  110. ConsoleElement:=Nil;
  111. if (ConsoleElementID<>'') then
  112. ConsoleElement:=document.getElementById(ConsoleElementID);
  113. if (ConsoleElement=Nil) then
  114. ConsoleElement:=document.body;
  115. if ConsoleElement=Nil then
  116. exit;
  117. InitConsole;
  118. ResetConsole;
  119. SetWriteCallBack(@WriteConsole);
  120. end;
  121. initialization
  122. ConsoleLinesToBrowserLog:=True;
  123. ConsoleElementID:='pasjsconsole';
  124. ConsoleStyle:=DefaultConsoleStyle;
  125. MaxConsoleLines:=DefaultMaxConsoleLines;
  126. HookConsole;
  127. end.