@@ -20,24 +20,20 @@ The following code snippets demonstrate how the ExecuteScriptWithResult can be u
2020// Tools function to generate the script code
2121// Using std::wstringstream to generate script code,
2222// it will generate the code like
23- // 'let str_0 = "This is a string" ; let n_0= str_0 .replace(/a/i , "microsoft "); n_0 ;'
23+ // '(() => { let str = "abc" ; let n = str .replace("b" , "d "); return n; })() ;'
2424std::wstring GenerateScriptCode (LPCWSTR str, LPCWSTR reg, LPCWSTR item)
2525{
2626 if (str == nullptr || reg == nullptr || item == nullptr)
2727 {
2828 return L"";
2929 }
3030
31- // This variable is used to ensure that the
32- // variables in the script are unique.
33- static int idx = 0;
34-
3531 static std::wstringstream sw;
3632 sw.clear();
3733
38- sw << L"let str_" << idx << L" = \"" << str << L"\"; let n_" << idx << L"= str_" << idx
39- << L".replace(" << reg << L", \"" << item << L"\"); n_" << idx << L" ;";
40- ++idx;
34+ sw << L"(() => { let str = \"" << str << L"\"; let n = str.replace("
35+ << reg << L", \"" << item << L"\"); return n; })() ;";
36+
4137 return sw.str();
4238}
4339
@@ -97,7 +93,7 @@ void MatchRegWithScript(wil::com_ptr<ICoreWebView2> webView
9793 // We will use MessageBox to print exception-related information
9894 else
9995 {
100- wil::com_ptr<ICoreWebView2ExecuteScriptException > exception;
96+ wil::com_ptr<ICoreWebView2ScriptException > exception;
10197
10298 result->get_Exception(&exception);
10399
@@ -139,16 +135,12 @@ void MatchRegWithScript(wil::com_ptr<ICoreWebView2> webView
139135```c#
140136class ExecuteScriptWithResultDemo
141137{
142- int idx = 0;
143-
144138 private String GenerateScriptCode(String str, String reg, String item)
145139 {
146- String ret = $"let str_{idx} = \"{str}\"; let n_{idx} = str_{idx}.replace({reg}, \"{item}\"); n_{idx};";
147- ++idx;
140+ String ret = $"(() => {{ let str = \"{str}\"; let n = str.replace({reg}, \"{item}\"); return n; }})();";
148141 return ret;
149142 }
150143
151-
152144 // This is a demo that uses regular expressions in
153145 // JavaScript to complete string replacement, it will handle
154146 // the case of successful execution and execution exception
@@ -195,7 +187,7 @@ The spec file for `ExecuteScript` is [WebView2Feedback/specs/ExecuteScript.md](h
195187// / If the CoreWebView2.ExecuteScriptWithResult result has Succeeded as false,
196188// / you can use the result's Exception property to get the script exception.
197189[uuid(82F22B72-1B22-403E-A0B9-A8816C9C8E45), object, pointer_default(unique)]
198- interface ICoreWebView2ExecuteScriptException : IUnknown {
190+ interface ICoreWebView2ScriptException : IUnknown {
199191
200192 /// The line number of the source where the exception occurred.
201193 /// In the JSON it is ` exceptionDetail.lineNumber ` .
@@ -259,7 +251,7 @@ interface ICoreWebView2ExecuteScriptResult : IUnknown {
259251 /// S_OK will be returned even if the acquisition fails.
260252 /// We can determine whether the acquisition is successful by judging whether the ` exception ` is nullptr.
261253 [ propget] HRESULT Exception(
262- [ out, retval] ICoreWebView2ExecuteScriptException ** exception);
254+ [ out, retval] ICoreWebView2ScriptException ** exception);
263255}
264256
265257// / This is the callback for ExecuteScriptWithResult
@@ -299,7 +291,7 @@ namespace Microsoft.Web.WebView2.Core
299291{
300292 runtimeclass CoreWebView2;
301293 runtimeclass CoreWebView2ExecuteScriptResult;
302- runtimeclass CoreWebView2ExecuteScriptException ;
294+ runtimeclass CoreWebView2ScriptException ;
303295
304296 runtimeclass CoreWebView2
305297 {
@@ -317,12 +309,12 @@ namespace Microsoft.Web.WebView2.Core
317309 // You can refer to the demo and documentation of `ExecuteScript`.
318310 String ResultAsJson { get ; };
319311
320- CoreWebView2ExecuteScriptException Exception { get ; };
312+ CoreWebView2ScriptException Exception { get ; };
321313
322314 Int32 TryGetResultAsString (out String stringResult );
323315 }
324316
325- runtimeclass CoreWebView2ExecuteScriptException
317+ runtimeclass CoreWebView2ScriptException
326318 {
327319 UInt32 LineNumber { get ; };
328320
0 commit comments