Skip to content

Commit 629bd56

Browse files
authored
Merge pull request #35 from mlipok/patch-1
Update NetWebView2Lib.au3 - better MessageReceived handling
2 parents 79d974b + 6ed4b21 commit 629bd56

1 file changed

Lines changed: 141 additions & 33 deletions

File tree

NetWebView2Lib.au3

Lines changed: 141 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,37 @@ Global $_g_oWeb
2626

2727
Global Enum _ ; $NETWEBVIEW2_MESSAGE__* are set by __NetWebView2_WebViewEvents__OnMessageReceived()
2828
$NETWEBVIEW2_MESSAGE__NONE, _ ; UDF setting - not related directly to API REFERENCES
29+
$NETWEBVIEW2_MESSAGE__INIT_FAILED, _
30+
$NETWEBVIEW2_MESSAGE__PROFILE_NOT_READY, _
2931
$NETWEBVIEW2_MESSAGE__INIT_READY, _
3032
$NETWEBVIEW2_MESSAGE__NAV_STARTING, _
3133
$NETWEBVIEW2_MESSAGE__URL_CHANGED, _
3234
$NETWEBVIEW2_MESSAGE__COMPLETED, _
3335
$NETWEBVIEW2_MESSAGE__TITLE_CHANGED, _
3436
$NETWEBVIEW2_MESSAGE__NAV_ERROR, _
37+
$NETWEBVIEW2_MESSAGE__EXTENSION, _
38+
$NETWEBVIEW2_MESSAGE__EXTENSION_LOADED, _
39+
$NETWEBVIEW2_MESSAGE__EXTENSION_FAILED, _
40+
$NETWEBVIEW2_MESSAGE__EXTENSION_REMOVED, _
41+
$NETWEBVIEW2_MESSAGE__EXTENSION_NOT_FOUND, _
42+
$NETWEBVIEW2_MESSAGE__REMOVE_EXTENSION_FAILED, _
3543
$NETWEBVIEW2_MESSAGE__SELECTED_TEXT, _
3644
$NETWEBVIEW2_MESSAGE__INNER_TEXT, _
37-
$NETWEBVIEW2_MESSAGE__HTML_SOURCE
45+
$NETWEBVIEW2_MESSAGE__INNER_TEXT_FAILED, _
46+
$NETWEBVIEW2_MESSAGE__HTML_SOURCE, _
47+
$NETWEBVIEW2_MESSAGE__CAPTURE_SUCCESS, _
48+
$NETWEBVIEW2_MESSAGE__CAPTURE_ERROR, _
49+
$NETWEBVIEW2_MESSAGE__PRINT_ERROR, _
50+
$NETWEBVIEW2_MESSAGE__PDF_EXPORT_SUCCESS, _
51+
$NETWEBVIEW2_MESSAGE__PDF_EXPORT_ERROR, _
52+
$NETWEBVIEW2_MESSAGE__CDP_RESULT, _
53+
$NETWEBVIEW2_MESSAGE__CDP_ERROR, _
54+
$NETWEBVIEW2_MESSAGE__DATA_CLEARED, _
55+
$NETWEBVIEW2_MESSAGE__COOKIES_B64, _
56+
$NETWEBVIEW2_MESSAGE__COOKIES_ERROR, _
57+
$NETWEBVIEW2_MESSAGE__COOKIE_ADD_ERROR, _
58+
$NETWEBVIEW2_MESSAGE__BLOCKED_AD, _
59+
$NETWEBVIEW2_MESSAGE___FAKE_COUNTER
3860

