Skip to content

Commit 434203f

Browse files
authored
Merge pull request #49 from mlipok/_NetWebView2_ExecuteScript()
_NetWebView2_ExecuteScript() added iMode
2 parents 6bc6ff3 + bc31abd commit 434203f

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

NetWebView2Lib.au3

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,47 @@ EndFunc ;==>_NetWebView2_GetBridge
167167
; #FUNCTION# ====================================================================================================================
168168
; Name ..........: _NetWebView2_ExecuteScript
169169
; Description ...:
170-
; Syntax ........: _NetWebView2_ExecuteScript(ByRef $oWebV2M, $sJavaScript)
170+
; Syntax ........: _NetWebView2_ExecuteScript(ByRef $oWebV2M, $sJavaScript[, $iMode = 0])
171171
; Parameters ....: $oWebV2M - [in/out] an object.
172172
; $sJavaScript - a string value.
173+
; $iMode - [optional] One of the following search modes:
174+
; |0 - Execute, do not wait "Fire-and-Forget" - Default
175+
; |1 - ExecuteScriptOnPage "Async-Void Trap"
176+
; |2 - ExecuteScriptWithResult - This is the only method designed to return data
173177
; Return values .: None
174178
; Author ........: mLipok, ioa747
175179
; Modified ......:
176-
; Remarks .......:
180+
; Remarks .......: $iMode additionall information:
181+
; 0. ExecuteScript (Fire-and-Forget)
182+
; In the C# bridge, this is defined as a public void. It uses _webView.Invoke to send the command to the UI thread and exits immediately. It does not wait for the JavaScript to execute, and it has no return type.
183+
; Use case: Clicking buttons, scrolling, or triggering JS events where you don't care about the result.
184+
; 1. ExecuteScriptOnPage (Async-Void Trap)
185+
; This is defined as public async void. In COM Interop (which AutoIt uses), an async void method returns control to the caller (AutoIt) the moment it hits the first internal await. The script runs in the background, but the result is never passed back to the COM interface.
186+
; Use case: Fast execution where background processing is acceptable, but again, no return value.
187+
; 2. ExecuteScriptWithResult
188+
; This is the only method designed to return data. It implements a Message Pump (using Application.DoEvents()) to keep the interface responsive while waiting up to 5 seconds for the result.
189+
; Key Features of ExecuteScriptWithResult:
190+
; Blocking: It waits for the script to finish (Sync-like behavior for AutoIt).
191+
; JSON Cleaning: WebView2 returns results as JSON strings (e.g., "Hello"). This method automatically strips the extra quotes and unescapes the string for you.
192+
; Error Handling: Returns ERROR: Script Timeout if the JS takes longer than 5 seconds.
177193
; Related .......:
178-
; Link ..........:
194+
; Link ..........: https://github.com/ioa747/NetWebView2Lib/issues/45#issuecomment-3831184514
179195
; Example .......: No
180196
; ===============================================================================================================================
181-
Func _NetWebView2_ExecuteScript(ByRef $oWebV2M, $sJavaScript)
182-
Local Const $s_Prefix = "[_NetWebView2_ExecuteScript]:"
197+
Func _NetWebView2_ExecuteScript(ByRef $oWebV2M, $sJavaScript, $iMode = 0)
198+
Local Const $s_Prefix = "[_NetWebView2_ExecuteScript]:" & " TYPE: " & $iMode
183199
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc) ; Local COM Error Handler
184200
#forceref $oMyError
185201

186-
Local $iRet = $oWebV2M.ExecuteScript($sJavaScript)
202+
Local $iRet
203+
Switch $iMode
204+
Case 0
205+
$iRet = $oWebV2M.ExecuteScript($sJavaScript)
206+
Case 1
207+
$iRet = $oWebV2M.ExecuteScriptOnPage($sJavaScript)
208+
Case 2
209+
$iRet = $oWebV2M.ExecuteScriptWithResult($sJavaScript)
210+
EndSwitch
187211
If @error Then __NetWebView2_Log(@ScriptLineNumber, $s_Prefix, 1)
188212
Return SetError(@error, @extended, $iRet)
189213
EndFunc ;==>_NetWebView2_ExecuteScript
@@ -896,3 +920,4 @@ Func __NetWebView2_WebViewEvents__OnContextMenu($sMenuData)
896920
EndFunc ;==>__NetWebView2_WebViewEvents__OnContextMenu
897921
#EndRegion ; NetWebView2Lib UDF - === EVENT HANDLERS ===
898922

923+

examples/v1.5.0_Static_PDF_Viewer/Static_PDF_Viewer.au3

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ Func _Example()
5050
; navigate to the page
5151
SetupStaticPDF($_g_oWeb, @ScriptDir & "\invoice-plugin-sample.pdf", True, True)
5252

53-
$_g_oWeb.ExecuteScriptOnPage("extractPDFText();")
53+
Local $sResult = _NetWebView2_ExecuteScript($_g_oWeb, "extractPDFText();", 2)
54+
MsgBox($MB_TOPMOST, "$sResult", $sResult)
5455

5556
; Main Loop
5657
While 1

0 commit comments

Comments
 (0)