Skip to content

Commit d52be4d

Browse files
authored
Merge pull request #102 from mlipok/Ater2.0
First update After 2.0
2 parents c806300 + ba62745 commit d52be4d

2 files changed

Lines changed: 71 additions & 36 deletions

File tree

NetWebView2Lib.au3

Lines changed: 69 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ Global $_g_bNetWebView2_DebugInfo = True
3131
Global $_g_bNetWebView2_DebugDev = (@Compiled = 1)
3232
#Region ; ENUMS
3333

34-
#Region ; ENUMS
35-
3634
;~ Global Enum _
3735
;~ $NETWEBVIEW2_ERR__INIT_FAILED, _
3836
;~ $NETWEBVIEW2_ERR__PROFILE_NOT_READY, _
@@ -116,11 +114,13 @@ Global Enum _ ; Indicates the reason for the process failure.
116114
#Region ; NetWebView2Lib UDF - _NetWebView2_* core functions
117115
; #FUNCTION# ====================================================================================================================
118116
; Name ..........: _NetWebView2_CreateManager
119-
; Description ...:
120-
; Syntax ........: _NetWebView2_CreateManager([$sUserAgent = ''[, $s_fnEventPrefix = ""[, $s_AddBrowserArgs = ""]]])
117+
; Description ...: Create WebView2 object
118+
; Syntax ........: _NetWebView2_CreateManager([$sUserAgent = ''[, $s_fnEventPrefix = ""[, $s_AddBrowserArgs = ""[,
119+
; $bVerbose = False]]]])
121120
; Parameters ....: $sUserAgent - [optional] a string value. Default is ''.
122121
; $s_fnEventPrefix - [optional] a string value. Default is "".
123122
; $s_AddBrowserArgs - [optional] a string value. Default is "". Allows passing command-line switches (e.g., --disable-gpu, --mute-audio, --proxy-server="...") to the Chromium engine.
123+
; $bVerbose - [optional] True/False - Enable/Disable diagnostic logging. Default is False = Disabled.
124124
; Return values .: None
125125
; Author ........: mLipok, ioa747
126126
; Modified ......:
@@ -131,7 +131,7 @@ Global Enum _ ; Indicates the reason for the process failure.
131131
; Link ..........: https://peter.sh/experiments/chromium-command-line-switches/
132132
; Example .......: No
133133
; ===============================================================================================================================
134-
Func _NetWebView2_CreateManager($sUserAgent = '', $s_fnEventPrefix = "", $s_AddBrowserArgs = "")
134+
Func _NetWebView2_CreateManager($sUserAgent = '', $s_fnEventPrefix = "", $s_AddBrowserArgs = "", $bVerbose = False)
135135
Local Const $s_Prefix = "[_NetWebView2_CreateManager]: fnEventPrefix=" & $s_fnEventPrefix & " AddBrowserArgs=" & $s_AddBrowserArgs
136136
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc) ; Local COM Error Handler
137137
#forceref $oMyError
@@ -140,6 +140,11 @@ Func _NetWebView2_CreateManager($sUserAgent = '', $s_fnEventPrefix = "", $s_AddB
140140
If @error Then __NetWebView2_Log(@ScriptLineNumber, $s_Prefix & " Manager Creation ERROR", 1)
141141
If @error Then Return SetError(@error, @extended, 0)
142142

143+
; Enable/Disable diagnostic logging
144+
; When enabled, the console will show entries like: +++[NetWebView2Lib][HANDLE:0x...][HH:mm:ss.fff] Message
145+
; Verbose property was added to allow real-time diagnostic logging to the SciTE console (or any stdout listener).
146+
; The diagnostic logs use a distinctive prefix and include the instance handle for easier filtering in multi-window applications.
147+
$oWebV2M.Verbose = $bVerbose
143148
;~ If $_g_bNetWebView2_DebugDev Then __NetWebView2_ObjName_FlagsValue($oWebV2M) ; FOR DEV TESTING ONLY
144149

145150
If $sUserAgent Then $oWebV2M.SetUserAgent($sUserAgent)
@@ -362,15 +367,16 @@ Func _NetWebView2_CleanUp(ByRef $oWebV2M, ByRef $oJSBridge)
362367

363368
If (Not IsObj($oWebV2M)) Or ObjName($oWebV2M, $OBJ_PROGID) <> 'NetWebView2Lib.WebView2Manager' Then Return SetError(1, 0, __NetWebView2_Log(@ScriptLineNumber, $s_Prefix & " ! Object not found", 1))
364369

365-
; Update Static Map to delete Handle
366-
__NetWebView2_LastMessage_KEEPER($oWebV2M, -1)
367-
370+
_NetWebView2_SetLockState($oWebV2M, True)
368371
Local $iRet = $oWebV2M.Cleanup()
369372
If @error Then __NetWebView2_Log(@ScriptLineNumber, $s_Prefix & " ! Error during internal cleanup", 1)
370373
$oWebV2M = 0
371374
$oJSBridge = 0
372375
If @error Then __NetWebView2_Log(@ScriptLineNumber, $s_Prefix, 1)
373376

377+
; Update Static Map to delete Handle
378+
__NetWebView2_LastMessage_KEEPER($oWebV2M, -1)
379+
374380
Return SetError(@error, @extended, $iRet)
375381
EndFunc ;==>_NetWebView2_CleanUp
376382

@@ -586,13 +592,16 @@ Func _NetWebView2_Navigate($oWebV2M, $s_URL, $iWaitMessage = $NETWEBVIEW2_MESSAG
586592
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc) ; Local COM Error Handler
587593
#forceref $oMyError
588594

595+
If (Not IsObj($oWebV2M)) Or ObjName($oWebV2M, $OBJ_PROGID) <> 'NetWebView2Lib.WebView2Manager' Then Return SetError(1, 0, "ERROR: Invalid Object")
596+
589597
; 1. Parameter Validation
590598
If $iWaitMessage < $NETWEBVIEW2_MESSAGE__INIT_READY Or $iWaitMessage > $NETWEBVIEW2_MESSAGE__TITLE_CHANGED Then ; higher messsages are not for NAVIGATION thus not checking in _NetWebView2_LoadWait()
591599
Return SetError(1, 0, False)
592600
EndIf
593601

594602
; 2. Execute Navigation
595603
; The Local Error Handler catches potential "Disposed Object" crashes here
604+
$oWebV2M.LockWebView()
596605
$oWebV2M.Navigate($s_URL)
597606
If @error Then Return SetError(2, @error, False)
598607

@@ -605,6 +614,7 @@ Func _NetWebView2_Navigate($oWebV2M, $s_URL, $iWaitMessage = $NETWEBVIEW2_MESSAG
605614
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & " -> LOAD WAIT FAILED (Err:" & $iErr & " Ext:" & $iExt & ")", 1)
606615
EndIf
607616

617+
$oWebV2M.UnLockWebView()
608618
Return SetError($iErr, $iExt, $bResult)
609619
EndFunc ;==>_NetWebView2_Navigate
610620

@@ -631,17 +641,26 @@ Func _NetWebView2_NavigateToString($oWebV2M, $s_HTML, $iWaitMessage = $NETWEBVIE
631641
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc) ; Local COM Error Handler
632642
#forceref $oMyError
633643

644+
If (Not IsObj($oWebV2M)) Or ObjName($oWebV2M, $OBJ_PROGID) <> 'NetWebView2Lib.WebView2Manager' Then Return SetError(1, 0, "ERROR: Invalid Object")
645+
634646
If $iWaitMessage < $NETWEBVIEW2_MESSAGE__INIT_READY Then
635647
Return SetError(1)
636648
ElseIf $iWaitMessage > $NETWEBVIEW2_MESSAGE__TITLE_CHANGED Then ; higher messsages are not for NAVIGATION thus not checking in _NetWebView2_LoadWait()
637649
Return SetError(2)
638650
Else
651+
$oWebV2M.LockWebView()
639652
Local $iNavigation = $oWebV2M.NavigateToString($s_HTML)
640-
If @error Then Return SetError(@error, @extended, $iNavigation)
641-
642-
_NetWebView2_LoadWait($oWebV2M, $iWaitMessage, $sExpectedTitle, $iTimeOut_ms)
643-
If @error Then __NetWebView2_Log(@ScriptLineNumber, $s_Prefix, 1)
644-
Return SetError(@error, @extended, '')
653+
Local $iErr = @error, $iExt = @extended
654+
If @error Then
655+
Return SetError($iErr, $iExt, $iNavigation)
656+
Else
657+
_NetWebView2_LoadWait($oWebV2M, $iWaitMessage, $sExpectedTitle, $iTimeOut_ms)
658+
$iErr = @error
659+
$iExt = @extended
660+
$oWebV2M.UnLockWebView()
661+
If $iErr Then __NetWebView2_Log(@ScriptLineNumber, $s_Prefix, 1, $iErr, $iExt)
662+
Return SetError($iErr, $iExt, $iNavigation)
663+
EndIf
645664
EndIf
646665
EndFunc ;==>_NetWebView2_NavigateToString
647666

@@ -740,13 +759,13 @@ EndFunc ;==>_NetWebView2_GetSource
740759
; #FUNCTION# ====================================================================================================================
741760
; Name ..........: _NetWebView2_NavigateToPDF
742761
; Description ...: Navigate to a PDF (local PDF file or online direct URL link to PDF file)
743-
; Syntax ........: _NetWebView2_NavigateToPDF($oWebV2M, $s_URL_or_FileFullPath[, $s_Parameters = ''[, $iWaitMessage = $NETWEBVIEW2_MESSAGE__TITLE_CHANGED[,
762+
; Syntax ........: _NetWebView2_NavigateToPDF($oWebV2M, $s_URL_or_FilePath[, $s_Parameters = ''[, $iWaitMessage = $NETWEBVIEW2_MESSAGE__TITLE_CHANGED[,
744763
; $sExpectedTitle = ""[, $iTimeOut_ms = 5000[, $iSleep_ms = 1000[, $bFreeze = True]]]]]])
745764
; Parameters ....: $oWebV2M - an object.
746-
; $s_URL_or_FileFullPath- a string value.
765+
; $s_URL_or_FilePath - a string value.
747766
; $s_Parameters - [optional] a string value. Default is ''.
748767
; $iWaitMessage - [optional] an integer value. Default is $NETWEBVIEW2_MESSAGE__TITLE_CHANGED.
749-
; $sExpectedTitle - [optional] Expected title to LoadWait for, as StringRegExp() pattern
768+
; $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
750769
; $iTimeOut_ms - [optional] Maximum time to wait in milliseconds. 0 for infinite. Default is 5000ms
751770
; $iSleep_ms - [optional] an integer value. Default is 1000.
752771
; $bFreeze - [optional] a boolean value. Default is True.
@@ -758,27 +777,43 @@ EndFunc ;==>_NetWebView2_GetSource
758777
; Link ..........:
759778
; Example .......: No
760779
; ===============================================================================================================================
761-
Func _NetWebView2_NavigateToPDF($oWebV2M, $s_URL_or_FileFullPath, Const $s_Parameters = '', $iWaitMessage = $NETWEBVIEW2_MESSAGE__TITLE_CHANGED, $sExpectedTitle = "", $iTimeOut_ms = 5000, Const $iSleep_ms = 1000, Const $bFreeze = True)
762-
Local Const $s_Prefix = "[_NetWebView2_NavigateToPDF]: URL_or_File:" & $s_URL_or_FileFullPath ; #TODO suplement
780+
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)
781+
Local Const $s_Prefix = "[_NetWebView2_NavigateToPDF]: URL_or_File:" & $s_URL_or_FilePath ; #TODO suplement
782+
783+
If (Not IsObj($oWebV2M)) Or ObjName($oWebV2M, $OBJ_PROGID) <> 'NetWebView2Lib.WebView2Manager' Then Return SetError(1, 0, "ERROR: Invalid Object")
784+
785+
If $sExpectedTitle = Default Then
786+
Local $aFilePath = StringSplit($s_URL_or_FilePath, "\")
787+
If @error Then
788+
$sExpectedTitle = ''
789+
Else
790+
$sExpectedTitle = $aFilePath[$aFilePath[0]]
791+
$sExpectedTitle = StringReplace($sExpectedTitle, '(', '\(')
792+
$sExpectedTitle = StringReplace($sExpectedTitle, ')', '\)')
793+
$sExpectedTitle = StringReplace($sExpectedTitle, '.', '\.')
794+
EndIf
795+
EndIf
763796

764-
If FileExists($s_URL_or_FileFullPath) Then
765-
$s_URL_or_FileFullPath = StringReplace($s_URL_or_FileFullPath, '\', '/')
766-
$s_URL_or_FileFullPath = StringReplace($s_URL_or_FileFullPath, ' ', '%20')
767-
$s_URL_or_FileFullPath = "file:///" & $s_URL_or_FileFullPath
797+
If FileExists($s_URL_or_FilePath) Then
798+
$s_URL_or_FilePath = StringReplace($s_URL_or_FilePath, '\', '/')
799+
$s_URL_or_FilePath = StringReplace($s_URL_or_FilePath, ' ', '%20')
800+
$s_URL_or_FilePath = "file:///" & $s_URL_or_FilePath
768801
EndIf
769802

770803
If $s_Parameters Then
771-
$s_URL_or_FileFullPath &= $s_Parameters
804+
$s_URL_or_FilePath &= $s_Parameters
772805
#TIP: FitToPage: https://stackoverflow.com/questions/78820187/how-to-change-webview2-fit-to-page-button-on-pdf-toolbar-default-to-fit-to-width#comment138971950_78821231
773806
#TIP: Open desired PAGE: https://stackoverflow.com/questions/68500164/cycle-pdf-pages-in-wpf-webview2#comment135402565_68566860
774807
EndIf
775808

776809
Local $idPic = 0
810+
$oWebV2M.LockWebView()
777811
If $bFreeze Then __NetWebView2_freezer($oWebV2M, $idPic)
778-
_NetWebView2_Navigate($oWebV2M, $s_URL_or_FileFullPath, $iWaitMessage, $sExpectedTitle, $iTimeOut_ms)
812+
_NetWebView2_Navigate($oWebV2M, $s_URL_or_FilePath, $iWaitMessage, $sExpectedTitle, $iTimeOut_ms)
779813
If Not @error Then Sleep($iSleep_ms)
780814
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix, 1)
781815
If $bFreeze And $idPic Then __NetWebView2_freezer($oWebV2M, $idPic)
816+
$oWebV2M.UnLockWebView()
782817
EndFunc ;==>_NetWebView2_NavigateToPDF
783818

784819
; #FUNCTION# ====================================================================================================================
@@ -1357,24 +1392,24 @@ EndFunc ;==>__Get_Core_Bridge_JS
13571392
; Example .......: No
13581393
; ===============================================================================================================================
13591394
Func __NetWebView2_freezer($oWebV2M, ByRef $idPic)
1360-
Local $hWebView2_Window = WinGetHandle($oWebV2M.BrowserWindowHandle)
1395+
Local $hWindow_WebView2 = WinGetHandle($oWebV2M.BrowserWindowHandle)
13611396
#Region ; if $idPic is given then it means you already have it and want to delete it - unfreeze - show WebView2 content
13621397
If $idPic Then
1363-
_SendMessage($hWebView2_Window, $WM_SETREDRAW, True, 0) ; Enables
1364-
_WinAPI_RedrawWindow($hWebView2_Window, 0, 0, BitOR($RDW_FRAME, $RDW_INVALIDATE, $RDW_ALLCHILDREN)) ; Repaints
1398+
_SendMessage($hWindow_WebView2, $WM_SETREDRAW, True, 0) ; Enables
1399+
_WinAPI_RedrawWindow($hWindow_WebView2, 0, 0, BitOR($RDW_FRAME, $RDW_INVALIDATE, $RDW_ALLCHILDREN)) ; Repaints
13651400
GUICtrlDelete($idPic)
13661401
$idPic = 0
13671402
Return
13681403
EndIf
13691404
#EndRegion ; if $idPic is given then it means you already have it and want to delete it - unfreeze - show WebView2 content
13701405

1371-
#Region ; freeze $hWebView2_Window
1406+
#Region ; freeze $hWindow_WebView2
13721407

13731408
#Region ; add PIC to parent window
1374-
Local $hMainGUI_Window = _WinAPI_GetWindow($hWebView2_Window, $GW_HWNDPREV)
1375-
Local $aPos = WinGetPos($hWebView2_Window)
1409+
Local $hWindow_Parent = WinGetHandle($oWebV2M.ParentWindowHandle)
1410+
Local $aPos = WinGetPos($hWindow_WebView2)
13761411
;~ _ArrayDisplay($aPos, '$aPos ' & @ScriptLineNumber)
1377-
Local $hPrev = GUISwitch($hMainGUI_Window)
1412+
Local $hPrev = GUISwitch($hWindow_Parent)
13781413
$idPic = GUICtrlCreatePic('', 0, 0, $aPos[2], $aPos[3])
13791414
Local $hPic = GUICtrlGetHandle($idPic)
13801415
GUISwitch($hPrev)
@@ -1388,7 +1423,7 @@ Func __NetWebView2_freezer($oWebV2M, ByRef $idPic)
13881423
Local $hSrcDC = _WinAPI_CreateCompatibleDC($hDC)
13891424
Local $hBmp = _WinAPI_CreateCompatibleBitmap($hDC, $aPos[2], $aPos[3])
13901425
Local $hSrcSv = _WinAPI_SelectObject($hSrcDC, $hBmp)
1391-
_WinAPI_PrintWindow($hWebView2_Window, $hSrcDC, 2)
1426+
_WinAPI_PrintWindow($hWindow_WebView2, $hSrcDC, 2)
13921427
_WinAPI_BitBlt($hDestDC, 0, 0, $aPos[2], $aPos[3], $hSrcDC, 0, 0, $MERGECOPY)
13931428

13941429
_WinAPI_ReleaseDC($hPic, $hDC)
@@ -1405,9 +1440,9 @@ Func __NetWebView2_freezer($oWebV2M, ByRef $idPic)
14051440
_WinAPI_DeleteObject($hBitmap)
14061441
EndIf
14071442

1408-
_SendMessage($hWebView2_Window, $WM_SETREDRAW, False, 0) ; Disables ; https://www.autoitscript.com/forum/topic/199172-disable-gui-updating-repainting/
1443+
_SendMessage($hWindow_WebView2, $WM_SETREDRAW, False, 0) ; Disables ; https://www.autoitscript.com/forum/topic/199172-disable-gui-updating-repainting/
14091444
Return $idPic
1410-
#EndRegion ; freeze $hWebView2_Window
1445+
#EndRegion ; freeze $hWindow_WebView2
14111446
EndFunc ;==>__NetWebView2_freezer
14121447

14131448
#EndRegion ; NetWebView2Lib UDF - #INTERNAL_USE_ONLY#

examples/004-FileViewerDemo_Loop.au3

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ Func _Example()
7878

7979
$s_PDF_FileFullPath = $a_Files[$IDX_File]
8080
GUICtrlSetData($idLabelStatus, $sProgress & ' - Navigation started: ' & $s_PDF_FileFullPath)
81-
_NetWebView2_NavigateToPDF($oWebV2M, $s_PDF_FileFullPath, '#view=FitH', $NETWEBVIEW2_MESSAGE__TITLE_CHANGED, "", 5000, 1000, True)
81+
_NetWebView2_NavigateToPDF($oWebV2M, $s_PDF_FileFullPath, '#view=FitH', $NETWEBVIEW2_MESSAGE__TITLE_CHANGED, Default, 5000, 1000, True)
8282
GUICtrlSetData($idLabelStatus, $sProgress & ' - Navigation completed: ' & $s_PDF_FileFullPath)
83-
ConsoleWrite("! =Example= @SLN=" & @ScriptLineNumber & ' ' & $s_PDF_FileFullPath & @CRLF)
83+
ConsoleWrite("! =Example= @SLN=" & @ScriptLineNumber & ' NAVIGATION COMPLETED FOR: ' & $s_PDF_FileFullPath & @CRLF)
8484
If $bSleep_UserReaction Then Sleep(2000) ; simulates user reaction on PDF
8585
Next
8686
EndIf

0 commit comments

Comments
 (0)