-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebView2Extensions.cs
More file actions
36 lines (31 loc) · 1.38 KB
/
Copy pathWebView2Extensions.cs
File metadata and controls
36 lines (31 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Threading.Tasks;
using Microsoft.Web.WebView2.Core;
using Newtonsoft.Json;
namespace WebToolsDataMonitor
{
public static class WebView2Extensions
{
public static Task<string> ExecuteScriptWithArgsAsync(this CoreWebView2 coreWebView2, string script, object[] args)
{
// Cleanly serialize the arguments to a plain JSON array string
string jsonArgs = JsonConvert.SerializeObject(args);
// 🔥 FIXED: Swapped out 'async function' for a standard synchronous execution block.
// This guarantees the return value is evaluated as a string instantly rather than a Promise object.
string executionWrapper = $@"
(function() {{
try {{
const args = {jsonArgs};
// Execute the tracking script logic immediately
const result = (function() {{
{script.Trim()}
}})();
return result !== undefined ? String(result) : 'EXECUTION_SUCCESS_VOID';
}} catch (err) {{
return 'JS_EXECUTION_ERROR: ' + err.message;
}}
}})();";
return coreWebView2.ExecuteScriptAsync(executionWrapper);
}
}
}