forked from academiadocodigo/TBGWebCharts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Browser.FMX.WebBrowser.pas
101 lines (86 loc) · 2.52 KB
/
Browser.FMX.WebBrowser.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
unit Browser.FMX.WebBrowser;
{$I TBGWebCharts.inc}
interface
uses
Interfaces,
FMX.WebBrowser;
Type
TModelBrowserFMXWebBrowser = class(TInterfacedObject, iModelBrowser)
private
FWebBrowser : TWebBrowser;
function ConvertString(aValue : String) : String;
public
constructor Create(Value : TObject);
destructor Destroy; override;
class function New(Value : TObject) : iModelBrowser;
procedure ExecuteScript(Value : iModelJSCommand);
function ExecuteScriptResult(Value : iModelJSCommand) : string;
procedure ExecuteScriptCallback(Value: iModelJSCommand);
function Generated(FHTML : string) : iModelBrowser;
end;
implementation
uses
System.SysUtils;
{ TModelBrowserFMX }
function TModelBrowserFMXWebBrowser.ConvertString(aValue: String): String;
var
rbs : RawByteString;
begin
rbs := UTF8Encode(aValue);
SetCodePage(rbs,0,false);
Result := UnicodeString(rbs);
end;
constructor TModelBrowserFMXWebBrowser.Create(Value: TObject);
begin
FWebBrowser := Value as TWebBrowser;
end;
destructor TModelBrowserFMXWebBrowser.Destroy;
begin
inherited;
end;
procedure TModelBrowserFMXWebBrowser.ExecuteScript(Value: iModelJSCommand);
begin
FWebBrowser.EvaluateJavaScript(Value.ResultCommand);
end;
procedure TModelBrowserFMXWebBrowser.ExecuteScriptCallback(
Value: iModelJSCommand);
var
ValueCallback : string;
proc : TProc<string>;
begin
if not Assigned(Value.CallBack) then
raise Exception.Create('Procedure para Callback inválida');
ValueCallback := ExecuteScriptResult(Value);
proc := Value.CallBack;
proc(ValueCallback);
end;
function TModelBrowserFMXWebBrowser.ExecuteScriptResult(Value: iModelJSCommand): string;
var
URL : string;
begin
FWebBrowser.EvaluateJavaScript(
value.ResultCommand + ';' +
'try {'+
'var e = "#" + document.getElementById(''' + Value.TagID + ''').' + Value.TagAttribute + ';' +
'history.replaceState(null, null, e);'+
'} catch (n) {' +
'document.location.href = e'+
'}');
URL := FWebBrowser.URL;
URL := Copy(URL, Pos('#', URL) + 1, Length(URL));
Result := URL;
end;
function TModelBrowserFMXWebBrowser.Generated(FHTML: string): iModelBrowser;
begin
Result := Self;
{$IF Defined(ANDROID) or Defined(IOS)}
FWebBrowser.LoadFromStrings(FHTML,'TBG');
{$ELSE}
FWebBrowser.LoadFromStrings(ConvertString(FHTML),'TBG');
{$ENDIF}
end;
class function TModelBrowserFMXWebBrowser.New(Value: TObject): iModelBrowser;
begin
Result := Self.Create(Value);
end;
end.