First off all, to install TEdgeBrowser there are three steps:
- Install the EdgeView2 SDK from GetIt.
- Download and install the Microsoft EdgeView2 Runtime appropriate for your architecture
- It comes in 3 varieties
- Evergreen Bootstraper – Downloads and installs the latest version of the runtime matching the current architecture. Use this to install with your application.
- Evergreen Standalone Installer – Offline installer for Win32 or Win64
- Fixed Version – Allows you to download and install previous versions instead of the latest one offered by the Evergreen installers.
- If you are building a Win32 application, then install the x86 version. If you are building a Win64 application, then install the x64 version. Or install both!
- It comes in 3 varieties
- At runtime your program needs to access to WebView2Loader.dll, the easiest way to do this is either place it on the path, or use a Post Build event
- For Win32: copy /y “$(BDS)Redistwin32WebView2Loader.dll” “$(OUTPUTDIR)”
- For Win64: copy /y “$(BDS)Redistwin64WebView2Loader.dll” “$(OUTPUTDIR)”
When deploying your application you need to include the correct
WebView2Loader.dll and make sure the Microsoft EdgeView2 Runtime is installed. At some point that will be installed by default, but the easiest way is use the Evergreen Bootstrapper.
Table of Contents
Displaying Arbitrary HTML
To load arbitrary HTML into the TEdgeView, call the NavigateToString method passing the HTML as a string.
EdgeBrowser1.NavigateToString('<html><body><h1>Hello From Delphi</h1></body></html>');
And your HTML will immediately render in the browser. No need to save it to a file or other such silliness.
View The Source
The fundamentals of viewing the source of a page start with the ExecuteScript method, but are a bit more involved. There is an event handler for when a your script finishes executing since it executes asynchronously. You can use that even to return a JSON Object. In this case, the JSON Object is the HTML Source.
This uses the DOM (Document Object Model) to grab the Document Element (the root element, or <HTML>) and request the outerHTML, which is all the HTML including the Document Element. We then use encodeURL to encode it. Now for the OnExecuteScriptevent handler
procedure TEdgeViewForm.EdgeBrowser1ExecuteScript(Sender: TCustomEdgeBrowser;
AResult: HRESULT; const AResultObjectAsJson: string);
if AResultObjectAsJson <> 'null' then
memoHTML.Text := TNetEncoding.URL.Decode(AResultObjectAsJson).DeQuotedString('"');
This event fires after every call to ExecuteScript, but the AResultObjectAsJsonwill be 'null' if no results are returns. So we just ignore the ‘null’ values. Otherwise we use the TNetEncoding.URL.Decode to remove the encoding, and remove the quotes with DeQuotedString('"').
You can take the results of this and send right back in with a call to NavigateToString().
TEdgeBrowser is just one of the great new and improved features in 10.4.2 Sydney. If you haven’t upgraded you are missing out!