Skip to content

Commit 8be9453

Browse files
authored
Update 018-BasicFramesDemo.au3
1 parent 75ad089 commit 8be9453

1 file changed

Lines changed: 68 additions & 65 deletions

File tree

examples/018-BasicFramesDemo.au3

Lines changed: 68 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ Func Main()
5252
#Region - Testing how to manage frames
5353

5454
_NetWebView2_Navigate($oWebV2M, 'https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe', $NETWEBVIEW2_MESSAGE__TITLE_CHANGED, "", 5000)
55-
MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, 1)
55+
MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, 'Wait till all frames are loaded')
5656
;~ _Demo_NavigateCheckBanner($sSession, "https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe", '//*[@id="snigel-cmp-framework" and @class="snigel-cmp-framework"]')
5757
If @error Then Return SetError(@error, @extended)
5858

59-
#Region ; Example part 1 - testing NetWebView2Lib new methodes: .GetFrameCount() .GetFrameUrl($IDX_Frame) .GetFrameName($IDX_Frame)
60-
ConsoleWrite("+ Example part 1 - testing NetWebView2Lib new methodes: .GetFrameCount() .GetFrameUrl($IDX_Frame) .GetFrameName($IDX_Frame)" & @CRLF)
59+
#Region ; Example part 1 - testing NetWebView2Lib methodes: .GetFrameCount() .GetFrameUrl($IDX_Frame) .GetFrameName($IDX_Frame)
60+
ConsoleWrite("+ Example part 1 - testing NetWebView2Lib methodes: .GetFrameCount() .GetFrameUrl($IDX_Frame) .GetFrameName($IDX_Frame)" & @CRLF)
6161

6262
Local $iFrameCount = $oWebV2M.GetFrameCount()
6363
ConsoleWrite(@CRLF)
@@ -68,17 +68,19 @@ Func Main()
6868
ConsoleWrite("- NAME=" & $oWebV2M.GetFrameName($IDX_Frame) & @CRLF)
6969
ConsoleWrite(@CRLF)
7070
Next
71-
#EndRegion ; Example part 1 - testing NetWebView2Lib new methodes: .GetFrameCount() .GetFrameUrl($IDX_Frame) .GetFrameName($IDX_Frame)
71+
MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, 'Example part 1 - testing NetWebView2Lib methodes: ' & @CRLF & '.GetFrameCount() .GetFrameUrl($IDX_Frame) .GetFrameName($IDX_Frame)' & @CRLF & 'End')
72+
#EndRegion ; Example part 1 - testing NetWebView2Lib methodes: .GetFrameCount() .GetFrameUrl($IDX_Frame) .GetFrameName($IDX_Frame)
7273

73-
#Region ; Example part 2 - testing NetWebView2Lib new methodes: .GetFrameCount() .GetFrameUrl($IDX_Frame) .GetFrameName($IDX_Frame)
74-
ConsoleWrite("+ Example part 2 - testing NetWebView2Lib new methodes: .GetFrameCount() .GetFrameUrl($IDX_Frame) .GetFrameName($IDX_Frame)" & @CRLF)
74+
#Region ; Example part 2 - testing NetWebView2Lib methodes: .GetFrameCount() .GetFrameUrl($IDX_Frame) .GetFrameName($IDX_Frame)
75+
ConsoleWrite("+ Example part 2 - testing NetWebView2Lib methodes: .GetFrameCount() .GetFrameUrl($IDX_Frame) .GetFrameName($IDX_Frame)" & @CRLF)
7576

