forked from academiadocodigo/TBGWebCharts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Browser.VCL.WebBrowser.pas
228 lines (203 loc) · 5.66 KB
/
Browser.VCL.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
unit Browser.VCL.WebBrowser;
{$I TBGWebCharts.inc}
interface
uses
Interfaces,
SHDocVw;
Type
TModelBrowserVCLWebBrowser = class(TInterfacedObject, iModelBrowser)
private
FWebBrowser : TWebBrowser;
procedure HtmlBrowserGenerated(CONST HTMLCode: string);
function ConvertString(aValue : String) : String;
procedure DefineIEVersion;
{$IF RTLVERSION > 22 }
procedure BeforeNavigate(ASender: TObject; const pDisp: IDispatch; const URL, Flags, TargetFrameName, PostData, Headers: OleVariant; var Cancel: WordBool);
{$ENDIF}
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
MSHTML,
System.SysUtils,
System.Variants,
System.Classes,
{$IF RTLVERSION > 22 }
CallBackJS,
{$ENDIF}
{$IF RTLVERSION > 31 }
Win.Registry,
{$ELSE}
Registry,
{$ENDIF}
Winapi.ActiveX, Winapi.Windows;
{ TModelHTMLFactory }
{$IF RTLVERSION > 22 }
procedure TModelBrowserVCLWebBrowser.BeforeNavigate(ASender: TObject;
const pDisp: IDispatch; const URL, Flags, TargetFrameName, PostData,
Headers: OleVariant; var Cancel: WordBool);
var
Target : String;
Aux, Method: string;
Params : TStringList;
begin
Target := URL;
if UpperCase(Target).StartsWith('ACTIONCALLBACKJS') then
begin
Method := Copy(Target, Pos(':', Target) + 1, Length(Target));
Method := Copy(Method, 1, Pos('(', Method) - 1);
Params := TStringList.Create;
try
Aux := Copy(Target, Pos('(', Target) + 1, Length(Target));
Aux := Copy(Aux, 1, LastDelimiter(')', Aux)-1);
Params.CommaText := Aux;
if not Method.IsEmpty then
if vCallBackJS.TryGetValue(Method, Params) then
Cancel := True;
finally
Params.Free;
end;
end;
end;
{$ENDIF}
function TModelBrowserVCLWebBrowser.ConvertString(aValue: String): String;
var
rbs : RawByteString;
begin
rbs := UTF8Encode(aValue);
SetCodePage(rbs,0,false);
Result := UnicodeString(rbs);
end;
constructor TModelBrowserVCLWebBrowser.Create(Value: TObject);
begin
FWebBrowser := Value as TWebBrowser;
DefineIEVersion;
{$IF RTLVERSION > 22 }
FWebBrowser.OnBeforeNavigate2 := BeforeNavigate;
{$ENDIF}
end;
procedure TModelBrowserVCLWebBrowser.DefineIEVersion;
const
REG_KEY = 'Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION';
var
Reg: TRegistry;
AppName: String;
begin
AppName := ExtractFileName(ExtractFileName(ParamStr(0)));
Reg := nil;
try
Reg := TRegistry.Create();
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey(REG_KEY, True) then
begin
// if Versao = 0 then
// Reg.DeleteValue(AppName)
// else
Reg.WriteInteger(AppName, 11000);
Reg.CloseKey;
end;
except
end;
if (Assigned(Reg)) then
FreeAndNil(Reg);
end;
destructor TModelBrowserVCLWebBrowser.Destroy;
begin
inherited;
end;
procedure TModelBrowserVCLWebBrowser.ExecuteScript(Value: iModelJSCommand);
var
Doc : IHTMLDocument2;
HTMLWindow: IHTMLWindow2;
begin
Doc := FWebBrowser.Document as IHTMLDocument2;
if Assigned(Doc) then
begin
HTMLWindow := Doc.parentWindow;
if Assigned(HTMLWindow) then
begin
try
HTMLWindow.execScript(Value.ResultCommand , 'javascript');
except on E: Exception do
raise Exception.Create('Erro ao Executar Script');
end;
end;
end;
end;
procedure TModelBrowserVCLWebBrowser.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 TModelBrowserVCLWebBrowser.ExecuteScriptResult(Value: iModelJSCommand): string;
var
Doc : IHTMLDocument2;
body : IHTMLElement2;
Tags : IHTMLElementCollection;
Tag : IHTMLElement;
I : Integer;
begin
Result := '';
ExecuteScript(Value);
if not Supports(FWebBrowser.Document, IHTMLDocument2, Doc) then
raise Exception.Create('Documento HTML Inválido');
if not Supports(Doc.body, IHTMLElement2, Body) then
raise Exception.Create('Não Foi Possível Encontrar o Elemento <body>');
Tags := body.getElementsByTagName(UpperCase(Value.TagName));
for I := 0 to Pred(Tags.length) do
begin
Tag := Tags.item(I, EmptyParam) as IHTMLElement;
if Tag.id = Value.TagId then
begin
Result := Tag.getAttribute(Value.TagAttribute, 0);
break;
end;
end;
end;
function TModelBrowserVCLWebBrowser.Generated(FHTML: string): iModelBrowser;
begin
Result := Self;
FWebBrowser.Silent := True;
HtmlBrowserGenerated(ConvertString(FHTML));
end;
procedure TModelBrowserVCLWebBrowser.HtmlBrowserGenerated(const HTMLCode: string);
var
sl : TStringList;
ms : TMemoryStream;
begin
if NOT Assigned(FWebBrowser.Document) then
FWebBrowser.Navigate('about:blank');
sl := TStringList.Create;
try
ms := TMemoryStream.Create;
try
sl.Text := HTMLCode;
sl.SaveToStream(ms) ;
ms.Seek(0, 0);
(FWebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms)) ;
finally
ms.Free;
end;
finally
sl.Free;
end;
end;
class function TModelBrowserVCLWebBrowser.New(Value: TObject): iModelBrowser;
begin
Result := Self.Create(Value);
end;
end.