66
77#Tidy_Parameters= / tcb= - 1
88
9- ; NetWebView2Lib.au3 - Script Version: 2026.2.25.11 🚩
9+ ; NetWebView2Lib.au3 - Script Version: 2026.3.17.22 🚩
1010
1111#include < Array.au3>
1212#include < GUIConstantsEx.au3>
2929Global $_g_bNetWebView2_DebugInfo = True
3030; ~ Global $_g_bNetWebView2_DebugDev = False
3131Global $_g_bNetWebView2_DebugDev = (@Compiled = 1 )
32+ Global $_g_bNetWebView2_Sleep = Sleep ; Default to calling standard Sleep function
3233
3334#Region ; === NetWebView2Lib UDF === ENUMS
3435
@@ -92,6 +93,7 @@ Global Enum _ ; $NETWEBVIEW2_MESSAGE__* are set by mainly by __NetWebView2_Event
9293 $NETWEBVIEW2_MESSAGE__DOWNLOAD_COMPLETED , _
9394 $NETWEBVIEW2_MESSAGE__RESPONSE_RECEIVED , _
9495 $NETWEBVIEW2_MESSAGE__UNKNOWN_MESSAGE , _
96+ $NETWEBVIEW2_MESSAGE__USER_ABORT , _
9597 $NETWEBVIEW2_MESSAGE___FAKE_COUNTER
9698
9799Global Enum _
@@ -211,7 +213,9 @@ Func _NetWebView2_Initialize($oWebV2M, $hUserGUI, $s_ProfileDirectory, $i_Left =
211213 Local $iTimeOut_ms = 10000 ; max 10 seconds for initialization
212214 Local $iMessage
213215 Do
214- Sleep (50 )
216+ __NetWebView2_Sleep(10 )
217+ If @error Then Return SetError (@error , @extended , ' ' )
218+
215219 $iMessage = __NetWebView2_LastMessage_KEEPER($oWebV2M )
216220 If $iMessage = $NETWEBVIEW2_MESSAGE__INIT_FAILED _
217221 Or $iMessage = $NETWEBVIEW2_MESSAGE__PROFILE_NOT_READY _
@@ -476,8 +480,9 @@ Func _NetWebView2_LoadWait($oWebV2M, $iWaitMessage = $NETWEBVIEW2_MESSAGE__TITLE
476480 __NetWebView2_LastMessage_Navigation($oWebV2M , $NETWEBVIEW2_MESSAGE__NONE )
477481
478482 While 1
479- ; Allow AutoIt to "breathe" and process the GUI messages
480- Sleep (10 )
483+ ; Allow AutoIt to "breathe" and process the GUI messages, also allow user to abort
484+ __NetWebView2_Sleep(10 )
485+ If @error Then Return SetError (@error , @extended , ' ' )
481486
482487 ; RULE 1: If we reached the target status or higher
483488 Local $bWebIsReady = $oWebV2M .IsReady
@@ -729,14 +734,14 @@ EndFunc ;==>_NetWebView2_GetSource
729734; Name ..........: _NetWebView2_NavigateToPDF
730735; Description ...: Navigate to a PDF (local PDF file or online direct URL link to PDF file)
731736; Syntax ........: _NetWebView2_NavigateToPDF($oWebV2M, $s_URL_or_FilePath[, $s_Parameters = ''[, $iWaitMessage = $NETWEBVIEW2_MESSAGE__TITLE_CHANGED[,
732- ; $sExpectedTitle = ""[, $iTimeOut_ms = 5000[, $iSleep_ms = 1000[, $bFreeze = True]]]]]])
737+ ; $sExpectedTitle = ""[, $iTimeOut_ms = 5000[, $iSleepAfter_ms = 1000[, $bFreeze = True]]]]]])
733738; Parameters ....: $oWebV2M - an object.
734739; $s_URL_or_FilePath - a string value.
735740; $s_Parameters - [optional] a string value. Default is ''.
736741; $iWaitMessage - [optional] an integer value. Default is $NETWEBVIEW2_MESSAGE__TITLE_CHANGED.
737742; $sExpectedTitle - [optional] Expected title to LoadWait for, as StringRegExp() pattern, By Default vaule it will compute the $s_URL_or_FilePath to guess RegExp for the Title
738743; $iTimeOut_ms - [optional] Maximum time to wait in milliseconds. 0 for infinite. Default is 5000ms
739- ; $iSleep_ms - [optional] an integer value. Default is 1000.
744+ ; $iSleepAfter_ms - [optional] an integer value. Default is 1000.
740745; $bFreeze - [optional] a boolean value. Default is True.
741746; Return values .: None
742747; Author ........: mLipok
@@ -746,7 +751,7 @@ EndFunc ;==>_NetWebView2_GetSource
746751; Link ..........:
747752; Example .......: No
748753; ===============================================================================================================================
749- Func _NetWebView2_NavigateToPDF($oWebV2M , $s_URL_or_FilePath , Const $s_Parameters = ' ' , $iWaitMessage = $NETWEBVIEW2_MESSAGE__TITLE_CHANGED , $sExpectedTitle = Default , $iTimeOut_ms = 5000 , Const $iSleep_ms = 1000 , Const $bFreeze = True )
754+ Func _NetWebView2_NavigateToPDF($oWebV2M , $s_URL_or_FilePath , Const $s_Parameters = ' ' , $iWaitMessage = $NETWEBVIEW2_MESSAGE__TITLE_CHANGED , $sExpectedTitle = Default , $iTimeOut_ms = 5000 , Const $iSleepAfter_ms = 1000 , Const $bFreeze = True )
750755 Local Const $s_Prefix = " [_NetWebView2_NavigateToPDF]: URL_or_File:" & $s_URL_or_FilePath ; #TODO suplement
751756
752757 If (Not IsObj ($oWebV2M )) Or ObjName ($oWebV2M , $OBJ_PROGID ) <> ' NetWebView2Lib.WebView2Manager' Then Return SetError (1 , 0 , " ERROR: Invalid Object" )
@@ -779,7 +784,9 @@ Func _NetWebView2_NavigateToPDF($oWebV2M, $s_URL_or_FilePath, Const $s_Parameter
779784 $oWebV2M .LockWebView()
780785 If $bFreeze Then __NetWebView2_freezer($oWebV2M , $idPic )
781786 _NetWebView2_Navigate($oWebV2M , $s_URL_or_FilePath , $iWaitMessage , $sExpectedTitle , $iTimeOut_ms )
782- If Not @error Then Sleep ($iSleep_ms )
787+ If Not @error Then __NetWebView2_Sleep($iSleepAfter_ms )
788+ If @error Then Return SetError (@error , @extended , ' ' )
789+
783790 __NetWebView2_Log(@ScriptLineNumber , $s_Prefix , 1 )
784791 If $bFreeze And $idPic Then __NetWebView2_freezer($oWebV2M , $idPic )
785792 $oWebV2M .UnLockWebView()
@@ -1109,6 +1116,26 @@ EndFunc ;==>_NetJson_EncodeB64
11091116#EndRegion ; === NetWebView2Lib UDF === _NetJson_* functions
11101117
11111118#Region ; === NetWebView2Lib UDF === #INTERNAL_USE_ONLY#
1119+ ; #INTERNAL_USE_ONLY# ===========================================================================================================
1120+ ; Name ..........: __NetWebView2_Sleep
1121+ ; Description ...: Pause script execution for designated timeframe.
1122+ ; Syntax ........: __NetWebView2_Sleep($iPause)
1123+ ; Parameters ....: $iPause - Amount of time to pause (in milliseconds)
1124+ ; Return values .: Success - None
1125+ ; Failure - None and sets @error $NETWEBVIEW2_MESSAGE__USER_ABORT
1126+ ; Author ........: @Danp2, mLipok
1127+ ; Modified ......:
1128+ ; Remarks .......: Calls standard Sleep() by default. This can be overridden by setting $_g_bNetWebView2_Sleep so that a user supplied function gets called instead.
1129+ ; User's function can throw error which will lead to $NETWEBVIEW2_MESSAGE__USER_ABORT
1130+ ; Related .......:
1131+ ; Link ..........:
1132+ ; Example .......: No
1133+ ; ===============================================================================================================================
1134+ Func __NetWebView2_Sleep($iPause )
1135+ $_g_bNetWebView2_Sleep ($iPause )
1136+ If @error Then Return SetError ($NETWEBVIEW2_MESSAGE__USER_ABORT , @extended )
1137+ EndFunc ; ==>__NetWebView2_Sleep
1138+
11121139; #INTERNAL_USE_ONLY# ===========================================================================================================
11131140; Name ..........: __NetWebView2_WaitForReadyState
11141141; Description ...: Polls the browser until the document.readyState reaches 'complete'.
@@ -1153,7 +1180,9 @@ Func __NetWebView2_WaitForReadyState($oWebV2M, $hTimer, $iTimeOut_ms = 5000)
11531180 __NetWebView2_Log(@ScriptLineNumber , $s_Prefix & " TIMEOUT: Document state is " & $sReadyState & " Timeout_ms: " & Round (TimerDiff ($hTimer ), 0 ), 1 )
11541181 Return SetError (1 , 0 , False )
11551182 EndIf
1156- Sleep (50 )
1183+ __NetWebView2_Sleep(50 )
1184+ If @error Then Return SetError (@error , @extended , ' ' )
1185+
11571186 WEnd
11581187EndFunc ; ==>__NetWebView2_WaitForReadyState
11591188
@@ -1479,7 +1508,7 @@ EndFunc ;==>__NetWebView2_freezer
14791508
14801509#EndRegion ; === NetWebView2Lib UDF === #INTERNAL_USE_ONLY#
14811510
1482- #Region ; === NetWebView2Lib UDF === EVENT HANDLERS === Collection ===
1511+ #Region ; === NetWebView2Lib UDF === EVENT HANDLERS ***** Collection *****
14831512
14841513#Region ; === NetWebView2Lib UDF === EVENT HANDLERS === Error Handlers ===
14851514; #INTERNAL_USE_ONLY# ===========================================================================================================
@@ -1609,9 +1638,17 @@ Volatile Func __NetWebView2_Events__OnMessageReceived($oWebV2M, $hGUI, $sMsg)
16091638 __NetWebView2_LastMessage_KEEPER($oWebV2M , $NETWEBVIEW2_MESSAGE__URL_CHANGED )
16101639
16111640 Case " NAV_ERROR"
1612- __NetWebView2_Log(@ScriptLineNumber , $s_Prefix & " COMMAND:" & $sCommand , 1 )
1613- __NetWebView2_LastMessage_KEEPER($oWebV2M , $NETWEBVIEW2_MESSAGE__NAV_ERROR )
1614- $oWebV2M .Stop()
1641+ If $sData = " OperationCanceled" Then ; Check if the error is actually a user cancellation
1642+ __NetWebView2_Log(@ScriptLineNumber , $s_Prefix & " COMMAND:" & $sCommand & " - USER_ABORT Data:" & $sData , 1 )
1643+ __NetWebView2_LastMessage_KEEPER($oWebV2M , $NETWEBVIEW2_MESSAGE__USER_ABORT )
1644+ Else
1645+ __NetWebView2_Log(@ScriptLineNumber , $s_Prefix & " COMMAND:" & $sCommand & " Data:" & $sData , 1 )
1646+ __NetWebView2_LastMessage_KEEPER($oWebV2M , $NETWEBVIEW2_MESSAGE__NAV_ERROR )
1647+ $oWebV2M .Stop() ; We only stop if it is a real error.
1648+ EndIf
1649+
1650+ ; 🚧 *******************************************
1651+ ConsoleWrite (" > TEST NAV_ERR: " & $sMsg & @CRLF )
16151652 ConsoleWrite (" > TEST NAV_ERR: __NetWebView2_LastMessage_KEEPER($oWebV2M)=" & __NetWebView2_LastMessage_KEEPER($oWebV2M ) & " SLN=" & @ScriptLineNumber & @CRLF )
16161653
16171654 Case " NAV_COMPLETED"
@@ -1929,19 +1966,20 @@ EndFunc ;==>__NetWebView2_Events__OnTitleChanged
19291966; #INTERNAL_USE_ONLY# ===========================================================================================================
19301967; Name ..........: __NetWebView2_Events__OnNavigationStarting
19311968; Description ...:
1932- ; Syntax ........: __NetWebView2_Events__OnNavigationStarting($oWebV2M, $hGUI, $sURL )
1969+ ; Syntax ........: __NetWebView2_Events__OnNavigationStarting($oWebV2M, $hGUI, $oArgs )
19331970; Parameters ....: $oWebV2M - an object.
19341971; $hGUI - a handle value.
1935- ; $sURL - a string value .
1972+ ; $oArgs - an object .
19361973; Return values .: None
19371974; Author ........: ioa747, mLipok
19381975; Modified ......:
19391976; Remarks .......:
19401977; Related .......:
1941- ; Link ..........:
1978+ ; Link ..........: https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.navigationstarting
19421979; Example .......: No
19431980; ===============================================================================================================================
1944- Volatile Func __NetWebView2_Events__OnNavigationStarting($oWebV2M , $hGUI , $sURL )
1981+ Volatile Func __NetWebView2_Events__OnNavigationStarting($oWebV2M , $hGUI , $oArgs )
1982+ Local $sURL = $oArgs .Uri
19451983 Local Const $s_Prefix = " [EVENT: OnNavigationStarting]: GUI:" & $hGUI & " URL: " & $sURL
19461984 __NetWebView2_Log(@ScriptLineNumber , (StringLen ($s_Prefix ) > 150 ? StringLeft ($s_Prefix , 150 ) & " ..." : $s_Prefix ), 1 )
19471985 __NetWebView2_LastMessage_KEEPER($oWebV2M , $NETWEBVIEW2_MESSAGE__NAV_STARTING )
@@ -2032,7 +2070,7 @@ EndFunc ;==>__NetWebView2_Events__OnContextMenu
20322070; Modified ......:
20332071; Remarks .......:
20342072; Related .......:
2035- ; Link ..........: https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.webresourceresponsereceived?view=webview2-dotnet-1.0.2849.39
2073+ ; Link ..........: https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.webresourceresponsereceived
20362074; Example .......: No
20372075; ===============================================================================================================================
20382076Volatile Func __NetWebView2_Events__OnWebResourceResponseReceived($oWebV2M , $hGUI , $iStatusCode , $sReasonPhrase , $sRequestUrl )
@@ -2185,7 +2223,7 @@ EndFunc ;==>__NetWebView2_Events__OnProcessFailed
21852223; Remarks .......: The host can provide a response with credentials for the authentication or cancel the request.
21862224; If the host sets the Cancel property to false but does not provide either UserName or Password properties on the Response property, then WebView2 will show the default authentication challenge dialog prompt to the user.
21872225; Related .......:
2188- ; Link ..........: https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.basicauthenticationrequested?view=webview2-dotnet-1.0.2903.40
2226+ ; Link ..........: https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.basicauthenticationrequested
21892227; Example .......: No
21902228; ===============================================================================================================================
21912229Volatile Func __NetWebView2_Events__OnBasicAuthenticationRequested($oWebV2M , $hGUI , $oArgs )
@@ -2284,11 +2322,11 @@ EndFunc ;==>__NetWebView2_Events__OnFrameNameChanged
22842322; #INTERNAL_USE_ONLY# ===========================================================================================================
22852323; Name ..........: __NetWebView2_Events__OnFrameNavigationStarting
22862324; Description ...: Handles Frame NavigationStarting event
2287- ; Syntax ........: __NetWebView2_Events__OnFrameNavigationStarting($oWebV2M, $hGUI, $oFrame, $sUri )
2325+ ; Syntax ........: __NetWebView2_Events__OnFrameNavigationStarting($oWebV2M, $hGUI, $oFrame, $oArgs )
22882326; Parameters ....: $oWebV2M - an object.
22892327; $hGUI - a handle value.
22902328; $oFrame - an Frame object.
2291- ; $sUri - a string value .
2329+ ; $oArgs - an object .
22922330; Return values .: None
22932331; Author ........: ioa747
22942332; Modified ......:
@@ -2297,7 +2335,8 @@ EndFunc ;==>__NetWebView2_Events__OnFrameNameChanged
22972335; Link ..........: https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2frame.navigationstarting
22982336; Example .......: No
22992337; ===============================================================================================================================
2300- Volatile Func __NetWebView2_Events__OnFrameNavigationStarting($oWebV2M , $hGUI , $oFrame , $sUri )
2338+ Volatile Func __NetWebView2_Events__OnFrameNavigationStarting($oWebV2M , $hGUI , $oFrame , $oArgs )
2339+ Local $sUri = $oArgs .Uri
23012340 Local Const $s_Prefix = " [EVENT: OnFrameNavigationStarting]: WebV2M: " & VarGetType ($oWebV2M ) & " GUI:" & $hGUI & " Frame:" & VarGetType ($oFrame ) & " Uri:" & $sUri
23022341 __NetWebView2_Log(@ScriptLineNumber , $s_Prefix , 1 )
23032342 ; __NetWebView2_LastMessage_KEEPER($oWebV2M, $NETWEBVIEW2_MESSAGE__FRAME_NAV_STARTING) ; Optional: Update status if needed
@@ -2410,5 +2449,4 @@ EndFunc ;==>__NetWebView2_Events__FrameKeeper
24102449; ~ EndFunc ;==>__NetWebView2_Events__OnScreenCaptureStarting
24112450#EndRegion ; === NetWebView2Lib UDF === EVENT HANDLERS * #TODO ===
24122451
2413- #EndRegion ; === NetWebView2Lib UDF === EVENT HANDLERS === Collection ===
2414-
2452+ #EndRegion ; === NetWebView2Lib UDF === EVENT HANDLERS ***** Collection *****
0 commit comments