Skip to content

Commit c80fc3b

Browse files
authored
Merge pull request #93 from mlipok/patch-2
refactoring _NetWebView2_LoadWait() by adding __NetWebView2_WaitForReadyState() - supplement
2 parents 6b84d58 + 9830c7d commit c80fc3b

13 files changed

Lines changed: 64 additions & 36 deletions

examples/003-Multi-Basic.au3

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ EndFunc ;==>_GetDemoHTML
7575
#Region ; USER DEFINED EVENTS HANDLER FUNCTION
7676
; BROWSER 1 - Manager Events
7777
Func __UserEventHandler_Web1__Manager__OnMessageReceived($oWebView, $hWindow, $sMsg)
78-
#forceref $hWindow
78+
$hWindow = WinGetHandle($hWindow)
7979
ConsoleWrite(">> [Browser 1]: " & (StringLen($sMsg) > 150 ? StringLeft($sMsg, 150) & "..." : $sMsg) & @CRLF)
8080
If $sMsg = "INIT_READY" Then
8181
_NetWebView2_ExecuteScript($oWebView, 'window.chrome.webview.postMessage(JSON.stringify({ "type": "COM_TEST", "status": "OK" }));', $NETWEBVIEW2_EXECUTEJS_MODE0_FIREANDFORGET)
@@ -84,6 +84,8 @@ EndFunc ;==>__UserEventHandler_Web1__Manager__OnMessageReceived
8484

8585
; BROWSER 1 - JavaScript Bridge Events
8686
Func __UserEventHandler_Web1__Bridge__OnMessageReceived($oWebView, $hWindow, $sMsg)
87+
$hWindow = WinGetHandle($hWindow)
88+
8789
Local Static $iMsgCnt = -1
8890
ConsoleWrite(">> [JS 1]: " & (StringLen($sMsg) > 150 ? StringLeft($sMsg, 150) & "..." : $sMsg) & @CRLF)
8991

@@ -107,7 +109,7 @@ EndFunc ;==>__UserEventHandler_Web1__Bridge__OnMessageReceived
107109

108110
; BROWSER 2 - Manager Events
109111
Func __UserEventHandler_Web2__Manager__OnMessageReceived($oWebView, $hWindow, $sMsg)
110-
#forceref $hWindow
112+
$hWindow = WinGetHandle($hWindow)
111113
ConsoleWrite(">> [Browser 2]: " & (StringLen($sMsg) > 150 ? StringLeft($sMsg, 150) & "..." : $sMsg) & @CRLF)
112114
If $sMsg = "INIT_READY" Then
113115
_NetWebView2_ExecuteScript($oWebView, 'window.chrome.webview.postMessage(JSON.stringify({ "type": "COM_TEST", "status": "OK" }));', $NETWEBVIEW2_EXECUTEJS_MODE0_FIREANDFORGET)
@@ -116,6 +118,7 @@ EndFunc ;==>__UserEventHandler_Web2__Manager__OnMessageReceived
116118

117119
; BROWSER 2 - JavaScript Bridge Events
118120
Func __UserEventHandler_Web2__Bridge__OnMessageReceived($oWebView, $hWindow, $sMsg)
121+
$hWindow = WinGetHandle($hWindow)
119122
Local Static $iMsgCnt = -1
120123
ConsoleWrite(">> [JS 2]: " & (StringLen($sMsg) > 150 ? StringLeft($sMsg, 150) & "..." : $sMsg) & @CRLF)
121124

examples/004-FileViewerDemo_Loop.au3

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
_Example()
3737