3961
#Region ; NetWebView2Lib UDF - _NetWebView2_* core functions
4062
; #FUNCTION# ====================================================================================================================
@@ -70,7 +92,7 @@ Func _NetWebView2_Initialize(ByRef $oWebV2M, $hGUI, $sProfileDirectory, $i_Left
7092
Local $iInit = $oWebV2M.Initialize(($hGUI), $sProfileDirectory, $i_Left, $i_Top, $i_Width, $i_Height)
7193
If @error Then Return SetError(@error, @extended, $iInit)
7294

73-
If $b_LoadWait Then _NetWebView2_LoadWait($oWebV2M, $NETWEBVIEW2_MESSAGE__INIT_READY)
95+
If $b_LoadWait Then _NetWebView2_LoadWait($oWebV2M, $NETWEBVIEW2_MESSAGE__INIT_FAILED)
7496
If @error Then Return SetError(@error, @extended, $iInit)
7597

7698
; WebView2 Configuration
@@ -197,9 +219,9 @@ EndFunc ;==>_NetWebView2_CleanUp
197219
; #FUNCTION# ====================================================================================================================
198220
; Name ..........: _NetWebView2_LoadWait
199221
; Description ...:
200-
; Syntax ........: _NetWebView2_LoadWait(ByRef $oWebV2M[, $iWaitNavStatus = $NETWEBVIEW2_MESSAGE__INIT_READY[, $iTimeOut_ms = 0]])
222+
; Syntax ........: _NetWebView2_LoadWait(ByRef $oWebV2M[, $iWaitMessage = $NETWEBVIEW2_MESSAGE__INIT_READY[, $iTimeOut_ms = 0]])
201223
; Parameters ....: $oWebV2M - [in/out] an object.
202-
; $iWaitNavStatus - [optional] an integer value. Default is $NETWEBVIEW2_MESSAGE__INIT_READY.
224+
; $iWaitMessage - [optional] an integer value. Default is $NETWEBVIEW2_MESSAGE__INIT_READY.
203225
; $iTimeOut_ms - [optional] an integer value. Default is 0.
204226
; Return values .: Success - True
205227
; Failure - False and set @error
@@ -210,55 +232,53 @@ EndFunc ;==>_NetWebView2_CleanUp
210232
; Link ..........:
211233
; Example .......: No
212234
; ===============================================================================================================================
213-
Func _NetWebView2_LoadWait(ByRef $oWebV2M, $iWaitNavStatus = $NETWEBVIEW2_MESSAGE__INIT_READY, $iTimeOut_ms = 0)
214-
Local Const $s_Prefix = "[_NetWebView2_LoadWait]: iStatus:" & $iWaitNavStatus
235+
Func _NetWebView2_LoadWait(ByRef $oWebV2M, $iWaitMessage = $NETWEBVIEW2_MESSAGE__INIT_READY, $iTimeOut_ms = 0)
236+
Local Const $s_Prefix = "[_NetWebView2_LoadWait]: iStatus:" & $iWaitMessage
215237
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc) ; Local COM Error Handler
216238
#forceref $oMyError
217239

218240
Local $RETURN_VALUE = False
219-
If $iWaitNavStatus = $NETWEBVIEW2_MESSAGE__NONE Then
241+
If $iWaitMessage = $NETWEBVIEW2_MESSAGE__NONE Then
220242
$RETURN_VALUE = False
221-
ElseIf $iWaitNavStatus > $NETWEBVIEW2_MESSAGE__NAV_ERROR Then ; higher messsages are not for NAVIGATION thus not checking in _NetWebView2_LoadWait()
243+
ElseIf $iWaitMessage > $NETWEBVIEW2_MESSAGE__NAV_ERROR Then ; higher messsages are not for NAVIGATION thus not checking in _NetWebView2_LoadWait()
222244
$RETURN_VALUE = False
223245
SetError(1)
224-
Else
246+
ElseIf $iTimeOut_ms Then
225247
Local $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.
226248

227249
; Wait for WebView2 to be ready
228250
While Sleep(10)
229-
If $iTimeOut_ms And TimerDiff($hTimer) > $iTimeOut_ms Then
251+
If TimerDiff($hTimer) > $iTimeOut_ms Then
230252
SetError(2)
231253
$RETURN_VALUE = False
232254
ExitLoop
233255
EndIf
234256

235257
If $oWebV2M.IsReady Then
236-
Local $iStatus_check = __NetWebView2_NavigationStatus()
237-
If $iStatus_check >= $iWaitNavStatus And $iStatus_check <= $NETWEBVIEW2_MESSAGE__NAV_ERROR Then
258+
Local $iStatus_check = __NetWebView2_LastMessageReceived()
259+
If $iStatus_check >= $iWaitMessage And $iStatus_check <= $NETWEBVIEW2_MESSAGE__NAV_ERROR Then
238260
$RETURN_VALUE = True
239261
ExitLoop
240262
EndIf
241263
EndIf
242264
WEnd
243265
EndIf
244266

245-
Local $iEndStatus = __NetWebView2_NavigationStatus()
267+
Local $iEndStatus = __NetWebView2_LastMessageReceived()
246268
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & " : END STATUS=" & $iEndStatus & ' RETURN_VALUE=' & $RETURN_VALUE, 1)
247-
__NetWebView2_NavigationStatus($NETWEBVIEW2_MESSAGE__NONE) ; reset
248-
249-
;~ Return SetError(@error, $iEndStatus, $RETURN_VALUE)
250-
Return
269+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__NONE) ; reset
251270

