browserconsole.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. Procedure WriteConsole(S : JSValue; NewLine : Boolean);
  83. Var
  84. CL: String;
  85. begin
  86. CL:=LastLine.InnerHtml;
  87. CL:=CL+String(S);
  88. cl:=StringReplace(cl,'<','&lt;',[rfReplaceAll]);
  89. cl:=StringReplace(cl,'>','&gt;',[rfReplaceAll]);
  90. cl:=StringReplace(cl,' ','&nbsp;',[rfReplaceAll]);
  91. cl:=StringReplace(cl,#13#10,'<br>',[rfReplaceAll]);
  92. cl:=StringReplace(cl,#10,'<br>',[rfReplaceAll]);
  93. cl:=StringReplace(cl,#13,'<br>',[rfReplaceAll]);
  94. LastLine.InnerHtml:=CL;
  95. if NewLine then
  96. begin
  97. if ConsoleLinesToBrowserLog then
  98. console.log(LastLine.InnerText);
  99. AppendLine;
  100. end;
  101. end;
  102. Procedure ResetConsole;
  103. begin
  104. if LinesParent=Nil then exit;
  105. While LinesParent.firstElementChild<>Nil do
  106. LinesParent.removeChild(LinesParent.firstElementChild);
  107. AppendLine;
  108. end;
  109. Procedure InitConsole;
  110. begin
  111. if ConsoleElement=Nil then
  112. exit;
  113. if (TJSString(ConsoleElement.nodeName).toLowerCase<>'body') then
  114. begin
  115. While ConsoleElement.firstElementChild<>Nil do
  116. ConsoleElement.removeChild(ConsoleElement.firstElementChild);
  117. end;
  118. StyleElement:=Document.createElement('style');
  119. StyleElement.innerText:=ConsoleStyle;
  120. ConsoleElement.appendChild(StyleElement);
  121. LinesParent:=Document.createElement('div');
  122. ConsoleElement.appendChild(LinesParent);
  123. end;
  124. Procedure HookConsole;
  125. begin
  126. ConsoleElement:=Nil;
  127. if (ConsoleElementID<>'') then
  128. ConsoleElement:=document.getElementById(ConsoleElementID);
  129. if (ConsoleElement=Nil) then
  130. ConsoleElement:=document.body;
  131. if ConsoleElement=Nil then
  132. exit;
  133. InitConsole;
  134. ResetConsole;
  135. SetWriteCallBack(@WriteConsole);
  136. end;
  137. initialization
  138. ConsoleLinesToBrowserLog:=True;
  139. ConsoleElementID:='pasjsconsole';
  140. ConsoleStyle:=DefaultConsoleStyle;
  141. MaxConsoleLines:=DefaultMaxConsoleLines;
  142. HookConsole;
  143. end.