Skip to content
Merged
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
2 changes: 1 addition & 1 deletion crates/plugin-webview/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ async fn composed_webview(app: &OpenHarmonyApp) -> Result<()> {
}
```

`WebviewCreateRequest` 支持 URL/HTML、`parent_node` 组合、样式、JavaScript 开关、devtools、
`WebviewCreateRequest` 支持 URL/HTML、`parent_node` 组合、样式、JavaScript 开关、DOM storage、devtools、
user agent、autoplay、document-start initialization scripts、headers 和 `transparent`。
`transparent(true)` 是创建时语义:若没有显式 background color,ArkTS 使用透明背景;显式颜色优先。

Expand Down
12 changes: 12 additions & 0 deletions crates/plugin-webview/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ pub struct WebviewCreateRequest {
pub html: Option<String>,
pub style: WebviewStyle,
pub javascript_enabled: Option<bool>,
/// Enables ArkWeb DOM storage (localStorage/sessionStorage). ArkWeb disables it by
/// default; when unset, DOM storage stays disabled, so callers opt in with `true`.
pub dom_storage_access: Option<bool>,
pub devtools: Option<bool>,
pub user_agent: Option<String>,
pub autoplay: Option<bool>,
Expand All @@ -312,6 +315,7 @@ impl WebviewCreateRequest {
html: None,
style: WebviewStyle::default(),
javascript_enabled: None,
dom_storage_access: None,
devtools: None,
user_agent: None,
autoplay: None,
Expand Down Expand Up @@ -350,6 +354,12 @@ impl WebviewCreateRequest {
self
}

/// Enables ArkWeb DOM storage (localStorage/sessionStorage).
pub fn dom_storage_access(mut self, enabled: bool) -> Self {
self.dom_storage_access = Some(enabled);
self
}

pub fn initialization_scripts(
mut self,
scripts: impl IntoIterator<Item = WebviewInitializationScript>,
Expand Down Expand Up @@ -914,13 +924,15 @@ mod tests {
let request = WebviewCreateRequest::new("webview")
.parent_node(7)
.transparent(true)
.dom_storage_access(true)
.url("https://example.test");
assert_eq!(request.id, "webview");
assert_eq!(request.parent_handle, Some(7));
assert_eq!(request.url.as_deref(), Some("https://example.test"));
assert!(request.html.is_none());
assert!(request.headers.is_none());
assert_eq!(request.transparent, Some(true));
assert_eq!(request.dom_storage_access, Some(true));
}

#[test]
Expand Down
5 changes: 5 additions & 0 deletions demo/entry/src/main/cpp/types/libdemo_native/Index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export interface WebviewCreateRequest {
html?: string;
style: WebviewStyle;
javascriptEnabled?: boolean;
/**
* Enables ArkWeb DOM storage (localStorage/sessionStorage). ArkWeb disables it by
* default; when unset, DOM storage stays disabled, so callers opt in with `true`.
*/
domStorageAccess?: boolean;
devtools?: boolean;
userAgent?: string;
autoplay?: boolean;
Expand Down
5 changes: 5 additions & 0 deletions demo/entry/src/main/cpp/types/libdemo_sub_native/Index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export interface WebviewCreateRequest {
html?: string;
style: WebviewStyle;
javascriptEnabled?: boolean;
/**
* Enables ArkWeb DOM storage (localStorage/sessionStorage). ArkWeb disables it by
* default; when unset, DOM storage stays disabled, so callers opt in with `true`.
*/
domStorageAccess?: boolean;
devtools?: boolean;
userAgent?: string;
autoplay?: boolean;
Expand Down
3 changes: 3 additions & 0 deletions plugins/webview/src/main/ets/WebviewPlugin.ets
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ interface WebviewCreatePayload {
html?: string | null;
style?: WebviewStyle | null;
javascriptEnabled?: boolean | null;
domStorageAccess?: boolean | null;
devtools?: boolean | null;
userAgent?: string | null;
autoplay?: boolean | null;
Expand Down Expand Up @@ -742,6 +743,7 @@ function BuildWebview(data: ManagedWebview) {
.backgroundColor(data.style.backgroundColor ?? undefined)
.visibility(data.style.visible === false ? Visibility.Hidden : Visibility.Visible)
.javaScriptAccess(data.javascriptEnabled ?? true)
.domStorageAccess(data.domStorageAccess ?? false)
.mediaPlayGestureAccess(data.autoplay === true ? false : true)
.javaScriptOnDocumentStart(data.initializationScripts ?? [])
.onControllerAttached(() => {
Expand Down Expand Up @@ -808,6 +810,7 @@ class WebviewSurface {
url: payload.url,
html: payload.html,
javascriptEnabled: payload.javascriptEnabled,
domStorageAccess: payload.domStorageAccess,
devtools: payload.devtools,
userAgent: payload.userAgent,
autoplay: payload.autoplay,
Expand Down
Loading