从 WebView 调用 Deno 函数。如何?

ASP*_*iRE 6 javascript webview deno

我在网上发现了这个非常酷的示例,介绍了如何使用WebViewDeno 运行。

是否可以从放置在 html 模板内的 HTML 按钮元素调用 Deno 应用程序内的函数?

看一看:

// 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();
Run Code Online (Sandbox Code Playgroud)

要运行此代码,您需要:

deno run -A --unstable webview.ts
Run Code Online (Sandbox Code Playgroud)

Ant*_*off 0

使用WebView.bind()

使这个问题中的示例工作可能很简单(test()将成为一个全局函数,如单击处理程序所期望的那样):

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