-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path007-HTTP_StatusCodeTracking.au3
More file actions
162 lines (128 loc) · 6.2 KB
/
007-HTTP_StatusCodeTracking.au3
File metadata and controls
162 lines (128 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
;~ #AutoIt3Wrapper_UseX64=y
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include "..\NetWebView2Lib.au3"
#include <WinAPISysWin.au3>
; Global objects
Global $oMyError = ObjEvent("AutoIt.Error", "_ErrFunc") ; COM Error Handler
_Example_HTTP_Tracking()
Func _Example_HTTP_Tracking()
ConsoleWrite("! MicrosoftEdgeWebview2 : version check: " & _NetWebView2_IsAlreadyInstalled() & ' ERR=' & @error & ' EXT=' & @extended & @CRLF)
Local $hGUI = GUICreate("WebView2 HTTP Status Tracker", 1000, 600)
ConsoleWrite("$hGUI=" & $hGUI & @CRLF)
; Initialize WebView2 Manager and register events
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_")
If @error Then Return SetError(@error, @extended, $oWebV2M)
; create JavaScript Bridge object
Local $oJSBridge = _NetWebView2_GetBridge($oWebV2M, "JavaScript_")
If @error Then Return SetError(@error, @extended, $oWebV2M)
; initialize browser - put it on the GUI
Local $sProfileDirectory = @ScriptDir & "\NetWebView2Lib-UserDataFolder"
_NetWebView2_Initialize($oWebV2M, $hGUI, $sProfileDirectory, 0, 0, 0, 0, True, True, 1.2, "0x2B2B2B")
; Setting up HTTP Tracking
$oWebV2M.HttpStatusCodeEventsEnabled = True
; Filtering only for the Main Document
; Very important to prevent the GUI from getting stuck by hundreds of requests (images, scripts, etc.)
$oWebV2M.HttpStatusCodeDocumentOnly = True
GUISetState(@SW_SHOW)
; Testing with a non-existent page to see the 404
_NetWebView2_Navigate($oWebV2M, "https://google.com/this-page-does-not-exist")
__Example_Log(@ScriptLineNumber, "END - close window to exit" & @CRLF)
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
EndSwitch
WEnd
_NetWebView2_CleanUp($oWebV2M, $oJSBridge)
EndFunc ;==>_Example_HTTP_Tracking
#Region ; === EVENT HANDLERS ===
; Handles native WebView2 events
Func WebEvents_OnMessageReceived($oWebV2M, $hGUI, $sMsg)
; { part of the $hGUI handle explanation
; with the new Advanced Handle Formatting logic [HANDLE:0x...]
ConsoleWrite("$hGUI=" & $hGUI & @CRLF)
ConsoleWrite("WinExists=" & WinExists($hGUI) & @CRLF)
ConsoleWrite("WinGetTitle=" & WinGetTitle($hGUI) & @CRLF)
ConsoleWrite("! _WinAPI_GetClientWidth($hGUI)=" & _WinAPI_GetClientWidth($hGUI) & @CRLF) ; not working
Local $hWnd = WinGetHandle($hGUI)
ConsoleWrite("$hWnd=" & $hWnd & @CRLF)
ConsoleWrite("WinExists($hWnd)=" & WinExists($hWnd) & @CRLF)
ConsoleWrite("- _WinAPI_GetClientWidth($hWnd)=" & _WinAPI_GetClientWidth($hWnd) & @CRLF) ; working
; End part of the $hGUI handle explanation }
ConsoleWrite(">>> [WebEvents]: " & (StringLen($sMsg) > 150 ? StringLeft($sMsg, 150) & "..." : $sMsg) & @CRLF)
Local $iSplitPos = StringInStr($sMsg, "|")
Local $sCommand = $iSplitPos ? StringStripWS(StringLeft($sMsg, $iSplitPos - 1), 3) : $sMsg
Local $sData = $iSplitPos ? StringTrimLeft($sMsg, $iSplitPos) : ""
#forceref $sData
Local $aParts
#forceref $aParts
Switch $sCommand
Case "INIT_READY"
_NetWebView2_ExecuteScript($oWebV2M, _
'window.chrome.webview.postMessage(JSON.stringify({ "type": "COM_TEST", "status": "OK" }));', _
$NETWEBVIEW2_EXECUTEJS_MODE0_FIREANDFORGET)
EndSwitch
EndFunc ;==>WebEvents_OnMessageReceived
; Handles custom messages from JavaScript (window.chrome.webview.postMessage)
Func JavaScript_OnMessageReceived($oWebV2M, $hGUI, $sMsg)
#forceref $oWebV2M, $hGUI
ConsoleWrite(">>> [JavaScript]: " & (StringLen($sMsg) > 150 ? StringLeft($sMsg, 150) & "..." : $sMsg) & @CRLF)
Local $sFirstChar = StringLeft($sMsg, 1)
; 1. JSON Messaging
If $sFirstChar = "{" Or $sFirstChar = "[" Then
ConsoleWrite("+>>> : Processing JSON Messaging..." & @CRLF)
Local $oJson = _NetJson_CreateParser($sMsg)
If @error Then Return ConsoleWrite("!> Error: Failed to create NetJson object." & @CRLF)
Local $sJobType = $oJson.GetTokenValue("type")
Switch $sJobType
Case "COM_TEST"
ConsoleWrite("-- COM_TEST Confirmed: " & $oJson.GetTokenValue("status") & @CRLF)
EndSwitch
Else
; 2. Legacy / Native Pipe-Delimited Messaging
ConsoleWrite("+>>> : Legacy / Native Pipe-Delimited Messaging..." & @CRLF)
Local $sCommand, $sData, $iSplitPos
$iSplitPos = StringInStr($sMsg, "|") - 1
If $iSplitPos < 0 Then
$sCommand = StringStripWS($sMsg, 3)
$sData = ""
Else
$sCommand = StringStripWS(StringLeft($sMsg, $iSplitPos), 3)
$sData = StringTrimLeft($sMsg, $iSplitPos + 1)
EndIf
Switch $sCommand
Case "COM_TEST"
ConsoleWrite("- Status: Legacy COM_TEST: " & $sData & @CRLF)
Case "ERROR"
ConsoleWrite("! Status: " & $sData & @CRLF)
EndSwitch
EndIf
EndFunc ;==>JavaScript_OnMessageReceived
; OnWebResourceResponseReceived
Func WebEvents_OnWebResourceResponseReceived($oWebV2M, $hGUI, $iStatusCode, $sReasonPhrase, $sRequestUrl)
#forceref $hGUI
Local $sLog = StringFormat("! [HTTP %d] | %s | URL: %s", $iStatusCode, $sReasonPhrase, $sRequestUrl)
ConsoleWrite($sLog & @CRLF)
Local $oGuard = ObjEvent("AutoIt.Error", __NetWebView2_fake_COMErrFunc)
#forceref $oGuard
; Management example:
If $iStatusCode >= 400 Then
ConsoleWrite("Navigation Issue detected on: " & @CRLF & $sRequestUrl)
; If it is the main URL and not an iframe/sub-resource
If $iStatusCode = 404 Then
; We use a small Ad-hoc HTML for the error
Local $sErrorHTML = "<html><body style='background:#222;color:#ff4c4c;text-align:center;padding-top:50px;'>" & _
"<h1>😟 Navigation Error " & $iStatusCode & " 🫢</h1>" & _
"<p>The requested URL was not found.</p>" & _
"<button onclick='history.back()'>Go Back</button></body></html>"
; direct - call without LoadWait (ms matters)
$oWebV2M.NavigateToString($sErrorHTML)
EndIf
EndIf
EndFunc ;==>WebEvents_OnWebResourceResponseReceived
#EndRegion ; === EVENT HANDLERS ===
Func __Example_Log($s_ScriptLineNumber, $sString, $iError = @error, $iExtended = @extended)
ConsoleWrite(@ScriptName & ' SLN=' & $s_ScriptLineNumber & ' [' & $iError & '/' & $iExtended & '] ::: ' & $sString & @CRLF)
Return SetError($iError, $iExtended, '')
EndFunc ;==>__Example_Log