Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions crates/plugin-webview/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ initial navigation. Use `custom_protocol_async`/`WebviewProtocolResponder` for a
See the [plugin development standard](../../docs/plugin-development-standard.md) for type names,
reverse events, threading, mounting and acceptance rules.

## Document-start scripts

Document-start scripts use `runJavaScriptOnDocumentStart` on API 15 and newer.
Each array entry remains a separate script and runs in array order, so an exception
in one entry does not prevent the following entries from running. On API 12–14,
the existing `javaScriptOnDocumentStart` component API is retained; it does not
guarantee execution order. Both direct and drag-overlay WebViews retain the
scripts supplied by `keySynthesisScripts`, including the main-window keyboard shim.

## License

Choose either [MIT](LICENSE-MIT) or [Apache-2.0](LICENSE-APACHE).
22 changes: 20 additions & 2 deletions plugins/webview/src/main/ets/WebviewPlugin.ets
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ function enableWebDebuggingIfAvailable(): void {
setWebDebuggingAccessIfAvailable(true);
}

/** Resolve the API-15 Web attribute dynamically to retain the API-12 baseline. */
interface OrderedDocumentStartAttribute {
runJavaScriptOnDocumentStart?: (scripts: ScriptItem[]) => void;
}

class DocumentStartScriptsModifier implements AttributeModifier<WebAttribute> {
constructor(private scripts: ScriptItem[]) {}

applyNormalAttribute(instance: WebAttribute): void {
const ordered = instance as ESObject as OrderedDocumentStartAttribute;
if (typeof ordered.runJavaScriptOnDocumentStart === "function") {
ordered.runJavaScriptOnDocumentStart(this.scripts);
} else {
instance.javaScriptOnDocumentStart(this.scripts);
}
}
}

interface WebviewStyle {
x?: number | string | null;
y?: number | string | null;
Expand Down Expand Up @@ -1335,7 +1353,7 @@ function BuildWebview(data: ManagedWebview) {
.javaScriptAccess(data.javascriptEnabled ?? true)
.domStorageAccess(data.domStorageAccess ?? true)
.mediaPlayGestureAccess(data.autoplay === true ? false : true)
.javaScriptOnDocumentStart(keySynthesisScripts(data))
.attributeModifier(new DocumentStartScriptsModifier(keySynthesisScripts(data)))
.multiWindowAccess(data.eventOptions.newWindow === true)
.allowWindowOpenMethod(data.eventOptions.newWindow === true)
.onControllerAttached(() => {
Expand Down Expand Up @@ -1409,7 +1427,7 @@ function BuildWebview(data: ManagedWebview) {
.javaScriptAccess(data.javascriptEnabled ?? true)
.domStorageAccess(data.domStorageAccess ?? true)
.mediaPlayGestureAccess(data.autoplay === true ? false : true)
.javaScriptOnDocumentStart(keySynthesisScripts(data))
.attributeModifier(new DocumentStartScriptsModifier(keySynthesisScripts(data)))
.multiWindowAccess(data.eventOptions.newWindow === true)
.allowWindowOpenMethod(data.eventOptions.newWindow === true)
.onControllerAttached(() => {
Expand Down