browserapp.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. unit browserapp;
  2. {$mode objfpc}
  3. interface
  4. uses
  5. Classes, SysUtils, Types, JS, web, CustApp;
  6. type
  7. { TBrowserApplication }
  8. TBrowserApplication = class(TCustomApplication)
  9. protected
  10. function GetHTMLElement(aID : String) : TJSHTMLElement;
  11. function CreateHTMLElement(aTag : String; aID : String = '') : TJSHTMLElement;
  12. procedure DoRun; override;
  13. function GetConsoleApplication: boolean; override;
  14. Function LogGetElementErrors : Boolean; virtual;
  15. function GetLocation: String; override;
  16. public
  17. procedure GetEnvironmentList(List: TStrings; NamesOnly: Boolean); override;
  18. procedure ShowException(E: Exception); override;
  19. procedure HandleException(Sender: TObject); override;
  20. end;
  21. procedure ReloadEnvironmentStrings;
  22. implementation
  23. uses Rtl.BrowserLoadHelper;
  24. var
  25. EnvNames: TJSObject;
  26. Params : TStringDynArray;
  27. procedure ReloadEnvironmentStrings;
  28. var
  29. I : Integer;
  30. S,N : String;
  31. A,P : TStringDynArray;
  32. begin
  33. if Assigned(EnvNames) then
  34. FreeAndNil(EnvNames);
  35. EnvNames:=TJSObject.new;
  36. S:=Window.Location.search;
  37. S:=Copy(S,2,Length(S)-1);
  38. A:=TJSString(S).split('&');
  39. for I:=0 to Length(A)-1 do
  40. begin
  41. P:=TJSString(A[i]).split('=');
  42. N:=LowerCase(decodeURIComponent(P[0]));
  43. if Length(P)=2 then
  44. EnvNames[N]:=decodeURIComponent(P[1])
  45. else if Length(P)=1 then
  46. EnvNames[N]:=''
  47. end;
  48. end;
  49. procedure ReloadParamStrings;
  50. begin
  51. SetLength(Params,1);
  52. Params[0]:=Window.location.pathname;
  53. end;
  54. function GetParamCount: longint;
  55. begin
  56. Result:=Length(Params)-1;
  57. end;
  58. function GetParamStr(Index: longint): String;
  59. begin
  60. Result:=Params[Index]
  61. end;
  62. function MyGetEnvironmentVariable(Const EnvVar: String): String;
  63. Var
  64. aName : String;
  65. begin
  66. aName:=Lowercase(EnvVar);
  67. if EnvNames.hasOwnProperty(aName) then
  68. Result:=String(EnvNames[aName])
  69. else
  70. Result:='';
  71. end;
  72. function MyGetEnvironmentVariableCount: Integer;
  73. begin
  74. Result:=length(TJSOBject.getOwnPropertyNames(envNames));
  75. end;
  76. function MyGetEnvironmentString(Index: Integer): String;
  77. begin
  78. Result:=String(EnvNames[TJSOBject.getOwnPropertyNames(envNames)[Index]]);
  79. end;
  80. { TBrowserApplication }
  81. function TBrowserApplication.GetHTMLElement(aID: String): TJSHTMLElement;
  82. begin
  83. Result:=TJSHTMLElement(Document.getElementById(aID));
  84. if (Result=Nil) and LogGetElementErrors then
  85. Writeln('Could not find element with ID ',aID);
  86. end;
  87. function TBrowserApplication.CreateHTMLElement(aTag: String; aID: String): TJSHTMLElement;
  88. begin
  89. Result:=TJSHTMLElement(Document.createElement(aTag));
  90. if aID<>'' then
  91. Result.ID:=aID;
  92. end;
  93. procedure TBrowserApplication.DoRun;
  94. begin
  95. // Override in descendent classes.
  96. end;
  97. function TBrowserApplication.GetConsoleApplication: boolean;
  98. begin
  99. Result:=true;
  100. end;
  101. function TBrowserApplication.LogGetElementErrors: Boolean;
  102. begin
  103. Result:=True;
  104. end;
  105. function TBrowserApplication.GetLocation: String;
  106. begin
  107. Result:=''; // ToDo ExtractFilePath(GetExeName);
  108. end;
  109. procedure TBrowserApplication.GetEnvironmentList(List: TStrings;
  110. NamesOnly: Boolean);
  111. var
  112. Names: TStringDynArray;
  113. i: Integer;
  114. begin
  115. Names:=TJSObject.getOwnPropertyNames(EnvNames);
  116. for i:=0 to length(Names)-1 do
  117. begin
  118. if NamesOnly then
  119. List.Add(Names[i])
  120. else
  121. List.Add(Names[i]+'='+String(EnvNames[Names[i]]));
  122. end;
  123. end;
  124. procedure TBrowserApplication.ShowException(E: Exception);
  125. Var
  126. S : String;
  127. begin
  128. if (E<>nil) then
  129. S:=E.ClassName+': '+E.Message
  130. else if ExceptObjectJS then
  131. s:=TJSObject(ExceptObjectJS).toString;
  132. window.alert('Unhandled exception caught:'+S);
  133. end;
  134. procedure TBrowserApplication.HandleException(Sender: TObject);
  135. begin
  136. if ExceptObject is Exception then
  137. ShowException(ExceptObject);
  138. inherited HandleException(Sender);
  139. end;
  140. initialization
  141. IsConsole:=true;
  142. OnParamCount:=@GetParamCount;
  143. OnParamStr:=@GetParamStr;
  144. ReloadEnvironmentStrings;
  145. ReloadParamStrings;
  146. OnGetEnvironmentVariable:=@MyGetEnvironmentVariable;
  147. OnGetEnvironmentVariableCount:=@MyGetEnvironmentVariableCount;
  148. OnGetEnvironmentString:=@MyGetEnvironmentString;
  149. end.