271+
Return SetError(@error, $iEndStatus, $RETURN_VALUE)
252272
EndFunc ;==>_NetWebView2_LoadWait
253273

254274
; #FUNCTION# ====================================================================================================================
255275
; Name ..........: _NetWebView2_Navigate
256276
; Description ...:
257-
; Syntax ........: _NetWebView2_Navigate(ByRef $oWebV2M, $sURL[, $iWaitNavStatus = $NETWEBVIEW2_MESSAGE__TITLE_CHANGED[,
277+
; Syntax ........: _NetWebView2_Navigate(ByRef $oWebV2M, $sURL[, $iWaitMessage = $NETWEBVIEW2_MESSAGE__NAV_ERROR[,
258278
; $iTimeOut_ms = 0]])
259279
; Parameters ....: $oWebV2M - [in/out] an object.
260280
; $sURL - a string value.
261-
; $iWaitNavStatus - [optional] an integer value. Default is $NETWEBVIEW2_MESSAGE__TITLE_CHANGED.
281+
; $iWaitMessage - [optional] an integer value. Default is $NETWEBVIEW2_MESSAGE__NAV_ERROR.
262282
; $iTimeOut_ms - [optional] an integer value. Default is 0.
263283
; Return values .: None
264284
; Author ........: mLipok, ioa747
@@ -268,16 +288,16 @@ EndFunc ;==>_NetWebView2_LoadWait
268288
; Link ..........:
269289
; Example .......: No
270290
; ===============================================================================================================================
271-
Func _NetWebView2_Navigate(ByRef $oWebV2M, $sURL, $iWaitNavStatus = $NETWEBVIEW2_MESSAGE__TITLE_CHANGED, $iTimeOut_ms = 0)
272-
Local Const $s_Prefix = "[_NetWebView2_LoadWait]: URL:" & $sURL & " WAIT:" & $iWaitNavStatus
291+
Func _NetWebView2_Navigate(ByRef $oWebV2M, $sURL, $iWaitMessage = $NETWEBVIEW2_MESSAGE__NAV_ERROR, $iTimeOut_ms = 0)
292+
Local Const $s_Prefix = "[_NetWebView2_LoadWait]: URL:" & $sURL & " WAIT:" & $iWaitMessage
273293
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc) ; Local COM Error Handler
274294
#forceref $oMyError
275295

276296
Local $iNavigation = $oWebV2M.Navigate($sURL)
277297
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix, 1)
278298
If @error Then Return SetError(@error, @extended, $iNavigation)
279299

280-
If $iWaitNavStatus Then _NetWebView2_LoadWait($oWebV2M, $iWaitNavStatus, $iTimeOut_ms)
300+
If $iWaitMessage Then _NetWebView2_LoadWait($oWebV2M, $iWaitMessage, $iTimeOut_ms)
281301
If @error Then __NetWebView2_Log(@ScriptLineNumber, $s_Prefix, 1)
282302
Return SetError(@error, @extended, '')
283303
EndFunc ;==>_NetWebView2_Navigate
@@ -486,11 +506,11 @@ EndFunc ;==>_NetJson_EncodeB64
486506

487507
#Region ; NetWebView2Lib UDF - #INTERNAL_USE_ONLY#
488508

489-
Func __NetWebView2_NavigationStatus($iStatus = Default, $iError = @error, $iExtended = @extended)
509+
Func __NetWebView2_LastMessageReceived($iStatus = Default, $iError = @error, $iExtended = @extended)
490510
Local Static $i_static = $NETWEBVIEW2_MESSAGE__NONE
491511
If $iStatus <> Default Then $i_static = $iStatus
492512
Return SetError($iError, $iExtended, $i_static)
493-
EndFunc ;==>__NetWebView2_NavigationStatus
513+
EndFunc ;==>__NetWebView2_LastMessageReceived
494514

495515
; #INTERNAL_USE_ONLY# ===========================================================================================================
496516
; Name ..........: __NetWebView2_Log
@@ -625,40 +645,128 @@ Func __NetWebView2_WebViewEvents__OnMessageReceived($sMsg)
625645
EndIf
626646
Case "NAV_STARTING"
627647
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
628-
__NetWebView2_NavigationStatus($NETWEBVIEW2_MESSAGE__NAV_STARTING)
648+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__NAV_STARTING)
649+
650+
Case "INIT_FAILED"
651+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
652+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__INIT_FAILED)
653+
654+
Case "WebView2 Profile not ready."
655+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
656+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__PROFILE_NOT_READY)
629657

