-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebBrowserUtils.pas
430 lines (369 loc) · 12 KB
/
WebBrowserUtils.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
unit WebBrowserUtils;
{$Include 'FastPHP.inc'}
interface
uses
// TODO: "{$IFDEF USE_SHDOCVW_TLB}ShDocVw_TLB{$ELSE}ShDocVw{$ENDIF}" does not work with Delphi 10.2
Windows, ShDocVw, SysUtils, Forms, Classes;
procedure WaitForBrowser(AWebBrowser: TWebbrowser);
function WebBrowserLoadHTML(AWebBrowser: TWebBrowser; const AHTML: string;
const AFakeURL: string=''): boolean;
function WebBrowserLoadStream(AWebBrowser: TWebBrowser; const AStream: TStream;
const AFakeURL: string=''): boolean;
type
TWebBrowserEx = class helper for TWebBrowser
public
procedure Clear;
procedure Wait;
function LoadHTML(const HTML: string; const AFakeURL: string=''): boolean;
function LoadStream(const Stream: TStream; const AFakeURL: string=''): boolean;
procedure ReadMetaTags(outSL: TStringList);
end;
implementation
uses
ActiveX, urlmon;
type
(*
ILoadHTMLMoniker = interface(IMoniker)
['{DCAE3F41-9B38-40EB-B7D0-4AF0FBFBE5AB}']
procedure InitLoader(sContent, sBaseUrl: string);
end;
*)
TLoadHTMLMoniker = class (TInterfacedObject, IMoniker{, ILoadHTMLMoniker})
private
m_stream: IStream;
m_sBaseName: string;
public
procedure InitLoader(sContent, sBaseUrl: string);
procedure InitLoaderStream(sStream: TStream; sBaseUrl: string);
{$REGION 'IMoniker members'}
function BindToObject(const bc: IBindCtx; const mkToLeft: IMoniker;
const iidResult: TIID; out vResult): HResult; stdcall;
function BindToStorage(const bc: IBindCtx; const mkToLeft: IMoniker;
const iid: TIID; out vObj): HResult; stdcall;
function Reduce(const bc: IBindCtx; dwReduceHowFar: Longint;
mkToLeft: PIMoniker; out mkReduced: IMoniker): HResult; stdcall;
function ComposeWith(const mkRight: IMoniker; fOnlyIfNotGeneric: BOOL;
out mkComposite: IMoniker): HResult; stdcall;
function Enum(fForward: BOOL; out enumMoniker: IEnumMoniker): HResult;
stdcall;
function IsEqual(const mkOtherMoniker: IMoniker): HResult; stdcall;
function Hash(out dwHash: Longint): HResult; stdcall;
function IsRunning(const bc: IBindCtx; const mkToLeft: IMoniker;
const mkNewlyRunning: IMoniker): HResult; stdcall;
function GetTimeOfLastChange(const bc: IBindCtx; const mkToLeft: IMoniker;
out filetime: TFileTime): HResult; stdcall;
function Inverse(out mk: IMoniker): HResult; stdcall;
function CommonPrefixWith(const mkOther: IMoniker;
out mkPrefix: IMoniker): HResult; stdcall;
function RelativePathTo(const mkOther: IMoniker;
out mkRelPath: IMoniker): HResult; stdcall;
function GetDisplayName(const bc: IBindCtx; const mkToLeft: IMoniker;
out pszDisplayName: POleStr): HResult; stdcall;
function ParseDisplayName(const bc: IBindCtx; const mkToLeft: IMoniker;
pszDisplayName: POleStr; out chEaten: Longint;
out mkOut: IMoniker): HResult; stdcall;
function IsSystemMoniker(out dwMksys: Longint): HResult; stdcall;
{$ENDREGION}
{$REGION 'IPersistStream members'}
function IsDirty: HResult; stdcall;
function Load(const stm: IStream): HResult; stdcall;
function Save(const stm: IStream; fClearDirty: BOOL): HResult; stdcall;
function GetSizeMax(out cbSize: Largeint): HResult; stdcall;
{$ENDREGION}
{$REGION 'IPersist members'}
function GetClassID(out classID: TCLSID): HResult; stdcall;
{$ENDREGION}
end;
// http://stackoverflow.com/questions/12605323/globalalloc-causes-my-delphi-app-hang
function StrToGlobalHandle(const aText: string): HGLOBAL;
var
ptr: PChar;
begin
Result := 0;
if aText <> '' then
begin
Result := GlobalAlloc(GMEM_MOVEABLE or GMEM_ZEROINIT, (length(aText) + 1) * SizeOf(Char));
if Result <> 0 then
begin
ptr := GlobalLock(Result);
if Assigned(ptr) then
begin
StrCopy(ptr, PChar(aText));
GlobalUnlock(Result);
end
end;
end;
end;
procedure WaitForBrowser(AWebBrowser: TWebbrowser);
begin
while (AWebBrowser.ReadyState <> READYSTATE_COMPLETE) and
(not Assigned(Application) or not Application.Terminated) do
begin
if Assigned(Application) then Application.ProcessMessages;
Sleep(50);
end;
end;
function WebBrowserLoadHTML(AWebBrowser: TWebBrowser; const AHTML: string;
const AFakeURL: string=''): boolean;
var
bindctx: IBindCtx;
pPM: IPersistMoniker;
loader: TLoadHTMLMoniker;
url: string;
begin
if AFakeURL <> '' then
url := AFakeURL
else if AWebBrowser.LocationURL <> '' then
url := AWebBrowser.LocationURL
else
url := 'about:blank';
if AWebBrowser.Document = nil then
begin
AWebBrowser.Navigate('about:blank');
WaitForBrowser(AWebBrowser);
end;
pPM := AWebBrowser.Document as IPersistMoniker;
if (pPM = nil) then
begin
result := false;
exit;
end;
bindctx := nil;
CreateBindCtx(0, bindctx);
if (bindctx = nil) then
begin
result := false;
exit;
end;
try
// TODO: Delphi2007 and FastMM4 say that here we have a memory leak
loader := TLoadHTMLMoniker.Create;
loader.InitLoader(AHTML, url);
except
if Assigned(loader) then FreeAndNil(loader);
result := false;
exit;
end;
result := pPM.Load(true, loader, bindctx, STGM_READ) = S_OK;
if not result and Assigned(loader) then FreeAndNil(loader);
end;
function WebBrowserLoadStream(AWebBrowser: TWebBrowser; const AStream: TStream;
const AFakeURL: string=''): boolean;
var
bindctx: IBindCtx;
pPM: IPersistMoniker;
loader: TLoadHTMLMoniker;
url: string;
begin
if AFakeURL <> '' then
url := AFakeURL
else if AWebBrowser.LocationURL <> '' then
url := AWebBrowser.LocationURL
else
url := 'about:blank';
if AWebBrowser.Document = nil then
begin
AWebBrowser.Navigate('about:blank');
WaitForBrowser(AWebBrowser);
end;
pPM := AWebBrowser.Document as IPersistMoniker;
if (pPM = nil) then
begin
result := false;
exit;
end;
bindctx := nil;
CreateBindCtx(0, bindctx);
if (bindctx = nil) then
begin
result := false;
exit;
end;
try
// TODO: Delphi2007 and FastMM4 say that here we have a memory leak
loader := TLoadHTMLMoniker.Create;
loader.InitLoaderStream(AStream, url);
except
if Assigned(loader) then FreeAndNil(loader);
result := false;
exit;
end;
result := pPM.Load(true, loader, bindctx, STGM_READ) = S_OK;
if not result and Assigned(loader) then FreeAndNil(loader);
end;
{ TLoadHTMLMoniker }
// TLoadHTMLMoniker. Translated from C# to Delphi by Daniel Marschall
// Resources:
// - http://stackoverflow.com/questions/40927080/relative-urls-in-a-twebbrowser-containing-custom-html-code
// - https://github.com/kuza55/csexwb2/blob/master/General_Classes/LoadHTMLMoniker.cs
// - https://github.com/kuza55/csexwb2/blob/master/cEXWB.cs#L1769
procedure TLoadHTMLMoniker.InitLoader(sContent, sBaseUrl: string);
resourcestring
SCannotAllocMemory = 'Cannot create IStream.';
var
hr: integer;
begin
m_sBaseName := sBaseUrl;
hr := CreateStreamOnHGlobal(StrToGlobalHandle(sContent), true, m_stream);
if ((hr <> S_OK) or (m_stream = nil)) then raise Exception.Create(SCannotAllocMemory);
end;
procedure TLoadHTMLMoniker.InitLoaderStream(sStream: TStream; sBaseUrl: string);
resourcestring
SCannotAllocMemory = 'Cannot create IStream.';
begin
m_sBaseName := sBaseUrl;
m_stream := TStreamAdapter.Create(sStream, soReference) as IStream;
if (m_stream = nil) then raise Exception.Create(SCannotAllocMemory);
end;
function TLoadHTMLMoniker.GetDisplayName(const bc: IBindCtx;
const mkToLeft: IMoniker; out pszDisplayName: POleStr): HResult;
var
bufSize: integer;
wTest: WideString;
begin
// pszDisplayName := PWideChar(WideString(m_sBaseName));
// I am not sure if that is correct......
bufSize := (Length(m_sBaseName)+1) * SizeOf(WideChar);
pszDisplayName := CoTaskMemAlloc(bufSize);
wTest := m_sBaseName;
CopyMemory(pszDisplayName, PWideChar(wTest), bufSize);
result := S_OK;
end;
function TLoadHTMLMoniker.BindToStorage(const bc: IBindCtx;
const mkToLeft: IMoniker; const iid: TIID; out vObj): HResult;
const
IID_IStream: TGUID = '{0000000C-0000-0000-C000-000000000046}';
begin
if IsEqualIID(iid, IID_IStream) then
begin
IStream(vObj) := m_stream;
result := S_OK;
end
else
begin
IStream(vObj) := nil;
result := E_NOINTERFACE;
end;
end;
{$REGION 'Not implemented'}
function TLoadHTMLMoniker.BindToObject(const bc: IBindCtx;
const mkToLeft: IMoniker; const iidResult: TIID; out vResult): HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.CommonPrefixWith(const mkOther: IMoniker;
out mkPrefix: IMoniker): HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.ComposeWith(const mkRight: IMoniker;
fOnlyIfNotGeneric: BOOL; out mkComposite: IMoniker): HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.Enum(fForward: BOOL;
out enumMoniker: IEnumMoniker): HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.GetClassID(out classID: TCLSID): HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.GetSizeMax(out cbSize: Largeint): HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.GetTimeOfLastChange(const bc: IBindCtx;
const mkToLeft: IMoniker; out filetime: TFileTime): HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.Hash(out dwHash: Integer): HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.Inverse(out mk: IMoniker): HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.IsDirty: HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.IsEqual(const mkOtherMoniker: IMoniker): HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.IsRunning(const bc: IBindCtx; const mkToLeft,
mkNewlyRunning: IMoniker): HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.IsSystemMoniker(out dwMksys: Integer): HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.Load(const stm: IStream): HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.ParseDisplayName(const bc: IBindCtx;
const mkToLeft: IMoniker; pszDisplayName: POleStr; out chEaten: Integer;
out mkOut: IMoniker): HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.Reduce(const bc: IBindCtx; dwReduceHowFar: Integer;
mkToLeft: PIMoniker; out mkReduced: IMoniker): HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.RelativePathTo(const mkOther: IMoniker;
out mkRelPath: IMoniker): HResult;
begin
result := E_NOTIMPL;
end;
function TLoadHTMLMoniker.Save(const stm: IStream; fClearDirty: BOOL): HResult;
begin
result := E_NOTIMPL;
end;
{$ENDREGION}
{ TWebBrowserEx }
procedure TWebBrowserEx.Wait;
begin
WaitForBrowser(Self);
end;
function TWebBrowserEx.LoadHTML(const HTML: string; const AFakeURL: string=''): boolean;
begin
result := WebBrowserLoadHTML(Self, HTML, AFakeURL);
Self.Wait;
end;
function TWebBrowserEx.LoadStream(const Stream: TStream; const AFakeURL: string=''): boolean;
begin
result := WebBrowserLoadStream(Self, Stream, AFakeURL);
Self.Wait;
end;
procedure TWebBrowserEx.ReadMetaTags(outSL: TStringList);
var
vDocument: OleVariant;
vMetas: OleVariant;
vMetaItem: OleVariant;
i: Integer;
begin
vDocument := Self.Document;
vMetas := vDocument.GetElementsByTagName('meta');
for i := 0 to vMetas.Length-1 do
begin
vMetaItem := vMetas.Item(i);
if string(vMetaItem.httpequiv) = '' then
outSL.Values[vMetaItem.Name] := vMetaItem.Content
else
outSL.Values[vMetaItem.httpequiv] := vMetaItem.Content;
end;
end;
procedure TWebBrowserEx.Clear;
begin
Self.LoadHTML('', 'about:blank');
end;
end.