Skip to content

Commit 1ca26fd

Browse files
authored
Refactor BasicFramesDemo.au3 for cleanup and updates
1 parent 0b336f1 commit 1ca26fd

1 file changed

Lines changed: 11 additions & 79 deletions

File tree

examples/018-BasicFramesDemo.au3

Lines changed: 11 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#WIP - this Example is imported from 1.5.0 UDF - and is in "WORK IN PROGRESS" state
21
#AutoIt3Wrapper_UseX64=y
32
#AutoIt3Wrapper_Run_AU3Check=Y
43
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y
@@ -28,11 +27,11 @@ Func Main()
2827

2928
; Initialize WebView2 Manager and register events
3029
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", _
31-
"MyHook_", "--disable-gpu, --mute-audio")
30+
"", "--disable-gpu, --mute-audio")
3231
If @error Then Return SetError(@error, @extended, $oWebV2M)
3332

3433
; Initialize JavaScript Bridge
35-
Local $oJSBridge = _NetWebView2_GetBridge($oWebV2M, "_BridgeMyEventsHandler_")
34+
Local $oJSBridge = _NetWebView2_GetBridge($oWebV2M, "")
3635
If @error Then Return SetError(@error, @extended, $oWebV2M)
3736

3837
Local $sProfileDirectory = @ScriptDir & "\NetWebView2Lib-UserDataFolder"
@@ -81,9 +80,12 @@ Func Main()
8180

8281
#Region ; Example part 3 - testing NetWebView2Lib methodes .GetFrameHtmlSource($IDX_Frame)
8382
ConsoleWrite("+ Example part 3 - testing NetWebView2Lib methodes .GetFrameHtmlSource($IDX_Frame)" & @CRLF)
83+
Local $oFrame
8484
For $IDX_Frame = 0 To $iFrameCount - 1
8585
ConsoleWrite(@CRLF & "======================================================" & @CRLF)
86-
Local $sHtmlSource = Fire_And_Wait($oWebV2M.GetFrameHtmlSource($IDX_Frame), 5000) ; pair with "FRAME_HTML_SOURCE"
86+
$oFrame = $oWebV2M.GetFrame($IDX_Frame)
87+
If Not IsObj($oFrame) Then ContinueLoop
88+
Local $sHtmlSource = _WebView2_FrameGetHtmlSource($oFrame)
8789
ConsoleWrite("! " & @ScriptLineNumber & " : GetFrameHtmlSource(" & $IDX_Frame & ") :" & @CRLF & $sHtmlSource & @CRLF)
8890
Next
8991
ConsoleWrite(@CRLF & "======================================================" & @CRLF)
@@ -181,92 +183,22 @@ Func _NetWebView2_GetAllFrames_AsArray($oWebV2M)
181183
Local $aFrames[$iFrameCount][$FRAME__COUNTER]
182184
Local $oFrame
183185
For $IDX_Frame = 0 To $iFrameCount - 1
186+
184187
$oFrame = $oWebV2M.GetFrame($IDX_Frame)
188+
If Not IsObj($oFrame) Then ContinueLoop
189+
185190
$aFrames[$IDX_Frame][$FRAME_IDX] = $IDX_Frame
186191
$aFrames[$IDX_Frame][$FRAME_OBJECT] = $oFrame
187192
$aFrames[$IDX_Frame][$FRAME_ID] = $oFrame.FrameId
188193
$aFrames[$IDX_Frame][$FRAME_NAME] = $oFrame.Name
189194
$aFrames[$IDX_Frame][$FRAME_URL] = $oFrame.Source
190-
$aFrames[$IDX_Frame][$FRAME_DESTROYED] = $oFrame.IsDestroyed()
191-
$aFrames[$IDX_Frame][$FRAME_HTML] = Fire_And_Wait($oWebV2M.GetFrameHtmlSource($IDX_Frame), 5000)
195+
$aFrames[$IDX_Frame][$FRAME_DESTROYED] = $oFrame.IsDestroyed ;()
196+
$aFrames[$IDX_Frame][$FRAME_HTML] = _WebView2_FrameGetHtmlSource($oFrame)
192197
Next
193198
Return $aFrames
194199
EndFunc ;==>_NetWebView2_GetAllFrames_AsArray
195200