7677
ConsoleWrite("! " & @ScriptLineNumber & " : GetFrameUrls() :" & @CRLF & $oWebV2M.GetFrameUrls() & @CRLF)
7778
ConsoleWrite("! " & @ScriptLineNumber & " : GetFrameNames() :" & @CRLF & $oWebV2M.GetFrameNames() & @CRLF)
78-
#EndRegion ; Example part 2 - testing NetWebView2Lib new methodes: .GetFrameCount() .GetFrameUrl($IDX_Frame) .GetFrameName($IDX_Frame)
79+
MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, 'Example part 2 - testing NetWebView2Lib methodes:' & @CRLF & '.GetFrameCount() .GetFrameUrl($IDX_Frame) .GetFrameName($IDX_Frame)' & @CRLF & 'End')
80+
#EndRegion ; Example part 2 - testing NetWebView2Lib methodes: .GetFrameCount() .GetFrameUrl($IDX_Frame) .GetFrameName($IDX_Frame)
7981

80-
#Region ; Example part 3 - testing NetWebView2Lib new methodes .GetFrameHtmlSource($IDX_Frame)
81-
ConsoleWrite("+ Example part 3 - testing NetWebView2Lib new methodes .GetFrameHtmlSource($IDX_Frame)" & @CRLF)
82+
#Region ; Example part 3 - testing NetWebView2Lib methodes .GetFrameHtmlSource($IDX_Frame)
83+
ConsoleWrite("+ Example part 3 - testing NetWebView2Lib methodes .GetFrameHtmlSource($IDX_Frame)" & @CRLF)
8284
For $IDX_Frame = 0 To $iFrameCount - 1
8385
ConsoleWrite(@CRLF & "======================================================" & @CRLF)
8486
Local $sHtmlSource = Fire_And_Wait($oWebV2M.GetFrameHtmlSource($IDX_Frame), 5000) ; pair with "FRAME_HTML_SOURCE"
@@ -87,8 +89,8 @@ Func Main()
8789
ConsoleWrite(@CRLF & "======================================================" & @CRLF)
8890
ConsoleWrite(@CRLF)
8991
ConsoleWrite(@CRLF)
90-
#EndRegion ; Example part 3 - testing NetWebView2Lib new methodes .GetFrameHtmlSource($IDX_Frame)
91-
92+
MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, 'Example part 3 - testing NetWebView2Lib methodes : ' & @CRLF & '.GetFrameHtmlSource($IDX_Frame)' & @CRLF & 'End')
93+
#EndRegion ; Example part 3 - testing NetWebView2Lib methodes .GetFrameHtmlSource($IDX_Frame)
9294

9395
#Region ; Example part 4 - Direct Frame Interaction
9496

@@ -106,6 +108,8 @@ Func Main()
106108

107109
ConsoleWrite("VarGetType($oFrame0)=" & VarGetType($oFrame0) & @CRLF)
108110
ConsoleWrite("$oFrame0.Name=" & $oFrame0.Name & @CRLF & @CRLF)
111+
ConsoleWrite("$oFrame0.FrameId=" & $oFrame0.FrameId & @CRLF & @CRLF)
112+
ConsoleWrite("$oFrame0.IsDestroyed()=" & $oFrame0.IsDestroyed() & @CRLF & @CRLF)
109113

110114
; Direct script execution in the iframe without involving the central Manager
111115
$oFrame0.ExecuteScript("document.body.style.backgroundColor = 'red';")
@@ -118,12 +122,19 @@ Func Main()
118122
Else
119123
ConsoleWrite("! Error: $oFrame0 is not a valid COM Object" & @CRLF)
120124
EndIf
121-
125+
MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, 'Example part 4 - Direct Frame Interaction: ' & @CRLF & '' & @CRLF & 'End')
122126
#EndRegion ; Example part 4 - Direct Frame Interaction
123127

128+
#Region ; Example part 5 - Get all Frames as array
129+
Local $aFrames
130+
Do
131+
$aFrames = _NetWebView2_GetAllFrames_AsArray($oWebV2M)
132+
_ArrayDisplay($aFrames, @ScriptLineNumber & ' $aFrames : Example part 5 - Get all Frames as array')
133+
Until ($IDNO = MsgBox($MB_YESNO + $MB_TOPMOST + $MB_ICONQUESTION + $MB_DEFBUTTON2, "Question", "Check again all frames ?"))
124134

