Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TEdgeBrowser component: calling native code from a script running on the embedded web page

Currently we use TWebBrowser component to embed IE to our desktop application. To call a Delphi code from a script running in the embedded browser we have implemented support for the window.external object as described here: http://www.delphidabbler.com/articles/article-22

Now, for many reasons, we need to switch to a modern browser. We already upgraded to Delphi 10.4 which introduces new TEdgeBrowser component (MS Edge based on Chromium). Is it possible to use the window.external also for TEdgeBrowser? If so, how? Or is there other way how to call native code from a script in the embedded browser?

like image 470
stepand76 Avatar asked Jun 15 '20 06:06

stepand76


People also ask

How do I use the tedgebrowser component?

You can use the TEdgeBrowser component in a similar way you use a TWebBrowser, because a number of TEdgeBrowser's methods and properties are much the same to those of TWebBrowser ’s methods and properties. Drop the component on a VCL form and size it as appropriate. To navigate to a URL simply pass that URL to the Navigate method.

How does the embedded JavaScript program call the WebAssembly Host_Inc() function?

The embedded JavaScript program calls the host_inc () function. The JavaScript interpreter (the Rust program for host_function.wasm) routes this call to a WebAssembly host_inc () call. The customized WasmEdge runtime (the C program for demo_wasmedge) routes the WebAssembly call to a native C function.

What is twebbrowser tselectedengine?

TWebBrowser.TSelectedEngine.IEOnly: the TWebBrowser functions as it always has, employing the IE WebBrowser control. TWebBrowser.TSelectedEngine.EdgeIfAvailable: the TWebBrowser uses the Edge WebView2 browser control if possible, otherwise it falls back to the traditional IE WebBrowser control.

What is webview2 web/native Interop?

See also in Introduction to Microsoft Edge WebView2. Web/Native Interop in Overview of WebView2 features and APIs. Executes JavaScript code from the javaScript parameter in the current top level document rendered in the WebView. Equivalent to calling ExecuteScriptAsync (String).


1 Answers

Finally it was pretty simple. Thanks to TOndrej for the "Getting started" link which help me to figure it out. I also realized that it works with MS Edge Beta (84.0.522.28), so no Canary required as described by Marco Cantu here: https://blog.marcocantu.com/blog/2020-may-edge-browser-component.html. I hope it will work with official MS Edge soon. Here are some code snippets:

Delphi:

procedure TForm1.Button1Click(Sender: TObject);
begin
  EdgeBrowser1.Navigate(ExtractFilePath(ParamStr(0))  + 'index.html');
end;

procedure TForm1.EdgeBrowser1WebMessageReceived(Sender: TCustomEdgeBrowser; Args: TWebMessageReceivedEventArgs);
var
  Msg: PChar;
begin
  Args.ArgsInterface.Get_webMessageAsJson(Msg);
  MessageBox(Handle, Msg, PChar(EmptyStr), MB_OK);
end;

HTML:

<!DOCTYPE html>
<html>
<body>
    <p onclick="handleClick()">Click me</p>
    <script>
        function handleClick() {
            window.chrome.webview.postMessage({ data: 'Message from Edge Chromium', url: window.document.URL });
        }
    </script>
</body>
</html> 

enter image description here

like image 93
stepand76 Avatar answered Sep 28 '22 03:09

stepand76