javascript - 从 WebView 调用 Deno 函数。如何?

标签 javascript webview deno

我在网上找到了这个关于如何运行 WebView 的非常酷的示例与德诺。
是否可以从放置在 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();
要运行此代码,您需要:
deno run -A --unstable webview.ts

最佳答案

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

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

关于javascript - 从 WebView 调用 Deno 函数。如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63657464/

相关文章:

javascript - Angularjs 如何在每个页面显示导航和页脚?

javascript - 将 bootstrap.css 包含到 nuxt 项目的最佳方法是什么?

android - 如何在 Webview Android 中执行按钮单击

deno - 我如何开始使用 deno.land?

typescript - 使用 Deno 连接到 Redis 数据库

javascript - jQuery AJAX 帮助

javascript - 如果在单词中找到相同的字符串,则停止 .replace() 替换该字符串

javascript - 如何通过 WebView 将来自 javascript 的 Alert() 操作接收到 Cocoa 应用程序

android - WebView 返回

deno - 类型 'URL' 不可分配给类型 'string'