Skip to content

Commit e88dbbc

Browse files
authored
Update ExecuteScriptWithResult.md
1 parent d5e8850 commit e88dbbc

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

specs/ExecuteScriptWithResult.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,13 @@ void MatchRegWithScript(wil::com_ptr<ICoreWebView2> webView
4949
, LPCWSTR reg
5050
, LPCWSTR item)
5151
{
52+
wil::com_ptr<ICoreWebView2_10> webview2 = webView.try_query<ICoreWebView2_10>();
5253
if (!webview2)
5354
{
5455
// ExecuteScriptWithResult is not supported by this WebView.
5556
return;
5657
}
5758

58-
wil::com_ptr<ICoreWebView2_10> webview2 = webView.try_query<ICoreWebView2_10>();
59-
6059
auto scriptCode = GenerateScriptCode(str, reg, item);
6160
webview2->ExecuteScriptWithResult(
6261
scriptCode.c_str(),
@@ -76,12 +75,14 @@ void MatchRegWithScript(wil::com_ptr<ICoreWebView2> webView
7675
// We will use a MessageBox to print the replaced result.
7776
if (isSuccess)
7877
{
79-
// We try to use TryGetResultAsString to get the string result here.
78+
// We try to use `TryGetResultAsString` to get the string result here.
79+
// We can check the `isString` to get if the result is string type.
8080
// Since the JavaScript platform's `string.replace` returns a string,
8181
// the call here will succeed.
8282
// If the script is replaced by `string.search`, the function will
8383
// return an int and the call will fail here.
84-
if (result->TryGetResultAsString(&stringData) != S_OK)
84+
BOOL isString;
85+
if (result->TryGetResultAsString(&stringData, &isString) != S_OK || !isString)
8586
{
8687
MessageBox(
8788
nullptr, L"Get string failed", L"ExecuteScript Result", MB_OK);
@@ -159,13 +160,15 @@ class ExecuteScriptWithResultDemo
159160
bool isSuccess = result.Succeeded;
160161
// Here is the successful execution.
161162
if (isSuccess) {
162-
// Try to get the string result, it will throw an exception
163+
// Try to get the string result, it will return 0
163164
// if the result type isn't string type.
164-
try {
165-
String stringResult = result.TryGetResultAsString();
165+
String stringResult;
166+
if (result.TryGetResultAsString(stringResult) != 0)
167+
{
166168
Debug.WriteLine($"replaced string: {stringResult}");
167169
}
168-
catch (ArgumentException) {
170+
else
171+
{
169172
Debug.WriteLine($"Non-string message received");
170173
}
171174
}
@@ -245,6 +248,9 @@ interface ICoreWebView2ExecuteScriptResult : IUnknown {
245248

246249
/// If Succeeded is true and the result of script execution is a string, this method provides the value of the string result,
247250
/// and we will get the `FALSE` var value when the js result is not string type.
251+
/// The return value description is as follows
252+
/// 1. S_OK: Execution succeeds.
253+
/// 2. E_POINTER: When the `stringResult` or `value` is nullptr.
248254
HRESULT TryGetResultAsString([out] LPWSTR* stringResult, [out, retval] BOOL* value);
249255

250256
/// If Succeeded is false, you can use this property to get the unhandled exception thrown by script execution

0 commit comments

Comments
 (0)