Module

x/webview/src/webview.ts>Webview

🌐 Deno bindings for webview, a tiny library for creating web-based desktop GUIs
Latest
class Webview
import { Webview } from "https://dotland.deno.dev/x/webview@0.8.1/src/webview.ts";

An instance of a webview window.

Examples

Local

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

const html = `
  <html>
  <body>
    <h1>Hello from deno v${Deno.version.deno}</h1>
  </body>
  </html>
`;

const webview = new Webview();

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

Remote

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

const webview = new Webview();
webview.navigate("https://deno.land/");
webview.run();

Constructors

new
Webview(handle: Deno.PointerValue)

UNSAFE: Highly unsafe API, beware!

Creates a new webview instance from a webview handle.

new
Webview(
debug?: boolean,
size?: Size,
window?: Deno.PointerValue | null,
)

Creates a new webview instance.

Example

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

// Create a new webview and change from the default size to a small fixed window
const webview = new Webview(true, {
  width: 200,
  height: 200,
  hint: SizeHint.FIXED
});

webview.navigate("https://deno.land/");
webview.run();
new
Webview(
debugOrHandle?: boolean | Deno.PointerValue,
size?: Size | undefined,
window?: Deno.PointerValue | null,
)

Properties

writeonly
size: Size

Sets the native window size

Example

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

const webview = new Webview();
webview.navigate("https://deno.land/");

// Change from the default size to a small fixed window
webview.size = {
  width: 200,
  height: 200,
  hint: SizeHint.FIXED
};

webview.run();
writeonly
title: string

Sets the native window title

Example

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

const webview = new Webview();
webview.navigate("https://deno.land/");

// Set the window title to "Hello world!"
webview.title = "Hello world!";

webview.run();
readonly
unsafeHandle: Deno.PointerValue

UNSAFE: Highly unsafe API, beware!

An unsafe pointer to the webview

readonly
unsafeWindowHandle: Deno.PointerValue

UNSAFE: Highly unsafe API, beware!

An unsafe pointer to the webviews platform specific native window handle. When using GTK backend the pointer is GtkWindow pointer, when using Cocoa backend the pointer is NSWindow pointer, when using Win32 backend the pointer is HWND pointer.

Methods

bind(name: string, callback: (...args: any) => any)

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.

bindRaw(
name: string,
callback: (
seq: string,
req: string,
arg: Deno.PointerValue | null,
) => void
,
arg?: Deno.PointerValue | null,
)

Binds a callback so that it will appear in the webview with the given name as a global async JavaScript function. Callback receives a seq and req value. The seq parameter is an identifier for using {@link Webview.return} to return a value while the req parameter is a string of an JSON array representing the arguments passed from the JavaScript function call.

Destroys the webview and closes the window along with freeing all internal resources.

eval(source: string)

Evaluates arbitrary JavaScript code. Evaluation happens asynchronously, also the result of the expression is ignored. Use {@link Webview.bind | bindings} if you want to receive notifications about the results of the evaluation.

init(source: string)

Injects JavaScript code at the initialization of the new page. Every time the webview will open a the new page - this initialization code will be executed. It is guaranteed that code is executed before window.onload.

navigate(url: URL | string)

Navigates webview to the given URL. URL may be a data URI, i.e. "data:text/html,<html>...</html>". It is often ok not to url-encodeCString it properly, webview will re-encodeCString it for you.

return(
seq: string,
status: number,
result: string,
)

Returns a value to the webview JavaScript environment.

run(): void

Runs the main event loop until it's terminated. After this function exits the webview is automatically destroyed.

unbind(name: string)

Unbinds a previously bound function freeing its resource and removing it from the webview JavaScript context.