Skip to main content
Module

x/webview/mod.ts>Webview#bind

🌐 Deno bindings for webview, a tiny library for creating web-based desktop GUIs
Go to Latest
method Webview.prototype.bind
import { Webview } from "https://deno.land/x/webview@0.7.5/mod.ts";

Binds a callback so that it will appear in the webview with the given name as a global async JavaScript function. Callback arguments are automatically converted from json to as closely as possible match the arguments in the webview context and the callback automatically converts and returns the return value to the webview.

Parameters

name: string

The name of the bound function

callback: (...args: any) => any

A callback which is passed the arguments as called from the webview JavaScript environment and optionally returns a value to the webview JavaScript caller

Example

import { Webview } from "../mod.ts";

const html = `
<html>
<body>
<h1>Hello from deno v${Deno.version.deno}</h1>
<button onclick="press('I was pressed!', 123, new Date()).then(log);">
Press me!
</button>
</body>
</html>
`;

const webview = new Webview();

webview.navigate(`data:text/html,${encodeURIComponent(html)}`);

let counter = 0;
// Create and bind `press` to the webview javascript instance.
// This functions in addition to logging its parameters also returns
// a value from deno land to webview land.
webview.bind("press", (a, b, c) => {
console.log(a, b, c);

return { times: counter++ };
});

// Bind the `log` function in the webview to the parent instances `console.log`
webview.bind("log", (...args) => console.log(...args));

webview.run();