3838
Func _Example()
39-
; Create the UI
39+
ConsoleWrite("! MicrosoftEdgeWebview2 : version check: " & _NetWebView2_IsAlreadyInstalled() & ' ERR=' & @error & ' EXT=' & @extended & @CRLF)
40+
41+
; Create the GUI
4042
Local $iHeight = 800
4143
Local $hMainGUIWindow = GUICreate("WebView2 .NET Manager - Demo: " & @ScriptName, 1100, $iHeight, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
4244
Local $idLabelStatus = GUICtrlCreateLabel("Status: Initializing Engine...", 10, $iHeight - 20, 1080, 20)
@@ -76,7 +78,7 @@ Func _Example()
7678

7779
$s_PDF_FileFullPath = $a_Files[$IDX_File]
7880
GUICtrlSetData($idLabelStatus, $sProgress & ' - Navigation started: ' & $s_PDF_FileFullPath)
79-
_NetWebView2_NavigateToPDF($oWebV2M, $s_PDF_FileFullPath, '#view=FitH', 1000, True)
81+
_NetWebView2_NavigateToPDF($oWebV2M, $s_PDF_FileFullPath, '#view=FitH', $NETWEBVIEW2_MESSAGE__TITLE_CHANGED, "", 5000, 1000, True)
8082
GUICtrlSetData($idLabelStatus, $sProgress & ' - Navigation completed: ' & $s_PDF_FileFullPath)
8183
ConsoleWrite("! =Example= @SLN=" & @ScriptLineNumber & ' ' & $s_PDF_FileFullPath & @CRLF)
8284
If $bSleep_UserReaction Then Sleep(2000) ; simulates user reaction on PDF

examples/005-SaveDemo.au3

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
_Example()
1414

1515
Func _Example()
16+
ConsoleWrite("! MicrosoftEdgeWebview2 : version check: " & _NetWebView2_IsAlreadyInstalled() & ' ERR=' & @error & ' EXT=' & @extended & @CRLF)
17+
1618
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc)
1719
#forceref $oMyError
1820

examples/007-HTTP_StatusCodeTracking.au3

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Global $oMyError = ObjEvent("AutoIt.Error", "_ErrFunc") ; COM Error Handler
1010
_Example_HTTP_Tracking()
1111

1212
Func _Example_HTTP_Tracking()
13+
ConsoleWrite("! MicrosoftEdgeWebview2 : version check: " & _NetWebView2_IsAlreadyInstalled() & ' ERR=' & @error & ' EXT=' & @extended & @CRLF)
14+
1315
Local $hGUI = GUICreate("WebView2 HTTP Status Tracker", 1000, 600)
1416
ConsoleWrite("$hGUI=" & $hGUI & @CRLF)
1517

@@ -32,7 +34,6 @@ Func _Example_HTTP_Tracking()
3234
; Very important to prevent the GUI from getting stuck by hundreds of requests (images, scripts, etc.)
3335
$oWebV2M.HttpStatusCodeDocumentOnly = True
3436

35-
3637
; Testing with a non-existent page to see the 404
3738
_NetWebView2_Navigate($oWebV2M, "https://google.com/this-page-does-not-exist")
3839

@@ -71,7 +72,9 @@ Func WebEvents_OnMessageReceived($oWebV2M, $hGUI, $sMsg)
7172
Local $iSplitPos = StringInStr($sMsg, "|")
7273
Local $sCommand = $iSplitPos ? StringStripWS(StringLeft($sMsg, $iSplitPos - 1), 3) : $sMsg
7374
Local $sData = $iSplitPos ? StringTrimLeft($sMsg, $iSplitPos) : ""
75+
#forceref $sData
7476
Local $aParts
77+
#forceref $aParts
7578

7679
Switch $sCommand
7780
Case "INIT_READY"
@@ -84,7 +87,7 @@ EndFunc ;==>WebEvents_OnMessageReceived
8487

8588
; Handles custom messages from JavaScript (window.chrome.webview.postMessage)
8689
Func JavaScript_OnMessageReceived($oWebV2M, $hGUI, $sMsg)
87-
#forceref $oWebV2M
90+
#forceref $oWebV2M, $hGUI
8891
ConsoleWrite(">>> [JavaScript]: " & (StringLen($sMsg) > 150 ? StringLeft($sMsg, 150) & "..." : $sMsg) & @CRLF)
8992
Local $sFirstChar = StringLeft($sMsg, 1)
9093

@@ -126,6 +129,8 @@ EndFunc ;==>JavaScript_OnMessageReceived
126129

127130
; OnWebResourceResponseReceived
128131
Func WebEvents_OnWebResourceResponseReceived($oWebV2M, $hGUI, $iStatusCode, $sReasonPhrase, $sRequestUrl)
132+
#forceref $hGUI
133+
129134
Local $sLog = StringFormat("! [HTTP %d] | %s | URL: %s", $iStatusCode, $sReasonPhrase, $sRequestUrl)
130135
ConsoleWrite($sLog & @CRLF)
131136

examples/008-JavaScript.au3

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Global $oWebV2M, $oBridge
1212
_Example_Console_Redirect()
1313

