11#include < GUIConstantsEx.au3>
22#include < WindowsConstants.au3>
3-
4- ; Register exit function to ensure clean WebView2 shutdown
5- OnAutoItExitRegister (" _ExitApp" )
3+ #include " ..\NetWebView2Lib.au3"
64
75; Global objects
8- Global $oWeb , $oJS
96Global $oMyError = ObjEvent (" AutoIt.Error" , " _ErrFunc" ) ; COM Error Handler
10- Global $g_DebugInfo = True
11- Global $sProfileDirectory = @ScriptDir & " \UserDataFolder"
12- Global $hGUI
137
148_Example_HTTP_Tracking()
159
1610Func _Example_HTTP_Tracking()
17- $hGUI = GUICreate (" WebView2 HTTP Status Tracker" , 1000 , 600 )
11+ Local $hGUI = GUICreate (" WebView2 HTTP Status Tracker" , 1000 , 600 )
12+
13+ ; Initialize WebView2 Manager and register events
14+ Local $oWebV2M = _NetWebView2_CreateManager(" Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0" , " WebEvents_" )
15+ If @error Then Return SetError (@error , @extended , $oWebV2M )
1816
19- $oWeb = ObjCreate ( " NetWebView2.Manager " )
20- If Not IsObj ( $oWeb ) Then Return MsgBox ( 16 , " Error " , " WebView2 Library not registered! " )
21- ObjEvent ( $oWeb , " WebEvents_ " , " IWebViewEvents " )
17+ ; create JavaScript Bridge object
18+ Local $oJSBridge = _NetWebView2_GetBridge( $oWebV2M , " JavaScript_ " )
19+ If @error Then Return SetError ( @error , @extended , $oWebV2M )
2220
23- ; Initialize JavaScript Bridge
24- $oJS = $oWeb .GetBridge()
25- ObjEvent ( $oJS , " JavaScript_ " , " IBridgeEvents " )
21+ ; initialize browser - put it on the GUI
22+ Local $sProfileDirectory = @ScriptDir & " \NetWebView2Lib-UserDataFolder "
23+ _NetWebView2_Initialize( $oWebV2M , $hGUI , $sProfileDirectory , 0 , 0 , 0 , 0 , True , True , True , 1.2 , " 0x2B2B2B " )
2624
27- ; 2. Ρύθμιση του HTTP Tracking
28- $oWeb .HttpStatusCodeEventsEnabled = True
25+ ; Ρύθμιση του HTTP Tracking
26+ $oWebV2M .HttpStatusCodeEventsEnabled = True
2927
3028 ; Φιλτράρισμα μόνο για το Main Document
3129 ; Πολύ σημαντικό για να μην κολλάει το GUI από εκατοντάδες αιτήματα (εικόνες, scripts κλπ)
32- $oWeb .HttpStatusCodeDocumentOnly = True
30+ $oWebV2M .HttpStatusCodeDocumentOnly = True
3331
34- $oWeb .Initialize(($hGUI ), $sProfileDirectory , 0 , 0 , 1000 , 600 )
35-
36- Do
37- Sleep (10 )
38- Until $oWeb .IsReady
3932
4033 ; Δοκιμή με μια σελίδα που δεν υπάρχει για να δούμε το 404
41- $oWeb .Navigate(" https://google.com/this-page-does-not-exist" )
42- ; ~ $oWeb.Navigate("https://google.com")
34+ ; ~ $oWebV2M.Navigate("https://google.com/this-page-does-not-exist")
35+ _NetWebView2_Navigate($oWebV2M , " https://google.com/this-page-does-not-exist" )
36+ ; ~ $oWebV2M.Navigate("https://google.com")
4337
4438 GUISetState (@SW_SHOW )
4539
4640 While 1
4741 Switch GUIGetMsg ()
4842 Case $GUI_EVENT_CLOSE
49- $oWeb .Cleanup()
50- Exit
43+ ExitLoop
5144 EndSwitch
5245 WEnd
46+
47+ _NetWebView2_CleanUp($oWebV2M , $oJSBridge )
48+
5349EndFunc ; ==>_Example_HTTP_Tracking
5450
5551#Region ; === EVENT HANDLERS ===
56-
5752; Handles native WebView2 events
58- Func WebEvents_OnMessageReceived($sMsg )
59- __DW( " +++ [WebEvents]: " & (StringLen ($sMsg ) > 150 ? StringLeft ($sMsg , 150 ) & " ..." : $sMsg ) & @CRLF , 0 )
53+ Func WebEvents_OnMessageReceived($oWebV2M , $hGUI , $ sMsg )
54+ ConsoleWrite ( " >>> [WebEvents]: " & (StringLen ($sMsg ) > 150 ? StringLeft ($sMsg , 150 ) & " ..." : $sMsg ) & @CRLF )
6055 Local $iSplitPos = StringInStr ($sMsg , " |" )
6156 Local $sCommand = $iSplitPos ? StringStripWS (StringLeft ($sMsg , $iSplitPos - 1 ), 3 ) : $sMsg
6257 Local $sData = $iSplitPos ? StringTrimLeft ($sMsg , $iSplitPos ) : " "
6358 Local $aParts
6459
6560 Switch $sCommand
6661 Case " INIT_READY"
67- $oWeb .ExecuteScript(' window.chrome.webview.postMessage(JSON.stringify({ "type": "COM_TEST", "status": "OK" }));' )
68-
69- Case " WINDOW_RESIZED"
70- $aParts = StringSplit ($sData , " |" )
71- If $aParts [0 ] > = 2 Then
72- Local $iW = Int ($aParts [1 ]), $iH = Int ($aParts [2 ])
73- ; Filter minor resize glitches
74- If $iW > 50 And $iH > 50 Then __DW(" WINDOW_RESIZED : " & $iW & " x" & $iH & @CRLF )
75- EndIf
62+ _NetWebView2_ExecuteScript($oWebV2M , _
63+ ' window.chrome.webview.postMessage(JSON.stringify({ "type": "COM_TEST", "status": "OK" }));' , _
64+ $NETWEBVIEW2_EXECUTEJS_MODE0_FIREANDFORGET )
65+
7666 EndSwitch
7767EndFunc ; ==>WebEvents_OnMessageReceived
7868
7969; Handles custom messages from JavaScript (window.chrome.webview.postMessage)
80- Func JavaScript_OnMessageReceived($sMsg )
81- __DW(" >>> [JavaScript]: " & (StringLen ($sMsg ) > 150 ? StringLeft ($sMsg , 150 ) & " ..." : $sMsg ) & @CRLF , 0 )
70+ Func JavaScript_OnMessageReceived($oWebV2M , $hGUI , $sMsg )
71+ #forceref $oWebV2M
72+ ConsoleWrite (" >>> [JavaScript]: " & (StringLen ($sMsg ) > 150 ? StringLeft ($sMsg , 150 ) & " ..." : $sMsg ) & @CRLF )
8273 Local $sFirstChar = StringLeft ($sMsg , 1 )
8374
8475 ; 1. JSON Messaging
8576 If $sFirstChar = " {" Or $sFirstChar = " [" Then
86- __DW (" +> : Processing JSON Messaging..." & @CRLF )
87- Local $oJson = ObjCreate ( " NetJson.Parser " )
88- If Not IsObj ( $oJson ) Then Return ConsoleWrite (" !> Error: Failed to create NetJson object." & @CRLF )
77+ ConsoleWrite (" +>> > : Processing JSON Messaging..." & @CRLF )
78+ Local $oJson = _NetJson_CreateParser( $sMsg )
79+ If @error Then Return ConsoleWrite (" !> Error: Failed to create NetJson object." & @CRLF )
8980
90- $oJson .Parse($sMsg )
9181 Local $sJobType = $oJson .GetTokenValue(" type" )
92-
9382 Switch $sJobType
9483 Case " COM_TEST"
95- __DW( " - COM_TEST Confirmed: " & $oJson .GetTokenValue(" status" ) & @CRLF )
84+ ConsoleWrite ( " - - COM_TEST Confirmed: " & $oJson .GetTokenValue(" status" ) & @CRLF )
9685 EndSwitch
9786
9887 Else
9988 ; 2. Legacy / Native Pipe-Delimited Messaging
100- __DW (" +> : Legacy / Native Pipe-Delimited Messaging..." & @CRLF , 0 )
89+ ConsoleWrite (" +>>> : Legacy / Native Pipe-Delimited Messaging..." & @CRLF )
10190 Local $sCommand , $sData , $iSplitPos
10291 $iSplitPos = StringInStr ($sMsg , " |" ) - 1
10392
@@ -111,70 +100,39 @@ Func JavaScript_OnMessageReceived($sMsg)
111100
112101 Switch $sCommand
113102 Case " COM_TEST"
114- __DW (" - Status: Legacy COM_TEST: " & $sData & @CRLF )
103+ ConsoleWrite (" - Status: Legacy COM_TEST: " & $sData & @CRLF )
115104
116105 Case " ERROR"
117- __DW (" ! Status: " & $sData & @CRLF )
106+ ConsoleWrite (" ! Status: " & $sData & @CRLF )
118107 EndSwitch
119108 EndIf
120109EndFunc ; ==>JavaScript_OnMessageReceived
121110
122111; OnWebResourceResponseReceived
123- Func WebEvents_OnWebResourceResponseReceived($iStatusCode , $sReasonPhrase , $sRequestUrl )
124- Local $sLog = StringFormat (" ! [HTTP %d] | %s | URL: %s" , $iStatusCode , $sReasonPhrase , $sRequestUrl )
125- ConsoleWrite ($sLog & @CRLF )
126-
127- ; Management example:
128- If $iStatusCode > = 400 Then
129- __DW(" Navigation Issue detected on: " & $sRequestUrl )
130-
131- ; If it is the main URL and not an iframe/sub-resource
132- If $iStatusCode = 404 Then
133- ; We use a small Ad-hoc HTML for the error
134- Local $sErrorHTML = " <html><body style='background:#222;color:#ff4c4c;text-align:center;padding-top:50px;'>" & _
135- " <h1>😟 Navigation Error " & $iStatusCode & " 🫢</h1>" & _
136- " <p>The requested URL was not found.</p>" & _
137- " <button onclick='history.back()'>Go Back</button></body></html>"
138-
139- $oWeb .NavigateToString($sErrorHTML )
140- EndIf
141- EndIf
112+ Func WebEvents_OnWebResourceResponseReceived($oWebV2M , $hGUI , $iStatusCode , $sReasonPhrase , $sRequestUrl )
113+ Local $sLog = StringFormat (" ! [HTTP %d] | %s | URL: %s" , $iStatusCode , $sReasonPhrase , $sRequestUrl )
114+ ConsoleWrite ($sLog & @CRLF )
115+
116+ Local $oGuard = ObjEvent (" AutoIt.Error" , " _NetWebView2_SilentErrorHandler" )
117+ #forceref $oGuard
118+
119+ ; Management example:
120+ If $iStatusCode > = 400 Then
121+ ConsoleWrite (" Navigation Issue detected on: " & @CRLF & $sRequestUrl )
122+
123+ ; If it is the main URL and not an iframe/sub-resource
124+ If $iStatusCode = 404 Then
125+ ; We use a small Ad-hoc HTML for the error
126+ Local $sErrorHTML = " <html><body style='background:#222;color:#ff4c4c;text-align:center;padding-top:50px;'>" & _
127+ " <h1>😟 Navigation Error " & $iStatusCode & " 🫢</h1>" & _
128+ " <p>The requested URL was not found.</p>" & _
129+ " <button onclick='history.back()'>Go Back</button></body></html>"
130+
131+ ; direct - call without LoadWait (ms matters)
132+ $oWebV2M .NavigateToString($sErrorHTML )
133+ EndIf
134+ EndIf
142135EndFunc ; ==>WebEvents_OnWebResourceResponseReceived
143136
144137#EndRegion ; === EVENT HANDLERS ===
145138
146- #Region ; === UTILS ===
147- Func _ErrFunc($oError ) ; Global COM Error Handler
148- ConsoleWrite (' @@ Line(' & $oError .scriptline & ' ) : COM Error Number: (0x' & Hex ($oError .number , 8 ) & ' ) ' & $oError .windescription & @CRLF )
149- EndFunc ; ==>_ErrFunc
150-
151- ; Debug Write utility
152- Func __DW($sString , $iErrorNoLineNo = 1 , $iLine = @ScriptLineNumber , $iError = @error , $iExtended = @extended )
153- If Not $g_DebugInfo Then Return SetError ($iError , $iExtended , 0 )
154- Local $iReturn
155- If $iErrorNoLineNo = 1 Then
156- If $iError Then
157- $iReturn = ConsoleWrite (" @@(" & $iLine & " ) :: @error:" & $iError & " , @extended:" & $iExtended & " , " & $sString )
158- Else
159- $iReturn = ConsoleWrite (" +>(" & $iLine & " ) :: " & $sString )
160- EndIf
161- Else
162- $iReturn = ConsoleWrite ($sString )
163- EndIf
164- Return SetError ($iError , $iExtended , $iReturn )
165- EndFunc ; ==>__DW
166-
167- Func _NetJson_New($sInitialJson = " {}" )
168- Local $oParser = ObjCreate (" NetJson.Parser" )
169- If Not IsObj ($oParser ) Then Return SetError (1 , 0 , 0 )
170- If $sInitialJson <> " " Then $oParser .Parse($sInitialJson )
171- Return $oParser
172- EndFunc ; ==>_NetJson_New
173-
174- Func _ExitApp()
175- If IsObj ($oWeb ) Then $oWeb .Cleanup()
176- $oWeb = 0
177- $oJS = 0
178- Exit
179- EndFunc ; ==>_ExitApp
180- #EndRegion ; === UTILS ===
0 commit comments