Skip to content

Commit

Permalink
Add initializationScript (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
zephraph authored Sep 29, 2024
1 parent dc31e5c commit acc0571
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.0.16 (binary 0.1.13) -- 2024-09-29

- Add `initializationScript` to `WebViewOptions`. Allows providing JS that runs before `onLoad`.

## 0.0.15 (binary 0.1.12) -- 2024-09-28

- Pages loaded with `html` are now considered to be in a secure context.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "deno-webview"
version = "0.1.12"
version = "0.1.13"
edition = "2021"

[profile.release]
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@justbe/webview",
"exports": "./src/lib.ts",
"version": "0.0.15",
"version": "0.0.16",
"tasks": {
"dev": "deno run --watch main.ts",
"gen": "deno task gen:rust && deno task gen:deno",
Expand Down
2 changes: 2 additions & 0 deletions examples/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ using webview = await createWebView({
title: "Simple",
html: "<h1>Hello, World!</h1>",
devtools: true,
initializationScript:
"console.log('This is printed from initializationScript!')",
});

webview.on("started", async () => {
Expand Down
8 changes: 8 additions & 0 deletions schemas/WebViewOptions.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,7 @@ export type { WebViewOptions } from "./schemas.ts";

// Should match the cargo package version
/** The version of the webview binary that's expected */
export const BIN_VERSION = "0.1.12";

type JSON =
| string
| number
| boolean
| null
| JSON[]
| { [key: string]: JSON };
export const BIN_VERSION = "0.1.13";

type WebViewNotification = Extract<
WebViewMessage,
Expand Down
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ struct WebViewOptions {
/// Sets whether host should be able to receive messages from the webview via `window.ipc.postMessage`.
#[serde(default)]
ipc: bool,
#[serde(default)]
/// Run JavaScript code when loading new pages. When the webview loads a new page, this code will be executed. It is guaranteed that the code is executed before window.onload.
initialization_script: Option<String>,
}

fn default_true() -> bool {
Expand Down Expand Up @@ -339,6 +342,10 @@ fn main() -> wry::Result<()> {
.unwrap()
})
}
if let Some(initialization_script) = webview_options.initialization_script {
webview_builder =
webview_builder.with_initialization_script(initialization_script.as_str());
}
let webview = webview_builder.build()?;

let notify_tx = tx.clone();
Expand Down
3 changes: 3 additions & 0 deletions src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export type WebViewOptions =
* Platform-specific: - Windows: Requires WebView2 Runtime version 101.0.1210.39 or higher, does nothing on older versions, see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10121039
*/
incognito?: boolean;
/** Run JavaScript code when loading new pages. When the webview loads a new page, this code will be executed. It is guaranteed that the code is executed before window.onload. */
initializationScript?: string;
/** Sets whether host should be able to receive messages from the webview via `window.ipc.postMessage`. */
ipc?: boolean;
/** The size of the window. */
Expand Down Expand Up @@ -65,6 +67,7 @@ export const WebViewOptions: z.ZodType<WebViewOptions> = z.intersection(
devtools: z.boolean().optional(),
focused: z.boolean().optional(),
incognito: z.boolean().optional(),
initializationScript: z.string().optional(),
ipc: z.boolean().optional(),
size: z.union([
z.literal("maximized"),
Expand Down

0 comments on commit acc0571

Please sign in to comment.