Skip to content

Commit 20c1906

Browse files
feat: add WebviewBuilder.disable_javascript and WebviewWindowBuilder.disable_javascript (#12821)
* feat: add `WebviewBuilder.disable_javascript` and `WebviewWindowBuilder.disable_javascript` * wry 0.50.3 * add missing config options and API types * add change file for api --------- Co-authored-by: Lucas Nogueira <[email protected]>
1 parent 3fb8d7c commit 20c1906

File tree

14 files changed

+72
-4
lines changed

14 files changed

+72
-4
lines changed

.changes/disable-javascript-api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@tauri-apps/api": minor:feat
3+
---
4+
5+
Added `WindowOptions::javascriptDisabled` and `WebviewOptions::javascriptDisabled`.
6+

.changes/disable-javascript.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
tauri: 'minor:feat'
3+
tauri-runtime-wry: 'minor:feat'
4+
---
5+
6+
Add `WebviewBuilder.disable_javascript` and `WebviewWindowBuilder.disable_javascript` api to disable JavaScript.

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/tauri-cli/config.schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,11 @@
527527
"type": "null"
528528
}
529529
]
530+
},
531+
"javascriptDisabled": {
532+
"description": "Whether we should disable JavaScript code execution on the webview or not.",
533+
"default": false,
534+
"type": "boolean"
530535
}
531536
},
532537
"additionalProperties": false

crates/tauri-runtime-wry/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ rustc-args = ["--cfg", "docsrs"]
1717
rustdoc-args = ["--cfg", "docsrs"]
1818

1919
[dependencies]
20-
wry = { version = "0.50.0", default-features = false, features = [
20+
wry = { version = "0.50.3", default-features = false, features = [
2121
"drag-drop",
2222
"protocol",
2323
"os-webview",

crates/tauri-runtime-wry/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4211,6 +4211,10 @@ fn create_webview<T: UserEvent>(
42114211
});
42124212
}
42134213

4214+
if webview_attributes.javascript_disabled {
4215+
webview_builder = webview_builder.with_javascript_disabled();
4216+
}
4217+
42144218
if let Some(color) = webview_attributes.background_color {
42154219
webview_builder = webview_builder.with_background_color(color.into());
42164220
}

crates/tauri-runtime/src/webview.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ pub struct WebviewAttributes {
218218
pub devtools: Option<bool>,
219219
pub background_color: Option<Color>,
220220
pub background_throttling: Option<BackgroundThrottlingPolicy>,
221+
pub javascript_disabled: bool,
221222
}
222223

223224
impl From<&WindowConfig> for WebviewAttributes {
@@ -254,6 +255,7 @@ impl From<&WindowConfig> for WebviewAttributes {
254255
if let Some(color) = config.background_color {
255256
builder = builder.background_color(color);
256257
}
258+
builder.javascript_disabled = config.javascript_disabled;
257259
builder
258260
}
259261
}
@@ -285,6 +287,7 @@ impl WebviewAttributes {
285287
devtools: None,
286288
background_color: None,
287289
background_throttling: None,
290+
javascript_disabled: false,
288291
}
289292
}
290293

crates/tauri-schema-generator/schemas/config.schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,11 @@
527527
"type": "null"
528528
}
529529
]
530+
},
531+
"javascriptDisabled": {
532+
"description": "Whether we should disable JavaScript code execution on the webview or not.",
533+
"default": false,
534+
"type": "boolean"
530535
}
531536
},
532537
"additionalProperties": false

crates/tauri-utils/src/config.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,9 @@ pub struct WindowConfig {
17171717
/// see https://github.com/tauri-apps/tauri/issues/5250#issuecomment-2569380578
17181718
#[serde(default, alias = "background-throttling")]
17191719
pub background_throttling: Option<BackgroundThrottlingPolicy>,
1720+
/// Whether we should disable JavaScript code execution on the webview or not.
1721+
#[serde(default, alias = "javascript-disabled")]
1722+
pub javascript_disabled: bool,
17201723
}
17211724

17221725
impl Default for WindowConfig {
@@ -1770,6 +1773,7 @@ impl Default for WindowConfig {
17701773
devtools: None,
17711774
background_color: None,
17721775
background_throttling: None,
1776+
javascript_disabled: false,
17731777
}
17741778
}
17751779
}
@@ -3047,6 +3051,7 @@ mod build {
30473051
let devtools = opt_lit(self.devtools.as_ref());
30483052
let background_color = opt_lit(self.background_color.as_ref());
30493053
let background_throttling = opt_lit(self.background_throttling.as_ref());
3054+
let javascript_disabled = self.javascript_disabled;
30503055

30513056
literal_struct!(
30523057
tokens,
@@ -3098,7 +3103,8 @@ mod build {
30983103
use_https_scheme,
30993104
devtools,
31003105
background_color,
3101-
background_throttling
3106+
background_throttling,
3107+
javascript_disabled
31023108
);
31033109
}
31043110
}

crates/tauri/src/webview/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,17 @@ fn main() {
891891
self.webview_attributes.background_throttling = Some(policy);
892892
self
893893
}
894+
895+
/// Whether JavaScript should be disabled.
896+
///
897+
/// ## Platform-specific
898+
///
899+
/// - **Android:** Not implemented yet.
900+
#[must_use]
901+
pub fn disable_javascript(mut self) -> Self {
902+
self.webview_attributes.javascript_disabled = true;
903+
self
904+
}
894905
}
895906

896907
/// Webview.

0 commit comments

Comments
 (0)