196-
; ==============================================================================
197-
; MyHook_ Events
198-
; ==============================================================================
199-
Func MyHook_OnMessageReceived($oWebV2M, $hGUI, $sMsg)
200-
#forceref $oWebV2M, $hGUI
201-
ConsoleWrite("> [MyHook] OnMessageReceived: GUI:" & $hGUI & " Msg: " & (StringLen($sMsg) > 30 ? StringLeft($sMsg, 30) & "..." : $sMsg) & @CRLF)
202-
Local $iSplitPos = StringInStr($sMsg, "|")
203-
Local $sCommand = $iSplitPos ? StringStripWS(StringLeft($sMsg, $iSplitPos - 1), 3) : $sMsg
204-
Local $sData = $iSplitPos ? StringTrimLeft($sMsg, $iSplitPos) : ""
205-
;~ Local $aParts
206-
207-
Switch $sCommand
208-
Case "INIT_READY"
209-
210-
Case "FRAME_HTML_SOURCE"
211-
$iSplitPos = StringInStr($sData, "|")
212-
Local $sIDX = StringLeft($sData, $iSplitPos - 1)
213-
ConsoleWrite(" >> $sIDX=" & $sIDX & @CRLF)
214-
Local $sHtmlSource = StringTrimLeft($sData, $iSplitPos)
215-
If $sHtmlSource = "null" Then $sHtmlSource = "!! <Inaccessible>"
216-
Fire_And_Wait($sHtmlSource)
217-
EndSwitch
218-
EndFunc ;==>MyHook_OnMessageReceived
219-
220201
Func __Example_Log($s_ScriptLineNumber, $sString, $iError = @error, $iExtended = @extended)
221202
ConsoleWrite(@ScriptName & ' SLN=' & $s_ScriptLineNumber & ' [' & $iError & '/' & $iExtended & '] ::: ' & $sString & @CRLF)
222203
Return SetError($iError, $iExtended, '')
223204
EndFunc ;==>__Example_Log
224-
225-
; #FUNCTION# ====================================================================================================================
226-
; Name...........: Fire_And_Wait
227-
; Description....: Synchronizes asynchronous events by waiting for a response or a timeout.
228-
; Syntax.........: Fire_And_Wait([$sData = "" [, $iTimeout = 5000]])
229-
; Parameters.....: $sData - [Optional] Data string.
230-
; If provided: Acts as a "Signal" (setter) from the Event Handler.
231-
; If empty: Acts as a "Listener" (getter) from the Main Script.
232-
; $iTimeout - [Optional] Maximum wait time in milliseconds (Default is 5000ms).
233-
; Return values..: Success - Returns the stored data string.
234-
; Sets @extended to the duration of the wait in ms.
235-
; Failure - Returns an empty string and sets @error:
236-
; |1 - Timeout reached.
237-
; Author.........: YourName
238-
; Modified.......: 2026-02-23
239-
; Remarks........: This function uses static variables to bridge the gap between async COM events and sync script execution.
240-
; It effectively pauses the script execution until the WebView2 event fires back with data.
241-
; ===============================================================================================================================
242-
243-
Func Fire_And_Wait($sData = "", $iTimeout = 5000)
244-
Local Static $vStoredData = ""
245-
Local Static $hJobTimer = 0
246-
247-
; === Part A: Response (From Event Handler) ===
248-
If $sData <> "" Then
249-
$vStoredData = $sData
250-
Return True
251-
EndIf
252-
253-
; === Part B: Fire and Wait (From Main Script) ===
254-
$vStoredData = ""
255-
$hJobTimer = TimerInit()
256-
257-
While $vStoredData = ""
258-
If TimerDiff($hJobTimer) > $iTimeout Then
259-
ConsoleWrite("! Fire_And_Wait | TIMEOUT after " & Round(TimerDiff($hJobTimer), 2) & " ms" & @CRLF)
260-
Return SetError(1, 0, "")
261-
EndIf
262-
Sleep(10)
263-
WEnd
264-
265-
Local $fDuration = TimerDiff($hJobTimer)
266-
Local $vResult = $vStoredData
267-
$vStoredData = "" ; Reset for next use
268-
269-
ConsoleWrite("> Fire_And_Wait | Duration: " & Round($fDuration, 0) & " ms | Status: SUCCESS" & @CRLF)
270-
271-
Return SetError(0, Int($fDuration), $vResult)
272-
EndFunc ;==>Fire_And_Wait

0 commit comments

Comments
 (0)