630658
Case "INIT_READY"
631659
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
632-
__NetWebView2_NavigationStatus($NETWEBVIEW2_MESSAGE__INIT_READY)
660+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__INIT_READY)
633661

634662
Case "URL_CHANGED"
635663
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
636-
__NetWebView2_NavigationStatus($NETWEBVIEW2_MESSAGE__URL_CHANGED)
664+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__URL_CHANGED)
637665

638666
Case "NAV_COMPLETED"
639667
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
640-
__NetWebView2_NavigationStatus($NETWEBVIEW2_MESSAGE__COMPLETED)
668+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__COMPLETED)
641669

642670
Case "TITLE_CHANGED"
643671
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
644-
__NetWebView2_NavigationStatus($NETWEBVIEW2_MESSAGE__TITLE_CHANGED)
672+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__TITLE_CHANGED)
645673

646674
Case "NAV_ERROR"
647675
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
648-
__NetWebView2_NavigationStatus($NETWEBVIEW2_MESSAGE__NAV_ERROR)
676+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__NAV_ERROR)
649677
$_g_oWeb.Stop()
650678

679+
Case "EXTENSION"
680+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
681+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__EXTENSION)
682+
683+
Case "EXTENSION_LOADED"
684+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
685+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__EXTENSION_LOADED)
686+
687+
Case "EXTENSION_FAILED"
688+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
689+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__EXTENSION_FAILED)
690+
691+
Case "EXTENSION_REMOVED"
692+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
693+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__EXTENSION_REMOVED)
694+
695+
Case "EXTENSION_NOT_FOUND"
696+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
697+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__EXTENSION_NOT_FOUND)
698+
699+
Case "REMOVE_EXTENSION_FAILED"
700+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
701+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__REMOVE_EXTENSION_FAILED)
702+
651703
Case "SELECTED_TEXT"
652704
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
653-
__NetWebView2_NavigationStatus($NETWEBVIEW2_MESSAGE__SELECTED_TEXT)
705+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__SELECTED_TEXT)
654706

655707
Case "INNER_TEXT"
656708
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
657-
__NetWebView2_NavigationStatus($NETWEBVIEW2_MESSAGE__INNER_TEXT)
709+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__INNER_TEXT)
710+
711+
Case "INNER_TEXT_FAILED"
712+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
713+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__INNER_TEXT_FAILED)
658714

659715
Case "HTML_SOURCE"
660716
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
661-
__NetWebView2_NavigationStatus($NETWEBVIEW2_MESSAGE__HTML_SOURCE)
717+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__HTML_SOURCE)
718+
719+
Case "CAPTURE_SUCCESS"
720+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
721+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__CAPTURE_SUCCESS)
722+
723+
Case "CAPTURE_ERROR"
724+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
725+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__CAPTURE_ERROR)
726+
727+
Case "PRINT_ERROR"
728+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
729+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__PRINT_ERROR)
730+
731+
Case "PDF_EXPORT_SUCCESS"
732+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
733+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__PDF_EXPORT_SUCCESS)
734+
735+
Case "PDF_EXPORT_ERROR"
736+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
737+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__PDF_EXPORT_ERROR)
738+
739+
Case "CDP_RESULT"
740+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
741+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__CDP_RESULT)
742+
743+
Case "CDP_ERROR"
744+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
745+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__CDP_ERROR)
746+
747+
Case "DATA_CLEARED"
748+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
749+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__DATA_CLEARED)
750+
751+
Case "COOKIES_B64"
752+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
753+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__COOKIES_B64)
754+
755+
Case "COOKIES_ERROR"
756+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
757+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__COOKIES_ERROR)
758+
759+
Case "COOKIE_ADD_ERROR"
760+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
761+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__COOKIE_ADD_ERROR)
762+
763+
Case "BLOCKED_AD"
764+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
765+
__NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__BLOCKED_AD)
766+
767+
;~ Case "*"
768+
;~ __NetWebView2_Log(@ScriptLineNumber, $s_Prefix & ' COMMAND:' & $sCommand, 1)
769+
;~ __NetWebView2_LastMessageReceived($NETWEBVIEW2_MESSAGE__*)
662770

663771
Case Else
664772
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & (StringLen($sMsg) > 150 ? StringLeft($sMsg, 150) & "..." : $sMsg), 1)

0 commit comments

Comments
 (0)