1414
Func _Example_Console_Redirect()
15+
ConsoleWrite("! MicrosoftEdgeWebview2 : version check: " & _NetWebView2_IsAlreadyInstalled() & ' ERR=' & @error & ' EXT=' & @extended & @CRLF)
16+
1517
Local $hGUI = GUICreate("Console Redirect Test", 400, 300)
1618

1719
; 1. Initialize WebView2

examples/009-Auditor.au3

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ _Example()
2121
Exit
2222

2323
Func _Example()
24+
ConsoleWrite("! MicrosoftEdgeWebview2 : version check: " & _NetWebView2_IsAlreadyInstalled() & ' ERR=' & @error & ' EXT=' & @extended & @CRLF)
25+
2426
#Region ; === GUI ===
2527
Local $hGUI = GUICreate("AutoIt Auditor", 1100, 850, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
2628
GUISetBkColor(0x1E1E1E, $hGUI)
@@ -149,8 +151,8 @@ Func _Example()
149151
Local $sProfileDirectory = @ScriptDir & "\NetWebView2Lib-UserDataFolder"
150152
_NetWebView2_Initialize($oWebV2M, $hGUI, $sProfileDirectory, 420, 45, 670, 795, True, True, 1.2, "0x1E1E1E")
151153

152-
_NetWebView2_Navigate($oWebV2M, $sURL, $NETWEBVIEW2_MESSAGE__NAV_STARTING)
153-
154+
_NetWebView2_Navigate($oWebV2M, $sURL)
155+
WinSetState($hGUI, "", @SW_MAXIMIZE)
154156
GUISetState(@SW_SHOW)
155157

156158
#EndRegion ; === GUI ===

examples/010-NavigationBar.au3

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Global $sProfileDirectory = @ScriptDir & "\NetWebView2Lib-UserDataFolder"
3131
_Example()
3232

3333
Func _Example()
34+
ConsoleWrite("! MicrosoftEdgeWebview2 : version check: " & _NetWebView2_IsAlreadyInstalled() & ' ERR=' & @error & ' EXT=' & @extended & @CRLF)
35+
3436
Local $hDLL = DllOpen("user32.dll")
3537
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc)
3638
#forceref $oMyError
@@ -57,7 +59,7 @@ Func _Example()
5759
GUISetState(@SW_SHOW)
5860

5961
; navigate to the page
60-
_NetWebView2_Navigate($oWebV2M, "https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population", $NETWEBVIEW2_MESSAGE__TITLE_CHANGED)
62+
_NetWebView2_Navigate($oWebV2M, "https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population")
6163
ConsoleWrite("navigate to the page" & @CRLF)
6264

6365
; Main Loop
@@ -100,8 +102,6 @@ Func _Example()
100102

101103
WEnd
102104

103-
__NetWebView2_LastMessageReceived($oWebV2M, -1000)
104-
105105
_NetWebView2_CleanUp($oWebV2M, $oJSBridge)
106106
GUIDelete($hGUI)
107107
DllClose($hDLL)
@@ -269,7 +269,7 @@ Func _Web_GoTo($oWebV2M, $sURL) ; Navigates to a URL or performs a Google search
269269

270270
; Execution
271271
ConsoleWrite("-> Web_GoTo: " & $sFinalURL & @CRLF)
272-
$oWebV2M.Navigate($sFinalURL)
272+
_NetWebView2_Navigate($oWebV2M, $sFinalURL)
273273
Return True
274274
EndFunc ;==>_Web_GoTo
275275

examples/011-TwinWebView.au3

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ _MainGUI()
3737

