Calling Deno function from WebView. How?

Viewed 360

I have found this really cool example online on how to run a WebView with Deno.

Is it possible to call a function insided your Deno App from an HTML button element placed inside the html template?

Take a look:

// Importing the webview library
import { WebView } from "https://deno.land/x/webview/mod.ts";
// Creating an HTML page
let html = `
  <html>
    <body>
        <h1>Hello from deno v${Deno.version.deno}</h1>
        <button type="button" onclick="test()">RUN TEST FUNCTION</button>
    </body>
  </html>
`;

function test() {

  console.log('You really can do that!');

}

// Creating and configuring the webview
const webview = new WebView({
  title: "Deno Webview Example",
  url: "data:text/html," + html,
  // url: 'https://www.google.com',
  width: 768,
  height: 1024,
  resizable: true,
  debug: true,
  frameless: false
});
// Running the webview
webview.run();

To run this code you need to:

deno run -A --unstable webview.ts
1 Answers

Use WebView.bind().

Making the example in this question work might be as simple as (test() will become a global function, as click handler expects):

@@ -27,5 +27,8 @@ const webview = new WebView({
   debug: true,
   frameless: false
 });
+
+webview.bind('test', test);
+
 // Running the webview
 webview.run();
Related