Skip to content

Commit f283f3b

Browse files
authored
Update ExecuteScriptWithResult.md
Merge two demos in one, fix some variables names.
1 parent 568c0d3 commit f283f3b

File tree

1 file changed

+53
-132
lines changed

1 file changed

+53
-132
lines changed

specs/ExecuteScriptWithResult.md

Lines changed: 53 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ The following code snippets demonstrate how the ExecuteScriptWithResult can be u
2323
// 'let str_0 = "This is a string"; let n_0= str_0.replace(/a/i, "microsoft"); n_0;'
2424
static std::wstring GenerateScriptCode(LPCWSTR str, LPCWSTR reg, LPCWSTR item)
2525
{
26-
if (str == nullptr || reg == nullptr)
26+
if (str == nullptr || reg == nullptr || item == nullptr)
2727
{
28-
return nullptr;
28+
return L"";
2929
}
3030

3131
// This variable is used to ensure that the
@@ -41,18 +41,22 @@ static std::wstring GenerateScriptCode(LPCWSTR str, LPCWSTR reg, LPCWSTR item)
4141
return sw.str();
4242
}
4343

44-
// The first case is that the script is executed successfully,
45-
// and we successfully get the replaced string.
46-
void MatchRegWithScript_1(wil::com_ptr<ICoreWebView2> m_webView)
44+
// This is a demo that uses regular expressions in
45+
// javascript to complete string replacement, it will handle
46+
// the case of successful execution and execution exception
47+
void MatchRegWithScript(wil::com_ptr<ICoreWebView2> webView
48+
, LPCWSTR str
49+
, LPCWSTR reg
50+
, LPCWSTR item)
4751
{
48-
wil::com_ptr<ICoreWebView2Staging2> webview2 = m_webView.try_query<ICoreWebView2Staging2>();
52+
wil::com_ptr<ICoreWebView2_10> webview2 = webView.try_query<ICoreWebView2_10>();
4953

50-
auto scriptCode = GenerateScriptCode(L"This is a string", L"\"This\"", L"It");
54+
auto scriptCode = GenerateScriptCode(str, reg, item);
5155
webview2->ExecuteScriptWithResult(
5256
scriptCode.c_str(),
53-
Callback<ICoreWebView2StagingExecuteScriptWithResultCompletedHandler>(
57+
Callback<ICoreWebView2ExecuteScriptWithResultCompletedHandler>(
5458
[](
55-
HRESULT errorCode, ICoreWebView2StagingExecuteScriptResult* result) -> HRESULT
59+
HRESULT errorCode, ICoreWebView2ExecuteScriptResult* result) -> HRESULT
5660
{
5761
if (errorCode != S_OK || result == nullptr)
5862
{
@@ -62,13 +66,13 @@ void MatchRegWithScript_1(wil::com_ptr<ICoreWebView2> m_webView)
6266
else
6367
{
6468
wil::unique_cotaskmem_string stringData;
65-
wil::unique_cotaskmem_string rawJsonData;
6669

67-
BOOL is_success;
68-
if (result->get_IsSuccess(&is_success) == S_OK && is_success)
70+
BOOL isSuccess;
71+
result->get_IsSuccess(&isSuccess);
72+
// Here is the successful execution.
73+
// We will use a MessageBox to print the replaced result.
74+
if (isSuccess)
6975
{
70-
result->get_ResultAsJson(&rawJsonData);
71-
7276
// We try to use TryGetResultAsString to get the string result here.
7377
// Since the javascript platform's `string.replace` returns a string,
7478
// the call here will succeed.
@@ -81,94 +85,43 @@ void MatchRegWithScript_1(wil::com_ptr<ICoreWebView2> m_webView)
8185
}
8286
else
8387
{
84-
MessageBox(nullptr, rawJsonData.get(), L"ExecuteScript Result",
85-
MB_OK);
8688
MessageBox(nullptr, stringData.get(), L"ExecuteScript Result",
8789
MB_OK);
8890
}
8991
}
90-
}
91-
return S_OK;
92-
})
93-
.Get());
94-
}
95-
96-
97-
// The second case is when the execution throws an exception due to a regex error,
98-
// we can catch and handle it via `get_Exception` in c++.
99-
void MatchRegWithScript_2(wil::com_ptr<ICoreWebView2> m_webView)
100-
{
101-
wil::com_ptr<ICoreWebView2Staging2> webview2 = m_webView.try_query<ICoreWebView2Staging2>();
102-
103-
auto scriptCode = GenerateScriptCode(L"This is a string", L"/a", L"It");
104-
webview2->ExecuteScriptWithResult(
105-
scriptCode.c_str(),
106-
Callback<ICoreWebView2StagingExecuteScriptWithResultCompletedHandler>(
107-
[](
108-
HRESULT errorCode, ICoreWebView2StagingExecuteScriptResult* result) -> HRESULT
109-
{
110-
if (errorCode != S_OK || result == nullptr)
111-
{
112-
MessageBox(
113-
nullptr, L"Get execute status failed!", L"ExecuteScript Result", MB_OK);
114-
}
115-
else
116-
{
117-
wil::com_ptr<ICoreWebView2StagingExecuteScriptException> exception;
118-
BOOL is_success;
119-
// The is_success will return false since an incorrect regular expression
120-
// was passed in.
121-
if (result->get_IsSuccess(&is_success) == S_OK && !is_success)
92+
// Here is the case of execution exception.
93+
// We will use MessageBox to print exception-related information
94+
else
12295
{
96+
wil::com_ptr<ICoreWebView2ExecuteScriptException> exception;
97+
12398
result->get_Exception(&exception);
12499

125-
// Get the exception name, this could return the empty string,
100+
// The ExceptionName property could be the empty string if script throws a non-Error object,
126101
// such as `throw 1`.
127-
// In this case, we will get 'SyntaxError'.
128102
wil::unique_cotaskmem_string exceptionName;
129-
if (exception && exception->get_Name(&exceptionName) == S_OK)
130-
{
131-
MessageBox(
132-
nullptr, exceptionName.get(), L"ExecuteScript Result", MB_OK);
133-
}
103+
exception->get_Name(&exceptionName);
134104

135-
// Get the exception message, this could return the empty
136-
// string, such as `throw 1`.
137-
// In this case, we will get 'SyntaxError: Invalid regular expression:
138-
// missing /'.
105+
// The ExceptionMessage property could be the empty string if script throws a non-Error object,
106+
// such as `throw 1`.
139107
wil::unique_cotaskmem_string exceptionMessage;
140-
if (exception && exception->get_Message(&exceptionMessage) == S_OK)
141-
{
142-
MessageBox(
143-
nullptr, exceptionMessage.get(), L"ExecuteScript Result",
144-
MB_OK);
145-
}
146-
147-
// Get the exception detail, it's a json struct data with all
148-
// exception infomation , we can parse it and get the detail
149-
// what we need.
150-
wil::unique_cotaskmem_string exceptionDetail;
151-
if (exception &&
152-
exception->get_ExceptionAsJSON(&exceptionDetail) == S_OK)
153-
{
154-
MessageBox(
155-
nullptr, exceptionDetail.get(), L"ExecuteScript Result", MB_OK);
156-
}
157-
108+
exception->get_Message(&exceptionMessage)
158109

159110
// Get the location of the exception, note that the coordinates
160111
// here are 0 as the starting position.
161-
// In this case, we will get the coordinate: `LineNumber:0, ColumnNumber:55`.
162112
uint32_t lineNumber = 0;
163113
uint32_t columnNumber = 0;
164-
if (exception && exception->get_LineNumber(&lineNumber) == S_OK &&
165-
exception->get_ColumnNumber(&columnNumber) == S_OK)
166-
{
167-
auto exception_location_info =
168-
L"LineNumber:" + std::to_wstring(lineNumber) +
114+
exception->get_LineNumber(&lineNumber);
115+
exception->get_ColumnNumber(&columnNumber);
116+
117+
auto exceptionInfo =
118+
L"The script execution failed." +
119+
L"\nName: " + exceptionName.get() +
120+
L"\nMessage: " + exceptionMessage.get() +
121+
L"\nLineNumber: " + std::to_wstring(lineNumber) +
169122
L", ColumnNumber:" + std::to_wstring(columnNumber);
170123
MessageBox(
171-
nullptr, exception_location_info.c_str(),
124+
nullptr, exceptionInfo.c_str(),
172125
L"ExecuteScript Result", MB_OK);
173126
}
174127
}
@@ -192,21 +145,19 @@ class ExecuteScriptWithResultDemo {
192145
}
193146
194147
195-
// The first case is that the script is executed successfully,
196-
// and we successfully get the replaced string.
197-
public void MatchRegWithScript_1() {
148+
// This is a demo that uses regular expressions in
149+
// javascript to complete string replacement, it will handle
150+
// the case of successful execution and execution exception
151+
public void MatchRegWithScript(String str, String reg, String item) {
198152
var environment = webView2Control.CoreWebView2.Environment;
199-
String script = GenerateScriptCode("This is a string", "\"This\"", "It");
153+
String script = GenerateScriptCode(str, reg, item);
200154
CoreWebView2ExecuteScriptResult result = await ExecuteScriptWithResultAsync(script);
201155
202156
bool isSuccess = result.IsSuccess;
203-
// It will return true, and we will get the replaced string.
204-
if (isSuccess) {
205-
Debug.WriteLine($"raw json data for replaced string: {result.ResultAsJson}");
206-
157+
// Here is the successful execution.
158+
if (isSuccess) {
207159
// Try to get the string result, it will throw an exception
208160
// if the result type isn't string type.
209-
210161
try {
211162
String stringResult = result.TryGetResultAsString();
212163
Debug.WriteLine($"replaced string: {result.stringResult}");
@@ -215,45 +166,16 @@ class ExecuteScriptWithResultDemo {
215166
Debug.WriteLine($"Non-string message received");
216167
}
217168
}
218-
}
219-
220-
221-
// The second case is when the execution throws an exception due to a regex error,
222-
// we can catch and handle it via `Exception` in c++.
223-
public void MatchRegWithScript_2() {
224-
var environment = webView2Control.CoreWebView2.Environment;
225-
String script = GenerateScriptCode("This is a string", "/a", "It");
226-
CoreWebView2ExecuteScriptResult result = await ExecuteScriptWithResultAsync(script);
227-
228-
bool isSuccess = result.IsSuccess;
229-
// It will return false, then we can get the its exception
230-
// to handle the error in javascrpt
231-
if (!isSuccess) {
169+
// Here is the case of execution exception.
170+
else
171+
{
232172
var exception = result.Exception;
233-
234-
// Get the exception name, this could return the empty string,
235-
// such as `throw 1`.
236-
// In this case, we will get 'SyntaxError'.
237-
Debug.WriteLine($"exception name: {exception.Name}");
238-
239-
// Get the exception message, this could return the empty
240-
// string, such as `throw 1`.
241-
// In this case, we will get 'SyntaxError: Invalid regular expression:
242-
// missing /'.
243-
Debug.WriteLine($"exception message: {exception.Message}");
244-
245-
246-
// Get the exception detail, it's a json struct data with all
247-
// exception infomation, we can parse it and get the detail
248-
// what we need.
249-
Debug.WriteLine($"exception detail: {exception.ExceptionAsJSON}");
250-
251-
// Get the location of the exception, note that the coordinates
252-
// here are 0 as the starting position.
253-
// In this case, we will get the coordinate: `LineNumber:0, ColumnNumber:55`.
254-
String coordinate = "LineNumber:" + exception.LineNumber +
173+
String exceptionInfo = "The script execution failed." +
174+
"\nName:" + exception.Name +
175+
"\nMesssge: " + exception.Message +
176+
"\n LineNumber:" + exception.LineNumber +
255177
", ColumnNumber:" + exception.ColumnNumber;
256-
Debug.WriteLine($"exception coordinate: {coordinate}");
178+
Debug.WriteLine($"{exceptionInfo}");
257179
}
258180
}
259181
}
@@ -266,7 +188,7 @@ class ExecuteScriptWithResultDemo {
266188
/// If the CoreWebView2.ExecuteScriptWithResult result has IsSuccessful as false,
267189
/// you can use the result's Exception property to get the script exception.
268190
[uuid(82F22B72-1B22-403E-A0B9-A8816C9C8E45), object, pointer_default(unique)]
269-
interface ICoreWebView2ScriptException : IUnknown {
191+
interface ICoreWebView2ExecuteScriptException : IUnknown {
270192

271193
/// The line number of the source where the exception occurred.
272194
/// In the JSON it is `exceptionDetail.lineNumber`.
@@ -384,4 +306,3 @@ namespace Microsoft.Web.WebView2.Core
384306
}
385307
}
386308
```
387-

0 commit comments

Comments
 (0)