browserconsole.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. { Unit that emulates console output in the browser.
  2. Copyright (C) 2020- Michael Van Canneyt [email protected]
  3. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public
  4. License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version
  5. with the following modification:
  6. As a special exception, the copyright holders of this library give you permission to link this library with independent modules
  7. to produce an executable, regardless of the license terms of these independent modules,and to copy and distribute the resulting
  8. executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions
  9. of the license of that module. An independent module is a module which is not derived from or based on this library. If you
  10. modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do
  11. not wish to do so, delete this exception statement from your version.
  12. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free
  15. Software Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
  16. }
  17. unit browserconsole;
  18. {$mode objfpc}
  19. interface
  20. uses
  21. js, web, Rtl.BrowserLoadHelper,sysutils;
  22. Const
  23. BrowserLineBreak = #10;
  24. DefaultMaxConsoleLines = 25;
  25. DefaultConsoleStyle = '.pasconsole { '+BrowserLineBreak+
  26. 'font-family: courier;'+BrowserLineBreak+
  27. 'font-size: 14px;'+BrowserLineBreak+
  28. 'background: #FFFFFF;'+BrowserLineBreak+
  29. 'color: #000000;'+BrowserLineBreak+
  30. 'display: block;'+BrowserLineBreak+
  31. '}';
  32. DefaultCRTConsoleStyle = '.pasconsole { '+BrowserLineBreak+
  33. 'font-family: courier;'+BrowserLineBreak+
  34. 'font-size: 14px;'+BrowserLineBreak+
  35. 'background: #000;'+BrowserLineBreak+
  36. 'color: #14fdce;'+BrowserLineBreak+
  37. 'display: block;'+BrowserLineBreak+
  38. '}';
  39. Var
  40. // Element ID for console output. If this is not set, body is used.
  41. // If you cange this, call HookConsole.
  42. ConsoleElementID : String;
  43. // Style to use for console lines. If you change this, call initconsole
  44. ConsoleStyle : String;
  45. // Style to use for console lines. You can change this at any time.
  46. MaxConsoleLines : Integer;
  47. // Copy console lines on newline to browser console log
  48. ConsoleLinesToBrowserLog : Boolean;
  49. // Clear console content
  50. Procedure ResetConsole;
  51. // Re-initialize console (style)
  52. Procedure InitConsole;
  53. // Re-hook console
  54. Procedure HookConsole;
  55. implementation
  56. Var
  57. LastLine,
  58. StyleElement,
  59. LinesParent,
  60. ConsoleElement : TJSElement;
  61. Procedure AppendLine;
  62. Var
  63. CurrentCount : Integer;
  64. S : TJSNode;
  65. begin
  66. CurrentCount:=0;
  67. S:=LinesParent.firstChild;
  68. While Assigned(S) do
  69. begin
  70. Inc(CurrentCount);
  71. S:=S.nextSibling;
  72. end;
  73. While CurrentCount>MaxConsoleLines do
  74. begin
  75. Dec(CurrentCount);
  76. LinesParent.removeChild(LinesParent.firstChild);
  77. end;
  78. LastLine:=Document.createElement('div');
  79. LastLine.className:='pasconsole';
  80. LinesParent.AppendChild(LastLine);
  81. end;
  82. Function EscapeString(S : String) : String;
  83. Var
  84. CL : string;
  85. begin
  86. cl:=StringReplace(S,'<','&lt;',[rfReplaceAll]);
  87. cl:=StringReplace(cl,'>','&gt;',[rfReplaceAll]);
  88. cl:=StringReplace(cl,' ','&nbsp;',[rfReplaceAll]);
  89. cl:=StringReplace(cl,#13#10,'<br>',[rfReplaceAll]);
  90. cl:=StringReplace(cl,#10,'<br>',[rfReplaceAll]);
  91. cl:=StringReplace(cl,#13,'<br>',[rfReplaceAll]);
  92. Result:=CL;
  93. end;
  94. Procedure WriteConsole(S : JSValue; NewLine : Boolean);
  95. Var
  96. CL: String;
  97. begin
  98. CL:=LastLine.InnerHtml;
  99. CL:=CL+EscapeString(String(S));
  100. LastLine.InnerHtml:=CL;
  101. if NewLine then
  102. begin
  103. if ConsoleLinesToBrowserLog then
  104. console.log(LastLine.InnerText);
  105. AppendLine;
  106. end;
  107. end;
  108. Procedure ResetConsole;
  109. begin
  110. if LinesParent=Nil then exit;
  111. While LinesParent.firstElementChild<>Nil do
  112. LinesParent.removeChild(LinesParent.firstElementChild);
  113. AppendLine;
  114. end;
  115. Procedure InitConsole;
  116. begin
  117. if ConsoleElement=Nil then
  118. exit;
  119. if (TJSString(ConsoleElement.nodeName).toLowerCase<>'body') then
  120. begin
  121. While ConsoleElement.firstElementChild<>Nil do
  122. ConsoleElement.removeChild(ConsoleElement.firstElementChild);
  123. end;
  124. StyleElement:=Document.createElement('style');
  125. StyleElement.innerText:=ConsoleStyle;
  126. ConsoleElement.appendChild(StyleElement);
  127. LinesParent:=Document.createElement('div');
  128. ConsoleElement.appendChild(LinesParent);
  129. end;
  130. Procedure HookConsole;
  131. begin
  132. ConsoleElement:=Nil;
  133. if (ConsoleElementID<>'') then
  134. ConsoleElement:=document.getElementById(ConsoleElementID);
  135. if (ConsoleElement=Nil) then
  136. ConsoleElement:=document.body;
  137. if ConsoleElement=Nil then
  138. exit;
  139. InitConsole;
  140. ResetConsole;
  141. SetWriteCallBack(@WriteConsole);
  142. end;
  143. initialization
  144. ConsoleLinesToBrowserLog:=True;
  145. ConsoleElementID:='pasjsconsole';
  146. ConsoleStyle:=DefaultConsoleStyle;
  147. MaxConsoleLines:=DefaultMaxConsoleLines;
  148. HookConsole;
  149. end.