2
0

Quick.WebBrowser.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. { ***************************************************************************
  2. Copyright (c) 2014-2017 Kike Pérez
  3. Unit : Quick.WebBrowser
  4. Description : Web browser functions
  5. Author : Kike Pérez
  6. Version : 1.0
  7. Created : 10/02/2014
  8. Modified : 03/11/2016
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. Uses code parts of: Thomas Stutz
  11. ***************************************************************************
  12. Licensed under the Apache License, Version 2.0 (the "License");
  13. you may not use this file except in compliance with the License.
  14. You may obtain a copy of the License at
  15. http://www.apache.org/licenses/LICENSE-2.0
  16. Unless required by applicable law or agreed to in writing, software
  17. distributed under the License is distributed on an "AS IS" BASIS,
  18. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. See the License for the specific language governing permissions and
  20. limitations under the License.
  21. *************************************************************************** }
  22. unit Quick.WebBrowser;
  23. interface
  24. uses
  25. Classes,
  26. Forms,
  27. System.SysUtils,
  28. SHDocVw,
  29. MSHTML,
  30. ActiveX,
  31. Vcl.Graphics,
  32. System.Variants,
  33. Winapi.WinInet;
  34. procedure WB_SetBorderColor(Sender: TObject; BorderColor: String);
  35. procedure WB_SetBorderStyle(Sender: TObject; BorderStyle: String);
  36. procedure WB_Set3DBorderStyle(Sender: TObject; bValue: Boolean);
  37. procedure WB_SetDessignMode(Sender : TObject; bEnabled : Boolean);
  38. procedure WB_SetFontColor(Sender : TObject; aColor : TColor);
  39. procedure WB_SetFontBold(Sender : TObject; bEnabled : Boolean);
  40. procedure WB_SetFontItalic(Sender : TObject; bEnabled : Boolean);
  41. procedure WB_SetFontUnderline(Sender : TObject; bEnabled : Boolean);
  42. procedure WB_SetFontFace(Sender : TObject; cFontName : string);
  43. procedure WB_SetFontSize(Sender : TObject; nFontSize : Integer);
  44. procedure WB_InsertImage(Sender : TObject);
  45. procedure WBLoadHTML(const WebBrowser: TWebBrowser; HTMLCode: string) ;
  46. function GetHTML(const wbBrowser : TWebBrowser) : string;
  47. function GetHTML2(const wbBrowser : TWebBrowser) : string;
  48. function GetPlainText(const Html: string): string;
  49. function GetWebBrowserHTML(const WebBrowser: TWebBrowser): String;
  50. procedure DeleteIECacheAll;
  51. procedure DeleteIECache(filenameWildcard : string);
  52. implementation
  53. procedure WB_SetBorderColor(Sender: TObject; BorderColor: String);
  54. {
  55. BorderColor: Can be specified in HTML pages in two ways.
  56. 1) by using a color name (red, green, gold, firebrick, ...)
  57. 2) or by using numbers to denote an RGB color value. (#9400D3, #00CED1,...)
  58. See: http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/properties/borderstyle.asp
  59. }
  60. var
  61. Document : IHTMLDocument2;
  62. Element : IHTMLElement;
  63. begin
  64. Document := TWebBrowser(Sender).Document as IHTMLDocument2;
  65. if Assigned(Document) then
  66. begin
  67. Element := Document.Body;
  68. if Element <> nil then
  69. begin
  70. Element.Style.BorderColor := BorderColor;
  71. end;
  72. end;
  73. end;
  74. procedure WB_SetBorderStyle(Sender: TObject; BorderStyle: String);
  75. {
  76. BorderStyle values:
  77. 'none' No border is drawn
  78. 'dotted' Border is a dotted line. (as of IE 5.5)
  79. 'dashed' Border is a dashed line. (as of IE 5.5)
  80. 'solid' Border is a solid line.
  81. 'double' Border is a double line
  82. 'groove' 3-D groove is drawn //Está se ve perfecto en Windows 7 y Windows 8
  83. 'ridge' 3-D ridge is drawn
  84. 'inset' 3-D inset is drawn
  85. 'window-inset' Border is the same as inset, but is surrounded by an additional single line
  86. 'outset' 3-D outset is drawn
  87. See: http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/properties/borderstyle.asp
  88. }
  89. var
  90. Document : IHTMLDocument2;
  91. Element : IHTMLElement;
  92. begin
  93. Document := TWebBrowser(Sender).Document as IHTMLDocument2;
  94. if Assigned(Document) then
  95. begin
  96. Element := Document.Body;
  97. if Element <> nil then
  98. begin
  99. Element.Style.BorderStyle := BorderStyle;
  100. end;
  101. end;
  102. end;
  103. procedure WB_Set3DBorderStyle(Sender: TObject; bValue: Boolean);
  104. {
  105. bValue: True: Show a 3D border style
  106. False: Show no border
  107. }
  108. var
  109. Document : IHTMLDocument2;
  110. Element : IHTMLElement;
  111. StrBorderStyle: string;
  112. begin
  113. Document := TWebBrowser(Sender).Document as IHTMLDocument2;
  114. if Assigned(Document) then
  115. begin
  116. Element := Document.Body;
  117. if Element <> nil then
  118. begin
  119. case BValue of
  120. False: StrBorderStyle := 'none';
  121. True: StrBorderStyle := '';
  122. end;
  123. Element.Style.BorderStyle := StrBorderStyle;
  124. end;
  125. end;
  126. end;
  127. procedure WB_SetDessignMode(Sender : TObject; bEnabled : Boolean);
  128. begin
  129. ((Sender as TWebBrowser).Document as IHTMLDocument2).designMode := 'On';
  130. end;
  131. procedure WB_SetFontColor(Sender : TObject; aColor : TColor);
  132. var
  133. Document : IHTMLDocument2;
  134. begin
  135. Document := TWebBrowser(Sender).Document as IHTMLDocument2;
  136. Document.execCommand('ForeColor',True,AColor);
  137. end;
  138. procedure WB_SetFontBold(Sender : TObject; bEnabled : Boolean);
  139. var
  140. Document : IHTMLDocument2;
  141. begin
  142. Document := TWebBrowser(Sender).Document as IHTMLDocument2;
  143. Document.execCommand('Bold',False,bEnabled);
  144. end;
  145. procedure WB_SetFontItalic(Sender : TObject; bEnabled : Boolean);
  146. var
  147. Document : IHTMLDocument2;
  148. begin
  149. Document := TWebBrowser(Sender).Document as IHTMLDocument2;
  150. Document.execCommand('Italic',False,bEnabled);
  151. end;
  152. procedure WB_SetFontUnderline(Sender : TObject; bEnabled : Boolean);
  153. var
  154. Document : IHTMLDocument2;
  155. begin
  156. Document := TWebBrowser(Sender).Document as IHTMLDocument2;
  157. Document.execCommand('Underline',False,bEnabled);
  158. end;
  159. procedure WB_SetFontFace(Sender : TObject; cFontName : string);
  160. var
  161. Document : IHTMLDocument2;
  162. begin
  163. Document := TWebBrowser(Sender).Document as IHTMLDocument2;
  164. Document.execCommand('FontName',False,cFontName);
  165. end;
  166. procedure WB_SetFontSize(Sender : TObject; nFontSize : Integer);
  167. var
  168. Document : IHTMLDocument2;
  169. begin
  170. Document := TWebBrowser(Sender).Document as IHTMLDocument2;
  171. Document.execCommand('FontSize',False,nFontSize);
  172. end;
  173. procedure WB_InsertImage(Sender : TObject);
  174. var
  175. Document : IHTMLDocument2;
  176. begin
  177. Document := TWebBrowser(Sender).Document as IHTMLDocument2;
  178. Document.execCommand('InsertImage',True,0);
  179. end;
  180. procedure WBLoadHTML(const WebBrowser: TWebBrowser; HTMLCode: string) ;
  181. var
  182. sl: TStringList;
  183. ms: TMemoryStream;
  184. begin
  185. WebBrowser.Navigate('about:blank') ;
  186. while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do
  187. Application.ProcessMessages;
  188. if Assigned(WebBrowser.Document) then
  189. begin
  190. sl := TStringList.Create;
  191. try
  192. ms := TMemoryStream.Create;
  193. try
  194. sl.Text := HTMLCode;
  195. sl.SaveToStream(ms) ;
  196. ms.Seek(0, 0) ;
  197. (WebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms)) ;
  198. finally
  199. ms.Free;
  200. end;
  201. finally
  202. sl.Free;
  203. end;
  204. end;
  205. end;
  206. function GetHTML(const wbBrowser : TWebBrowser) : string;
  207. var
  208. iall : IHTMLElement;
  209. begin
  210. (wbBrowser.Document as IHTMLDocument2).designMode := 'Off';
  211. Result := (wbBrowser.Document as IHTMLDocument2).body.toString;
  212. exit;
  213. if Assigned(wbBrowser.Document) then
  214. begin
  215. iall := (wbBrowser.Document as IHTMLDocument2).body;
  216. while iall.parentElement <> nil do
  217. begin
  218. iall := iall.parentElement;
  219. end;
  220. Result := iall.outerHTML;
  221. end;
  222. end;
  223. function GetHTML2(const wbBrowser : TWebBrowser) : string;
  224. var
  225. Doc: IHTMLDocument2;
  226. BodyElement: IHTMLElement;
  227. begin
  228. Assert(Assigned(wbBrowser.Document));
  229. if wbBrowser.Document.QueryInterface(IHTMLDocument2, Doc) = S_OK then begin
  230. BodyElement := Doc.body;
  231. if Assigned(BodyElement) then
  232. begin
  233. result := '<html>' + BodyElement.outerHTML + '</html>';
  234. end;
  235. end;
  236. end;
  237. function GetWebBrowserHTML(const WebBrowser: TWebBrowser): String;
  238. var
  239. LStream: TStringStream;
  240. Stream : IStream;
  241. LPersistStreamInit : IPersistStreamInit;
  242. begin
  243. if not Assigned(WebBrowser.Document) then exit;
  244. LStream := TStringStream.Create('');
  245. try
  246. LPersistStreamInit := WebBrowser.Document as IPersistStreamInit;
  247. Stream := TStreamAdapter.Create(LStream,soReference);
  248. LPersistStreamInit.Save(Stream,true);
  249. result := LStream.DataString;
  250. finally
  251. LStream.Free();
  252. end;
  253. end;
  254. function GetPlainText(const Html: string): string;
  255. var
  256. DummyWebBrowser: TWebBrowser;
  257. Document : IHtmlDocument2;
  258. DummyVar : Variant;
  259. begin
  260. Result := '';
  261. DummyWebBrowser := TWebBrowser.Create(nil);
  262. try
  263. //open an blank page to create a IHtmlDocument2 instance
  264. DummyWebBrowser.Navigate('about:blank');
  265. Document := DummyWebBrowser.Document as IHtmlDocument2;
  266. if (Assigned(Document)) then //Check the Document
  267. begin
  268. DummyVar := VarArrayCreate([0, 0], varVariant); //Create a variant array to write the html code to the IHtmlDocument2
  269. DummyVar[0] := Html; //assign the html code to the variant array
  270. Document.Write(PSafeArray(TVarData(DummyVar).VArray)); //set the html in the document
  271. Document.Close;
  272. Result :=(Document.body as IHTMLBodyElement).createTextRange.text;//get the plain text
  273. end;
  274. finally
  275. DummyWebBrowser.Free;
  276. end;
  277. end;
  278. procedure DeleteIECacheAll;
  279. var
  280. lpEntryInfo: PInternetCacheEntryInfo;
  281. hCacheDir: LongWord;
  282. dwEntrySize: LongWord;
  283. begin
  284. dwEntrySize := 0;
  285. FindFirstUrlCacheEntry(nil, TInternetCacheEntryInfo(nil^), dwEntrySize);
  286. GetMem(lpEntryInfo, dwEntrySize);
  287. if dwEntrySize > 0 then lpEntryInfo^.dwStructSize := dwEntrySize;
  288. hCacheDir := FindFirstUrlCacheEntry(nil, lpEntryInfo^, dwEntrySize);
  289. if hCacheDir <> 0 then
  290. begin
  291. repeat
  292. DeleteUrlCacheEntry(lpEntryInfo^.lpszSourceUrlName);
  293. FreeMem(lpEntryInfo, dwEntrySize);
  294. dwEntrySize := 0;
  295. FindNextUrlCacheEntry(hCacheDir, TInternetCacheEntryInfo(nil^), dwEntrySize);
  296. GetMem(lpEntryInfo, dwEntrySize);
  297. if dwEntrySize > 0 then lpEntryInfo^.dwStructSize := dwEntrySize;
  298. until not FindNextUrlCacheEntry(hCacheDir, lpEntryInfo^, dwEntrySize);
  299. end;
  300. FreeMem(lpEntryInfo, dwEntrySize);
  301. FindCloseUrlCache(hCacheDir);
  302. end;
  303. //DeleteIECache('?M=P');
  304. procedure DeleteIECache(filenameWildcard : string);
  305. var
  306. lpEntryInfo: PInternetCacheEntryInfo;
  307. hCacheDir: LongWord;
  308. dwEntrySize: LongWord;
  309. begin
  310. dwEntrySize := 0;
  311. FindFirstUrlCacheEntry(nil, TInternetCacheEntryInfo(nil^), dwEntrySize) ;
  312. GetMem(lpEntryInfo, dwEntrySize) ;
  313. if dwEntrySize > 0 then lpEntryInfo^.dwStructSize := dwEntrySize;
  314. hCacheDir := FindFirstUrlCacheEntry(nil, lpEntryInfo^, dwEntrySize) ;
  315. if hCacheDir <> 0 then
  316. begin
  317. repeat
  318. if Pos(filenameWildcard, lpEntryInfo^.lpszSourceUrlName) > 0 then begin
  319. DeleteUrlCacheEntry(lpEntryInfo^.lpszSourceUrlName) ;
  320. end;
  321. FreeMem(lpEntryInfo, dwEntrySize) ;
  322. dwEntrySize := 0;
  323. FindNextUrlCacheEntry(hCacheDir, TInternetCacheEntryInfo(nil^), dwEntrySize) ;
  324. GetMem(lpEntryInfo, dwEntrySize) ;
  325. if dwEntrySize > 0 then lpEntryInfo^.dwStructSize := dwEntrySize;
  326. until not FindNextUrlCacheEntry(hCacheDir, lpEntryInfo^, dwEntrySize) ;
  327. end;
  328. FreeMem(lpEntryInfo, dwEntrySize) ;
  329. FindCloseUrlCache(hCacheDir) ;
  330. end;
  331. end.