-
Notifications
You must be signed in to change notification settings - Fork 187
/
Quick.WebBrowser.pas
373 lines (331 loc) · 11.2 KB
/
Quick.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
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
{ ***************************************************************************
Copyright (c) 2014-2017 Kike Pérez
Unit : Quick.WebBrowser
Description : Web browser functions
Author : Kike Pérez
Version : 1.0
Created : 10/02/2014
Modified : 03/11/2016
This file is part of QuickLib: https://github.com/exilon/QuickLib
Uses code parts of: Thomas Stutz
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.WebBrowser;
interface
uses
Classes,
Forms,
System.SysUtils,
SHDocVw,
MSHTML,
ActiveX,
Vcl.Graphics,
System.Variants,
Winapi.WinInet;
procedure WB_SetBorderColor(Sender: TObject; BorderColor: String);
procedure WB_SetBorderStyle(Sender: TObject; BorderStyle: String);
procedure WB_Set3DBorderStyle(Sender: TObject; bValue: Boolean);
procedure WB_SetDessignMode(Sender : TObject; bEnabled : Boolean);
procedure WB_SetFontColor(Sender : TObject; aColor : TColor);
procedure WB_SetFontBold(Sender : TObject; bEnabled : Boolean);
procedure WB_SetFontItalic(Sender : TObject; bEnabled : Boolean);
procedure WB_SetFontUnderline(Sender : TObject; bEnabled : Boolean);
procedure WB_SetFontFace(Sender : TObject; cFontName : string);
procedure WB_SetFontSize(Sender : TObject; nFontSize : Integer);
procedure WB_InsertImage(Sender : TObject);
procedure WBLoadHTML(const WebBrowser: TWebBrowser; HTMLCode: string) ;
function GetHTML(const wbBrowser : TWebBrowser) : string;
function GetHTML2(const wbBrowser : TWebBrowser) : string;
function GetPlainText(const Html: string): string;
function GetWebBrowserHTML(const WebBrowser: TWebBrowser): String;
procedure DeleteIECacheAll;
procedure DeleteIECache(filenameWildcard : string);
implementation
procedure WB_SetBorderColor(Sender: TObject; BorderColor: String);
{
BorderColor: Can be specified in HTML pages in two ways.
1) by using a color name (red, green, gold, firebrick, ...)
2) or by using numbers to denote an RGB color value. (#9400D3, #00CED1,...)
See: http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/properties/borderstyle.asp
}
var
Document : IHTMLDocument2;
Element : IHTMLElement;
begin
Document := TWebBrowser(Sender).Document as IHTMLDocument2;
if Assigned(Document) then
begin
Element := Document.Body;
if Element <> nil then
begin
Element.Style.BorderColor := BorderColor;
end;
end;
end;
procedure WB_SetBorderStyle(Sender: TObject; BorderStyle: String);
{
BorderStyle values:
'none' No border is drawn
'dotted' Border is a dotted line. (as of IE 5.5)
'dashed' Border is a dashed line. (as of IE 5.5)
'solid' Border is a solid line.
'double' Border is a double line
'groove' 3-D groove is drawn //Está se ve perfecto en Windows 7 y Windows 8
'ridge' 3-D ridge is drawn
'inset' 3-D inset is drawn
'window-inset' Border is the same as inset, but is surrounded by an additional single line
'outset' 3-D outset is drawn
See: http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/properties/borderstyle.asp
}
var
Document : IHTMLDocument2;
Element : IHTMLElement;
begin
Document := TWebBrowser(Sender).Document as IHTMLDocument2;
if Assigned(Document) then
begin
Element := Document.Body;
if Element <> nil then
begin
Element.Style.BorderStyle := BorderStyle;
end;
end;
end;
procedure WB_Set3DBorderStyle(Sender: TObject; bValue: Boolean);
{
bValue: True: Show a 3D border style
False: Show no border
}
var
Document : IHTMLDocument2;
Element : IHTMLElement;
StrBorderStyle: string;
begin
Document := TWebBrowser(Sender).Document as IHTMLDocument2;
if Assigned(Document) then
begin
Element := Document.Body;
if Element <> nil then
begin
case BValue of
False: StrBorderStyle := 'none';
True: StrBorderStyle := '';
end;
Element.Style.BorderStyle := StrBorderStyle;
end;
end;
end;
procedure WB_SetDessignMode(Sender : TObject; bEnabled : Boolean);
begin
((Sender as TWebBrowser).Document as IHTMLDocument2).designMode := 'On';
end;
procedure WB_SetFontColor(Sender : TObject; aColor : TColor);
var
Document : IHTMLDocument2;
begin
Document := TWebBrowser(Sender).Document as IHTMLDocument2;
Document.execCommand('ForeColor',True,AColor);
end;
procedure WB_SetFontBold(Sender : TObject; bEnabled : Boolean);
var
Document : IHTMLDocument2;
begin
Document := TWebBrowser(Sender).Document as IHTMLDocument2;
Document.execCommand('Bold',False,bEnabled);
end;
procedure WB_SetFontItalic(Sender : TObject; bEnabled : Boolean);
var
Document : IHTMLDocument2;
begin
Document := TWebBrowser(Sender).Document as IHTMLDocument2;
Document.execCommand('Italic',False,bEnabled);
end;
procedure WB_SetFontUnderline(Sender : TObject; bEnabled : Boolean);
var
Document : IHTMLDocument2;
begin
Document := TWebBrowser(Sender).Document as IHTMLDocument2;
Document.execCommand('Underline',False,bEnabled);
end;
procedure WB_SetFontFace(Sender : TObject; cFontName : string);
var
Document : IHTMLDocument2;
begin
Document := TWebBrowser(Sender).Document as IHTMLDocument2;
Document.execCommand('FontName',False,cFontName);
end;
procedure WB_SetFontSize(Sender : TObject; nFontSize : Integer);
var
Document : IHTMLDocument2;
begin
Document := TWebBrowser(Sender).Document as IHTMLDocument2;
Document.execCommand('FontSize',False,nFontSize);
end;
procedure WB_InsertImage(Sender : TObject);
var
Document : IHTMLDocument2;
begin
Document := TWebBrowser(Sender).Document as IHTMLDocument2;
Document.execCommand('InsertImage',True,0);
end;
procedure WBLoadHTML(const WebBrowser: TWebBrowser; HTMLCode: string) ;
var
sl: TStringList;
ms: TMemoryStream;
begin
WebBrowser.Navigate('about:blank') ;
while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do
Application.ProcessMessages;
if Assigned(WebBrowser.Document) then
begin
sl := TStringList.Create;
try
ms := TMemoryStream.Create;
try
sl.Text := HTMLCode;
sl.SaveToStream(ms) ;
ms.Seek(0, 0) ;
(WebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms)) ;
finally
ms.Free;
end;
finally
sl.Free;
end;
end;
end;
function GetHTML(const wbBrowser : TWebBrowser) : string;
var
iall : IHTMLElement;
begin
(wbBrowser.Document as IHTMLDocument2).designMode := 'Off';
Result := (wbBrowser.Document as IHTMLDocument2).body.toString;
exit;
if Assigned(wbBrowser.Document) then
begin
iall := (wbBrowser.Document as IHTMLDocument2).body;
while iall.parentElement <> nil do
begin
iall := iall.parentElement;
end;
Result := iall.outerHTML;
end;
end;
function GetHTML2(const wbBrowser : TWebBrowser) : string;
var
Doc: IHTMLDocument2;
BodyElement: IHTMLElement;
begin
Assert(Assigned(wbBrowser.Document));
if wbBrowser.Document.QueryInterface(IHTMLDocument2, Doc) = S_OK then begin
BodyElement := Doc.body;
if Assigned(BodyElement) then
begin
result := '<html>' + BodyElement.outerHTML + '</html>';
end;
end;
end;
function GetWebBrowserHTML(const WebBrowser: TWebBrowser): String;
var
LStream: TStringStream;
Stream : IStream;
LPersistStreamInit : IPersistStreamInit;
begin
if not Assigned(WebBrowser.Document) then exit;
LStream := TStringStream.Create('');
try
LPersistStreamInit := WebBrowser.Document as IPersistStreamInit;
Stream := TStreamAdapter.Create(LStream,soReference);
LPersistStreamInit.Save(Stream,true);
result := LStream.DataString;
finally
LStream.Free();
end;
end;
function GetPlainText(const Html: string): string;
var
DummyWebBrowser: TWebBrowser;
Document : IHtmlDocument2;
DummyVar : Variant;
begin
Result := '';
DummyWebBrowser := TWebBrowser.Create(nil);
try
//open an blank page to create a IHtmlDocument2 instance
DummyWebBrowser.Navigate('about:blank');
Document := DummyWebBrowser.Document as IHtmlDocument2;
if (Assigned(Document)) then //Check the Document
begin
DummyVar := VarArrayCreate([0, 0], varVariant); //Create a variant array to write the html code to the IHtmlDocument2
DummyVar[0] := Html; //assign the html code to the variant array
Document.Write(PSafeArray(TVarData(DummyVar).VArray)); //set the html in the document
Document.Close;
Result :=(Document.body as IHTMLBodyElement).createTextRange.text;//get the plain text
end;
finally
DummyWebBrowser.Free;
end;
end;
procedure DeleteIECacheAll;
var
lpEntryInfo: PInternetCacheEntryInfo;
hCacheDir: LongWord;
dwEntrySize: LongWord;
begin
dwEntrySize := 0;
FindFirstUrlCacheEntry(nil, TInternetCacheEntryInfo(nil^), dwEntrySize);
GetMem(lpEntryInfo, dwEntrySize);
if dwEntrySize > 0 then lpEntryInfo^.dwStructSize := dwEntrySize;
hCacheDir := FindFirstUrlCacheEntry(nil, lpEntryInfo^, dwEntrySize);
if hCacheDir <> 0 then
begin
repeat
DeleteUrlCacheEntry(lpEntryInfo^.lpszSourceUrlName);
FreeMem(lpEntryInfo, dwEntrySize);
dwEntrySize := 0;
FindNextUrlCacheEntry(hCacheDir, TInternetCacheEntryInfo(nil^), dwEntrySize);
GetMem(lpEntryInfo, dwEntrySize);
if dwEntrySize > 0 then lpEntryInfo^.dwStructSize := dwEntrySize;
until not FindNextUrlCacheEntry(hCacheDir, lpEntryInfo^, dwEntrySize);
end;
FreeMem(lpEntryInfo, dwEntrySize);
FindCloseUrlCache(hCacheDir);
end;
//DeleteIECache('?M=P');
procedure DeleteIECache(filenameWildcard : string);
var
lpEntryInfo: PInternetCacheEntryInfo;
hCacheDir: LongWord;
dwEntrySize: LongWord;
begin
dwEntrySize := 0;
FindFirstUrlCacheEntry(nil, TInternetCacheEntryInfo(nil^), dwEntrySize) ;
GetMem(lpEntryInfo, dwEntrySize) ;
if dwEntrySize > 0 then lpEntryInfo^.dwStructSize := dwEntrySize;
hCacheDir := FindFirstUrlCacheEntry(nil, lpEntryInfo^, dwEntrySize) ;
if hCacheDir <> 0 then
begin
repeat
if Pos(filenameWildcard, lpEntryInfo^.lpszSourceUrlName) > 0 then begin
DeleteUrlCacheEntry(lpEntryInfo^.lpszSourceUrlName) ;
end;
FreeMem(lpEntryInfo, dwEntrySize) ;
dwEntrySize := 0;
FindNextUrlCacheEntry(hCacheDir, TInternetCacheEntryInfo(nil^), dwEntrySize) ;
GetMem(lpEntryInfo, dwEntrySize) ;
if dwEntrySize > 0 then lpEntryInfo^.dwStructSize := dwEntrySize;
until not FindNextUrlCacheEntry(hCacheDir, lpEntryInfo^, dwEntrySize) ;
end;
FreeMem(lpEntryInfo, dwEntrySize) ;
FindCloseUrlCache(hCacheDir) ;
end;
end.