|
1 | | -#WIP - this Example is imported from 1.5.0 UDF - and is in "WORK IN PROGRESS" state |
2 | 1 | #AutoIt3Wrapper_UseX64=y |
3 | 2 | #AutoIt3Wrapper_Run_AU3Check=Y |
4 | 3 | #AutoIt3Wrapper_AU3Check_Stop_OnWarning=y |
@@ -28,11 +27,11 @@ Func Main() |
28 | 27 |
|
29 | 28 | ; Initialize WebView2 Manager and register events |
30 | 29 | 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") |
32 | 31 | If @error Then Return SetError(@error, @extended, $oWebV2M) |
33 | 32 |
|
34 | 33 | ; Initialize JavaScript Bridge |
35 | | - Local $oJSBridge = _NetWebView2_GetBridge($oWebV2M, "_BridgeMyEventsHandler_") |
| 34 | + Local $oJSBridge = _NetWebView2_GetBridge($oWebV2M, "") |
36 | 35 | If @error Then Return SetError(@error, @extended, $oWebV2M) |
37 | 36 |
|
38 | 37 | Local $sProfileDirectory = @ScriptDir & "\NetWebView2Lib-UserDataFolder" |
@@ -81,9 +80,12 @@ Func Main() |
81 | 80 |
|
82 | 81 | #Region ; Example part 3 - testing NetWebView2Lib methodes .GetFrameHtmlSource($IDX_Frame) |
83 | 82 | ConsoleWrite("+ Example part 3 - testing NetWebView2Lib methodes .GetFrameHtmlSource($IDX_Frame)" & @CRLF) |
| 83 | + Local $oFrame, $sHtmlSource |
84 | 84 | For $IDX_Frame = 0 To $iFrameCount - 1 |
| 85 | + $oFrame = $oWebV2M.GetFrame($IDX_Frame) |
| 86 | + $sHtmlSource = _WebView2_FrameGetHtmlSource($oFrame) |
| 87 | + If @error Then ContinueLoop |
85 | 88 | ConsoleWrite(@CRLF & "======================================================" & @CRLF) |
86 | | - Local $sHtmlSource = Fire_And_Wait($oWebV2M.GetFrameHtmlSource($IDX_Frame), 5000) ; pair with "FRAME_HTML_SOURCE" |
87 | 89 | ConsoleWrite("! " & @ScriptLineNumber & " : GetFrameHtmlSource(" & $IDX_Frame & ") :" & @CRLF & $sHtmlSource & @CRLF) |
88 | 90 | Next |
89 | 91 | ConsoleWrite(@CRLF & "======================================================" & @CRLF) |
@@ -181,92 +183,24 @@ Func _NetWebView2_GetAllFrames_AsArray($oWebV2M) |
181 | 183 | Local $aFrames[$iFrameCount][$FRAME__COUNTER] |
182 | 184 | Local $oFrame |
183 | 185 | For $IDX_Frame = 0 To $iFrameCount - 1 |
| 186 | + |
184 | 187 | $oFrame = $oWebV2M.GetFrame($IDX_Frame) |
| 188 | + If Not IsObj($oFrame) Then ContinueLoop |
| 189 | + |
185 | 190 | $aFrames[$IDX_Frame][$FRAME_IDX] = $IDX_Frame |
186 | 191 | $aFrames[$IDX_Frame][$FRAME_OBJECT] = $oFrame |
187 | 192 | $aFrames[$IDX_Frame][$FRAME_ID] = $oFrame.FrameId |
188 | 193 | $aFrames[$IDX_Frame][$FRAME_NAME] = $oFrame.Name |
189 | 194 | $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) |
192 | 197 | Next |
193 | 198 | Return $aFrames |
194 | 199 | EndFunc ;==>_NetWebView2_GetAllFrames_AsArray |
195 | 200 |
|
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 | | - |
220 | 201 | Func __Example_Log($s_ScriptLineNumber, $sString, $iError = @error, $iExtended = @extended) |
221 | 202 | ConsoleWrite(@ScriptName & ' SLN=' & $s_ScriptLineNumber & ' [' & $iError & '/' & $iExtended & '] ::: ' & $sString & @CRLF) |
222 | 203 | Return SetError($iError, $iExtended, '') |
223 | 204 | EndFunc ;==>__Example_Log |
224 | 205 |
|
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 | 206 |
|
271 | | - Return SetError(0, Int($fDuration), $vResult) |
272 | | -EndFunc ;==>Fire_And_Wait |
0 commit comments