browserapp.pas 3.2 KB

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