125-
#EndRegion - Testing how to manage frames
135+
#EndRegion ; Example part 5 - Get all Frames as array
126136

137+
#EndRegion - Testing how to manage frames
127138

128139
; Main Loop
129140
While 1
@@ -135,46 +146,58 @@ Func Main()
135146

136147
GUIDelete($hGUI)
137148

138-
139149
_NetWebView2_CleanUp($oWebV2M, $oJSBridge)
140150
EndFunc ;==>Main
141151

142-
; ==============================================================================
143-
; ; Function to update a text element inside the WebView UI
144-
; ==============================================================================
145-
Func UpdateWebUI($oWebV2M, $sElementId, $sNewText)
146-
If Not IsObj($oWebV2M) Then Return ''
147-
148-
; Escape backslashes, single quotes and handle new lines for JavaScript safety
149-
Local $sCleanText = StringReplace($sNewText, "\", "\\")
150-
$sCleanText = StringReplace($sCleanText, "'", "\'")
151-
$sCleanText = StringReplace($sCleanText, @CRLF, "\n")
152-
$sCleanText = StringReplace($sCleanText, @LF, "\n")
153-
154-
Local $sJavaScript = "document.getElementById('" & $sElementId & "').innerText = '" & $sCleanText & "';"
155-
_NetWebView2_ExecuteScript($oWebV2M, $sJavaScript)
156-
EndFunc ;==>UpdateWebUI
157-
158-
; ==============================================================================
159-
; MY EVENT HANDLER: Bridge (JavaScript Messages)
160-
; ==============================================================================
161-
Func _BridgeMyEventsHandler_OnMessageReceived($oWebV2M, $hGUI, $sMessage)
162-
Local Static $iMsgCnt = 0
152+
; #FUNCTION# ====================================================================================================================
153+
; Name ..........: _NetWebView2_GetAllFrames_AsArray
154+
; Description ...: Get all Frames as array
155+
; Syntax ........: _NetWebView2_GetAllFrames_AsArray($oWebV2M)
156+
; Parameters ....: $oWebV2M - an object.
157+
; Return values .: None
158+
; Author ........: mLipok
159+
; Modified ......:
160+
; Remarks .......:
161+
; Related .......:
162+
; Link ..........:
163+
; Example .......: No
164+
; ===============================================================================================================================
165+
Func _NetWebView2_GetAllFrames_AsArray($oWebV2M)
166+
Local Const $s_Prefix = "[_NetWebView2_GetAllFrames_AsArray]:"
167+
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc) ; Local COM Error Handler
168+
#forceref $oMyError, $s_Prefix
169+
170+
Local Enum _
171+
$FRAME_IDX, _
172+
$FRAME_OBJECT, _
173+
$FRAME_ID, _
174+
$FRAME_NAME, _
175+
$FRAME_URL, _
176+
$FRAME_DESTROYED, _
177+
$FRAME_HTML, _
178+
$FRAME__COUNTER
163179

164-
If $sMessage = "CLOSE_APP" Then
165-
If MsgBox(36, "Confirm", "Exit Application?", 0, $hGUI) = 6 Then Exit
166-
Else
167-
MsgBox(64, "JS Notification", "Message from Browser: " & $sMessage)
168-
$iMsgCnt += 1
169-
UpdateWebUI($oWebV2M, "mainTitle", $iMsgCnt & " Hello from AutoIt!")
170-
EndIf
171-
EndFunc ;==>_BridgeMyEventsHandler_OnMessageReceived
180+
Local $iFrameCount = $oWebV2M.GetFrameCount()
181+
Local $aFrames[$iFrameCount][$FRAME__COUNTER]
182+
Local $oFrame
183+
For $IDX_Frame = 0 To $iFrameCount - 1
184+
$oFrame = $oWebV2M.GetFrame($IDX_Frame)
185+
$aFrames[$IDX_Frame][$FRAME_IDX] = $IDX_Frame
186+
$aFrames[$IDX_Frame][$FRAME_OBJECT] = $oFrame
187+
$aFrames[$IDX_Frame][$FRAME_ID] = $oFrame.FrameId
188+
$aFrames[$IDX_Frame][$FRAME_NAME] = $oWebV2M.Name
189+
$aFrames[$IDX_Frame][$FRAME_URL] = $oWebV2M.GetFrameUrl($IDX_Frame)
190+
$aFrames[$IDX_Frame][$FRAME_DESTROYED] = $oFrame.IsDestroyed()
191+
$aFrames[$IDX_Frame][$FRAME_HTML] = $oWebV2M.GetFrameHtmlSource($IDX_Frame)
192+
Next
193+
Return $aFrames
194+
EndFunc ;==>_NetWebView2_GetAllFrames_AsArray
172195

