TMS TAdvWebBrowser Developers Guide
TMS TAdvWebBrowser Developers Guide
TMS TAdvWebBrowser
DEVELOPERS GUIDE
TMS TAdvWebBrowser
DEVELOPERS GUIDE
June 2023
Copyright © 2020 - 2023 by tmssoftware.com bvba
Web: https://www.tmssoftware.com
Email: info@tmssoftware.com
1
TMS SOFTWARE
TMS TAdvWebBrowser
DEVELOPERS GUIDE
Index
Getting Started ........................................................................................................................................ 3
Installation ............................................................................................................................................... 3
Navigating to a URL ................................................................................................................................. 4
Loading HTML .......................................................................................................................................... 4
Loading files ............................................................................................................................................. 4
Capture Screenshot ................................................................................................................................. 4
Communicating with the application through a JavaScript bridge ......................................................... 4
Executing JavaScript ................................................................................................................................ 5
Using Events ............................................................................................................................................ 6
Manipulating the Context Menu ............................................................................................................. 6
Properties ................................................................................................................................................ 8
Settings ................................................................................................................................................ 8
Methods .................................................................................................................................................. 9
Events .................................................................................................................................................... 10
2
TMS SOFTWARE
TMS TAdvWebBrowser
DEVELOPERS GUIDE
Getting Started
Included in the TMS VCL UI Pack is a WebBrowser that can display web pages, HTML and load files
such as PDF files. The WebBrowser also allows executing scripts and catch the result in a callback.
To get started with the WebBrowser, add the AdvWebBrowser unit. The WebBrowser class is called
TAdvWebBrowser. TAdvWebBrowser supports Edge Chromium. Please follow the instructions below
to correct install Edge Chromium on your Windows operating system.
Installation
TAdvWebBrowser is part of the TMS VCL UI Pack and supports Delphi or C++Builder XE7 and newer
versions. Before the TAdvWebBrowser can be used there are a couple of things that need to be
done. Below are the steps to take when you want to use the TAdvWebBrowser and enable Edge
Chromium on your Windows operating system.
1) Windows 10 with automatic updates enabled has already Edge Chromium included that is
used by TAdvWebBrowser.
If you do not have automatic updates enabled or use an older version of Windows, install
Edge Chromium from the following page: https://www.microsoft.com/en-us/edge
We have tested the installation against v85.0.534.0. Earlier versions are not supported.
Newer version updates need to be tested first, as each update might potentially break your
application. Please before installing, check the version number and ask us for an update in
case you are having troubles getting the browser to run. Microsoft will also push out Edge
Chromium through Windows Updates.
3) Start the IDE and drop an instance of TAdvWebBrowser on the form. The border around the
webbrowser at designtime is for moving/selecting it. The blue box indicating the Edge
Chromium is initialized, is interactable and is a live browser instance. You should see the
following when the browser is successfully initialized:
3
TMS SOFTWARE
TMS TAdvWebBrowser
DEVELOPERS GUIDE
Navigating to a URL
After creating an instance of the TAdvWebBrowser, navigating to a web page is as simple as using
the code below.
procedure TForm1.Browse;
begin
AdvWebBrowser1.URL := ‘https://www.tmssoftware.com’;
end;
or
procedure TForm1.Browse;
begin
AdvWebBrowser1.Navigate(‘https://www.tmssoftware.com’);
end;
It is also possible to navigate with a custom method and data with the NavigateWithData function.
Loading HTML
Loading fully functional HTML/JavaScript can be done with the following code:
procedure TForm1.LoadFromHTML;
begin
AdvWebBrowser1.LoadHTML(‘<b>This is HTML!</b>’);
end;
Loading files
Files such as PDF files, images, HTML files and many more can be loaded with the LoadFile method:
procedure TForm1.LoadFromFile;
begin
AdvWebBrowser1.LoadFile(‘MyPDF.pdf’);
end;
Capture Screenshot
Communication with the application is possible through registration of a JavaScript bridge object.
The object needs to conform certain number of parameters to function properly. The definition of
the bridge object is shown below:
4
TMS SOFTWARE
TMS TAdvWebBrowser
DEVELOPERS GUIDE
private
FObjectMessage: string;
function GetObjectMessage: string;
procedure SetObjectMessage(const Value: string);
published
property ObjectMessage: string read GetObjectMessage write SetObjectMessage;
end;
Notice that the ObjectMessage property is set to published, so internal RTTI can pick up the
property and use this to communicate with the application. The IAdvCustomWebBrowserBridge
interface is used to make sure the object is picked up in mobile environments, as the
communication process is slightly different there. The JavaScript part that is required in HTML is
shown below.
sHTML :=
'<html>' + #13 +
' <head>' + #13 +
' <script>' + #13 +
w.GetBridgeCommunicationLayer(BridgeName) +
' </script>' + #13 +
' </head>' + #13 +
' <body>' + #13 +
' <button onclick="send' + BridgeName + 'ObjectMessage(''Hello World!'');">Click Me!</button>' +
#13 +
' </body>' + #13 +
'</html>';
w.Parent := Self;
o := TMyBridgeObject.Create;
w.AddBridge(BridgeName, o);
w.LoadHTML(sHTML);
end;
First, we need to create a webbrowser instance, create our bridge object and pass it to the
webbrowser. The ObjectMessage propery naming is important and needs to remain the same. The
HTML code snippet contains the helper function to setup communication between browser and
application. As seen in the onclick event of the button, the function is called with a string value as
a parameter. Communication between application and browser always happens with a string value.
Executing JavaScript
Executing JavaScript is supported and can also be used in combination with a return value callback.
Below is a sample code snippet that shows how to execute JavaScript and get a return value.
5
TMS SOFTWARE
TMS TAdvWebBrowser
DEVELOPERS GUIDE
Using Events
It is possible to change the context menu that is shown in the web browser.
• It is also possible to have this `TPopupMenu` rendered as a native context menu by enabling
the UsePopupMenuAsContextMenu property in `Settings`. The assigned events or actions
when a click happens, will still be triggered.
• You can fully customize the native context menu with the use of the event
OnGetContextMenu. This event is triggered when the context menu is rendered. It has a
target item TTMSFNCWebBrowserTargetItem that shows whether it is shown on a Page,
Image, Selected Text, Audio or Video.
And you can check if there is a link or selected text attached to it. Based on this you can
now choose which items to show in the context menu. In this event there is a list of
TTMSFNCWebBrowserContextMenuItem. You will notice that it is not possible to get any
information, because the system generated items are protected. Only custom items can be
changed. To help you with this the AsSystem and AsCustom functions are available. You
can delete system items from the list. But the position can't be changed.
TTMSFNCWebBrowserCustomContextMenuItem items can be created and inserted in the
list.
6
TMS SOFTWARE
TMS TAdvWebBrowser
DEVELOPERS GUIDE
//Make sure the customer can't reload the page from the context menu
while I < AContextMenu.Count do
begin
if AContextMenu[I].AsSystem.Name.ToLower <> 'reload' then
AContextMenu.Delete(I);
Inc(I);
end;
mi := TTMSFNCWebBrowserCustomContextMenuItem.Create;
mi.Name := 'Go to TMS';
AContextMenu.Insert(0, mi);
end;
7
TMS SOFTWARE
TMS TAdvWebBrowser
DEVELOPERS GUIDE
Properties
Property Description
name
Downloads List of all the downloads that were starting since initialization.
Settings
EnableAcceleratorKeys Boolean that indicates whether all accelerator keys that access
features specific to a web browser are enabled.
8
TMS SOFTWARE
TMS TAdvWebBrowser
DEVELOPERS GUIDE
Methods
NavigateWithData(AURI: string; AMethod: string; Navigates to an URI with a custom method and
ABody: string; AHeaders: TStrings) data as text.
9
TMS SOFTWARE
TMS TAdvWebBrowser
DEVELOPERS GUIDE
NavigateWithData(AURI: string; AMethod: string; Navigates to an URI with a custom method and
ABodyStream: TStream; AHeaders: TStrings) data as a stream.
Events
OnCustomContextMenuItemSelected Returns
a TTMSFNCWebBrowserCustomContextMenuItem when a
custom item was selected in the context menu.
10
TMS SOFTWARE
TMS TAdvWebBrowser
DEVELOPERS GUIDE
11