3838
;--------------------------------------------------------------------------------------------------------------------------------
3939
Func _MainGUI() ; Creates the primary application window and starts the message loop
40+
ConsoleWrite("! MicrosoftEdgeWebview2 : version check: " & _NetWebView2_IsAlreadyInstalled() & ' ERR=' & @error & ' EXT=' & @extended & @CRLF)
41+
4042
; Use $WS_CLIPCHILDREN to prevent flickering when resizing child windows
4143
$hMainGUI = GUICreate("Multi-WebView2 v1.4.0", 1000, 600, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
4244

@@ -85,13 +87,13 @@ Func _MainGUI() ; Creates the primary application window and starts the message
8587
Case $Bar1.GlobalNavButton
8688
MouseClick("right")
8789
Case $Bar1.ctx_Google
88-
If IsObj($oWeb1) Then $oWeb1.Navigate("https://www.google.com")
90+
If IsObj($oWeb1) Then _NetWebView2_Navigate($oWeb1, "https://www.google.com")
8991
Case $Bar1.ctx_AutoIt
90-
If IsObj($oWeb1) Then $oWeb1.Navigate("https://www.autoitscript.com/forum")
92+
If IsObj($oWeb1) Then _NetWebView2_Navigate($oWeb1, "https://www.autoitscript.com/forum")
9193
Case $Bar1.ctx_Ghostery
92-
If IsObj($oWeb1) Then $oWeb1.Navigate("extension://mlomiejdfkolichcflejclcbmpeaniij/pages/panel/index.html")
94+
If IsObj($oWeb1) Then _NetWebView2_Navigate($oWeb1, "extension://mlomiejdfkolichcflejclcbmpeaniij/pages/panel/index.html")
9395
Case $Bar1.ctx_DarkReader
94-
If IsObj($oWeb1) Then $oWeb1.Navigate("extension://eimadpbcbfnmbkopoojfekhnkhdbieeh/ui/popup/index.html")
96+
If IsObj($oWeb1) Then _NetWebView2_Navigate($oWeb1, "extension://eimadpbcbfnmbkopoojfekhnkhdbieeh/ui/popup/index.html")
9597
Case $Bar1.ctx_Extensions_Manager
9698
_WV2_ShowExtensionPicker(500, 600, $hMainGUI, @ScriptDir & "\Extensions_Lib", $Bar1.Web_ProfilePath)
9799
Case $Bar1.ctx_EnableCustomMenu
@@ -188,7 +190,7 @@ Func _InitBrowsers() ; Creates child window containers and initializes WebView2
188190
Sleep(10)
189191
Until $oWeb1.IsReady And $oWeb2.IsReady
190192

191-
$oWeb1.Navigate("https://www.google.com/search?q=web1")
193+
_NetWebView2_Navigate($oWeb1, "https://www.google.com/search?q=web1")
192194
GUISetState(@SW_SHOWNOACTIVATE, $hID1)
193195

194196
$oWeb2.Navigate("https://www.google.com/search?q=web2")
@@ -272,7 +274,7 @@ Func Bridge2_OnMessageReceived($sMsg) ; [Bridge2 JS]
272274
Local $sURL = "https://www.google.com/search?q=" & $sSearchText & "+site:" & $sDomain2
273275

274276
; Update Browser 1 with the results
275-
$oWeb1.Navigate($sURL)
277+
_NetWebView2_Navigate($oWeb1, $sURL)
276278
EndIf
277279
EndSwitch
278280
EndFunc ;==>Bridge2_OnMessageReceived

examples/012-GetCookies.au3

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <Array.au3>
77
#include <GuiEdit.au3>
88
#include <Misc.au3>
9+
#include "..\NetWebView2Lib.au3"
910

1011
_VersionChecker("1.2.0.0") ; DLL Version Check
1112

@@ -22,9 +23,11 @@ Global $hGUI, $idURL, $idStatusLabel
2223
Global $g_bURLFullSelected = False
2324
Global $g_bAutoRestoreSession = False
2425

25-
Main()
26+
_Example()
27+
28+
Func _Example()
29+
ConsoleWrite("! MicrosoftEdgeWebview2 : version check: " & _NetWebView2_IsAlreadyInstalled() & ' ERR=' & @error & ' EXT=' & @extended & @CRLF)
2630

27-
Func Main()
2831
#Region ; === Gui AutoIt ===
2932
$hGUI = GUICreate("AutoIt", 1285, 850, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
3033
GUISetBkColor(0x1E1E1E, $hGUI)
@@ -120,7 +123,7 @@ Func Main()
120123
$oManager.GetCookies(GUICtrlRead($idURL))
121124

122125
Case $idBtnRestoreSession
123-
Local $sURL = GUICtrlRead($idURL)
126+
$sURL = GUICtrlRead($idURL)
124127
Local $sDomainOnly = StringRegExpReplace($sURL, "https?://([^/]+).*", "$1")
125128
_RestoreSession($sDomainOnly)
126129

@@ -147,7 +150,7 @@ Func Main()
147150
GUICtrlSetData($idStatusLabel, "Reload")
148151

149152
Case $idURL
150-
$oManager.Navigate(GUICtrlRead($idURL))
153+
_NetWebView2_Navigate($oManager, GUICtrlRead($idURL))
151154
GUICtrlSetData($idStatusLabel, "Navigate: " & GUICtrlRead($idURL))
152155

153156
EndSwitch
@@ -159,7 +162,7 @@ Func Main()
159162

160163
WEnd
161164

162-
EndFunc ;==>Main
165+
EndFunc ;==>_Example
163166
;---------------------------------------------------------------------------------------
164167
Func _CleanExit() ; CleanExit
165168
; Check if the object exists before calling methods to avoid COM errors during crash
@@ -170,8 +173,8 @@ Func _CleanExit() ; CleanExit
170173
; Release the event sinks
171174
$oManager = 0
172175
$oBridge = 0
173-
$oEvtManager = 0
174-
$oEvtBridge = 0
176+
;~ $oEvtManager = 0
177+
;~ $oEvtBridge = 0
175178
$oMyError = 0
176179

177180
ConsoleWrite("--> Application exited cleanly." & @CRLF)
@@ -199,7 +202,7 @@ EndFunc ;==>Bridge_OnMessageReceived
199202
;---------------------------------------------------------------------------------------
200203
Func WebView_OnMessageReceived($sMessage)
201204
ConsoleWrite("+> [CORE EVENT]: " & $sMessage & @CRLF)
202-
Local Static $bIsInitialized, $sCurentURL = "", $sLastRestoredDomain = ""
205+
Local Static $sCurentURL = "", $sLastRestoredDomain = ""
203206
Local $sDomain
204207

205208
; Separating messages that have parameters (e.g. TITLE_CHANGED|...)
@@ -208,8 +211,7 @@ Func WebView_OnMessageReceived($sMessage)
208211

209212
Switch $sCommand
210213
Case "INIT_READY"
211-
$bIsInitialized = True ; We note that we are finished.
212-
$oManager.Navigate(GUICtrlRead($idURL))
214+
_NetWebView2_Navigate($oManager, GUICtrlRead($idURL))
213215
GUISetState(@SW_SHOW, $hGUI)
214216

215217
Case "NAV_STARTING"
@@ -368,8 +370,11 @@ Func ShowWebNotification($sMessage, $sBgColor = "#4CAF50", $iDuration = 3000) ;
368370

369371
$oManager.ExecuteScript($sJS)
370372
EndFunc ;==>ShowWebNotification
373+
371374
;---------------------------------------------------------------------------------------
372375
Func WM_SIZE($hWnd, $iMsg, $wParam, $lParam) ; Synchronizes WebView size with the GUI window
376+
#forceref $hWnd, $iMsg
377+
373378
If $wParam = 1 Then Return $GUI_RUNDEFMSG ; 1 = SIZE_MINIMIZED
374379

375380
Local $iNewWidth = BitAND($lParam, 0xFFFF)

examples/013-jsonTree.au3

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ _Example()
1818

1919
#Region ; UDF TESTING EXAMPLE
2020
Func _Example()
21+
ConsoleWrite("! MicrosoftEdgeWebview2 : version check: " & _NetWebView2_IsAlreadyInstalled() & ' ERR=' & @error & ' EXT=' & @extended & @CRLF)
22+
2123
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc)
2224
#forceref $oMyError
2325

@@ -101,7 +103,7 @@ Func _Example()
101103
WEnd
102104

103105
_NetWebView2_CleanUp($oWebV2M, $oJSBridge)
104-
EndFunc ;==>Main
106+
EndFunc ;==>_Example
105107

106108
#Region ; === UTILS ===
107109

@@ -159,7 +161,7 @@ Func _Web_jsonTree(ByRef $oWebV2M, $sJavaScripton)
159161
"</script></body></html>"
160162

161163
; 4. Navigate to the generated HTML
162-
$oWebV2M.NavigateToString($sHTML)
164+
_NetWebView2_NavigateToString($oWebV2M, $sHTML)
163165
__NetWebView2_Log(@ScriptLineNumber, "+ JSON Tree Rendered & Listeners Active")
164166
EndFunc ;==>_Web_jsonTree
165167

0 commit comments

Comments
 (0)