173196
; ==============================================================================
174197
; MyHook_ Events
175198
; ==============================================================================
176199
Func MyHook_OnMessageReceived($oWebV2M, $hGUI, $sMsg)
177-
#forceref $oWebV2M, $hGUI, $sMsg
200+
#forceref $oWebV2M, $hGUI
178201
ConsoleWrite("> [MyHook] OnMessageReceived: GUI:" & $hGUI & " Msg: " & (StringLen($sMsg) > 30 ? StringLeft($sMsg, 30) & "..." : $sMsg) & @CRLF)
179202
Local $iSplitPos = StringInStr($sMsg, "|")
180203
Local $sCommand = $iSplitPos ? StringStripWS(StringLeft($sMsg, $iSplitPos - 1), 3) : $sMsg
@@ -194,27 +217,6 @@ Func MyHook_OnMessageReceived($oWebV2M, $hGUI, $sMsg)
194217
EndSwitch
195218
EndFunc ;==>MyHook_OnMessageReceived
196219

197-
; ==============================================================================
198-
; HELPER: Demo HTML Content
199-
; ==============================================================================
200-
Func __GetDemoHTML()
201-
Local $sH = _
202-
'<html><head><style>' & _
203-
'body { font-family: "Segoe UI", sans-serif; background: #202020; color: white; padding: 40px; text-align: center; }' & _
204-
'.card { background: #2d2d2d; padding: 20px; border-radius: 8px; border: 1px solid #444; }' & _
205-
'button { padding: 12px 24px; cursor: pointer; background: #0078d4; color: white; border: none; border-radius: 4px; font-size: 16px; margin: 5px; }' & _
206-
'button:hover { background: #005a9e; }' & _
207-
'</style></head><body>' & _
208-
'<div class="card">' & _
209-
' <h1 id="mainTitle">WebView2 + AutoIt .NET Manager</h1>' & _ ; Fixed ID attribute
210-
' <p id="statusMsg">The communication is now 100% Event-Driven (No Sleep needed).</p>' & _
211-
' <button onclick="window.chrome.webview.postMessage(''Hello from JavaScript!'')">Send Ping</button>' & _
212-
' <button onclick="window.chrome.webview.postMessage(''CLOSE_APP'')">Exit App</button>' & _
213-
'</div>' & _
214-
'</body></html>'
215-
Return $sH
216-
EndFunc ;==>__GetDemoHTML
217-
218220
Func __Example_Log($s_ScriptLineNumber, $sString, $iError = @error, $iExtended = @extended)
219221
ConsoleWrite(@ScriptName & ' SLN=' & $s_ScriptLineNumber & ' [' & $iError & '/' & $iExtended & '] ::: ' & $sString & @CRLF)
220222
Return SetError($iError, $iExtended, '')
@@ -237,6 +239,7 @@ EndFunc ;==>__Example_Log
237239
; Remarks........: This function uses static variables to bridge the gap between async COM events and sync script execution.
238240
; It effectively pauses the script execution until the WebView2 event fires back with data.
239241
; ===============================================================================================================================
242+
240243
Func Fire_And_Wait($sData = "", $iTimeout = 5000)
241244
Local Static $vStoredData = ""
242245
Local Static $hJobTimer = 0

0 commit comments

Comments
 (0)