Skip to content

Commit d0142e8

Browse files
authored
fix(core): invoke key injection on regular JS scripts, closes #2342 (#2344)
1 parent 6e0dbf6 commit d0142e8

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-utils": patch
3+
"tauri": patch
4+
---
5+
6+
Inject the invoke key on regular `<script></script>` tags.

core/tauri-utils/src/html.rs

+42-17
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,54 @@ pub fn inject_invoke_key_token(document: &mut NodeRef) {
1919
let element = node.as_element().unwrap();
2020

2121
let attrs = element.attributes.borrow();
22-
// if the script is external (has `src`) or its type is not "module", we won't inject the token
23-
if attrs.get("src").is_some() || attrs.get("type") != Some("module") {
22+
// if the script is external (has `src`), we won't inject the token
23+
if attrs.get("src").is_some() {
2424
continue;
2525
}
2626

27-
let replacement_node = NodeRef::new_element(
28-
QualName::new(None, ns!(html), "script".into()),
29-
element
30-
.attributes
31-
.borrow()
32-
.clone()
33-
.map
34-
.into_iter()
35-
.collect::<Vec<_>>(),
36-
);
37-
let script = node.text_contents();
38-
replacement_node.append(NodeRef::new_text(format!(
39-
r#"
27+
let replacement_node = match attrs.get("type") {
28+
Some("module") | Some("application/ecmascript") => {
29+
let replacement_node = NodeRef::new_element(
30+
QualName::new(None, ns!(html), "script".into()),
31+
element
32+
.attributes
33+
.borrow()
34+
.clone()
35+
.map
36+
.into_iter()
37+
.collect::<Vec<_>>(),
38+
);
39+
let script = node.text_contents();
40+
replacement_node.append(NodeRef::new_text(format!(
41+
r#"
4042
const __TAURI_INVOKE_KEY__ = __TAURI__INVOKE_KEY_TOKEN__;
4143
{}
4244
"#,
43-
script
44-
)));
45+
script
46+
)));
47+
replacement_node
48+
}
49+
Some("application/javascript") | None => {
50+
let replacement_node = NodeRef::new_element(
51+
QualName::new(None, ns!(html), "script".into()),
52+
element
53+
.attributes
54+
.borrow()
55+
.clone()
56+
.map
57+
.into_iter()
58+
.collect::<Vec<_>>(),
59+
);
60+
let script = node.text_contents();
61+
replacement_node.append(NodeRef::new_text(
62+
script.replace("__TAURI_INVOKE_KEY__", "__TAURI__INVOKE_KEY_TOKEN__"),
63+
));
64+
replacement_node
65+
}
66+
_ => {
67+
continue;
68+
}
69+
};
4570

4671
node.insert_after(replacement_node);
4772
node.detach();

0 commit comments

Comments
 (0)