Skip to content

Commit 32dbd86

Browse files
authored
Merge pull request #113 from ioa747/updating---frame-examples
Updating frame examples
2 parents d24323b + a521271 commit 32dbd86

3 files changed

Lines changed: 54 additions & 144 deletions

File tree

NetWebView2Lib.au3

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#Tidy_Parameters=/tcb=-1
88

9-
; NetWebView2Lib.au3 - Script Version: 2026.2.23.9 🚩
9+
; NetWebView2Lib.au3 - Script Version: 2026.2.25.11 🚩
1010

1111
#include <Array.au3>
1212
#include <GUIConstantsEx.au3>
@@ -1030,6 +1030,46 @@ Volatile Func _NetWebView2_SilentErrorHandler($oError)
10301030
$oError = 0 ; Explicitly release the COM reference inside the volatile scopeEndFunc
10311031
EndFunc ;==>_NetWebView2_SilentErrorHandler
10321032

1033+
; #FUNCTION# ====================================================================================================================
1034+
; Name...........: _WebView2_FrameGetHtmlSource
1035+
; Description....: Synchronously retrieves the full HTML source of a frame.
1036+
; Syntax.........: _WebView2_FrameGetHtmlSource($oFrame)
1037+
; Parameters.....: $oFrame - The WebView2Frame object.
1038+
; Return values..: Success - Clean HTML string.
1039+
; Failure - Sets @error and returns empty string.
1040+
; Author ........: ioa747
1041+
; Modified ......:
1042+
; Remarks .......:
1043+
; Related .......:
1044+
; Link ..........:
1045+
; Example .......: No
1046+
; ===============================================================================================================================
1047+
Func _WebView2_FrameGetHtmlSource($oFrame)
1048+
If Not IsObj($oFrame) Then Return SetError(1, 0, "")
1049+
1050+
; Execute script synchronously
1051+
Local $sRaw = $oFrame.ExecuteScriptWithResult("document.documentElement.outerHTML")
1052+
1053+
; Basic validation
1054+
If $sRaw = "null" Or $sRaw = "" Then Return ""
1055+
If StringLeft($sRaw, 6) = "ERROR:" Then Return SetError(2, 0, "")
1056+
1057+
; Pre-process: Strip the mandatory JSON quotes BEFORE unescaping.
1058+
; This prevents the C# Parser from "double-wrapping" the string.
1059+
If StringLeft($sRaw, 1) = '"' And StringRight($sRaw, 1) = '"' Then
1060+
$sRaw = StringMid($sRaw, 2, StringLen($sRaw) - 2)
1061+
EndIf
1062+
1063+
; Initialize Parser from the library
1064+
Local $oJson = _NetJson_CreateParser()
1065+
If @error Then Return SetError(3, 0, "")
1066+
1067+
; Use the Parser's UnescapeString to handle all escapes (\uXXXX, \n, \", etc.)
1068+
Local $sClean = $oJson.UnescapeString($sRaw)
1069+
1070+
Return $sClean
1071+
EndFunc ;==>_WebView2_FrameGetHtmlSource
1072+
10331073
#EndRegion ; New Core Method Wrappers
10341074

10351075
#Region ; NetWebView2Lib UDF - _NetJson_* functions
@@ -2354,3 +2394,5 @@ EndFunc ;==>__NetWebView2_Events__OnFrameWebMessageReceived
23542394

23552395
#EndRegion ; === NetWebView2Lib UDF === EVENT HANDLERS ===
23562396

2397+
2398+

examples/006-DownloadDemo.au3

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -148,69 +148,3 @@ Volatile Func __UserEventHandler__OnAcceleratorKeyPressed($oWebV2M, $hGUI, $oArg
148148

149149
$oArgs = 0 ; Explicitly release the COM reference inside the volatile scope
150150
EndFunc ;==>__UserEventHandler__OnAcceleratorKeyPressed
151-
152-
; Advise using 'Volatile' for Event Handlers to ensure the WebView2 COM thread can interrupt the main script safely.
153-
Volatile Func __UserEventHandler__OnAcceleratorKeyPressed00($oWebV2M, $hGUI, $oArgs)
154-
Local Const $sArgsList = '[VirtualKey=' & $oArgs.VirtualKey & _ ; The VK code of the key.
155-
'; KeyEventKind=' & $oArgs.KeyEventKind & _ ; Type of key event (Down, Up, etc.).
156-
'; Handled=' & $oArgs.Handled & _ ; Set to `True` to stop the browser from processing the key.
157-
'; RepeatCount=' & $oArgs.RepeatCount & _ ; The number of times the key has repeated.
158-
'; ScanCode=' & $oArgs.ScanCode & _ ; Hardware scan code.
159-
'; IsExtendedKey=' & $oArgs.IsExtendedKey & _ ; True if it's an extended key (e.g., right Alt).
160-
'; IsMenuKeyDown=' & $oArgs.IsMenuKeyDown & _ ; True if Alt is pressed.
161-
'; WasKeyDown=' & $oArgs.WasKeyDown & _ ; True if the key was already down.
162-
'; IsKeyReleased=' & $oArgs.IsKeyReleased & _ ; True if the event is a key up.
163-
'; KeyEventLParam=' & $oArgs.KeyEventLParam & ']' ; Gets the LPARAM value that accompanied the window message.
164-
165-
166-
Local Const $s_Prefix = "[USER:EVENT: OnAcceleratorKeyPressed]:: GUI:" & $hGUI & " ARGS: " & ((IsObj($oArgs)) ? ($sArgsList) : ('ERRROR'))
167-
168-
;~ public uint VirtualKey { get; }
169-
;~ public int KeyEventLParam { get; }
170-
;~ public int KeyEventKind { get; }
171-
;~
172-
;~ public uint RepeatCount { get; }
173-
;~ public uint ScanCode { get; }
174-
;~ public bool IsExtendedKey { get; }
175-
;~ public bool IsMenuKeyDown { get; }
176-
;~ public bool WasKeyDown { get; }
177-
;~ public bool IsKeyReleased { get; }
178-
179-
;~ https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2acceleratorkeypressedeventargs?view=webview2-dotnet-1.0.705.50
180-
;~ ConsoleWrite($oArgs.Handled & @CRLF) ; Indicates whether the AcceleratorKeyPressed event is handled by host.
181-
;~ ConsoleWrite($oArgs.KeyEventKind & @CRLF) ; Gets the key event kind that caused the event to run
182-
;~ ConsoleWrite($oArgs.KeyEventLParam & @CRLF) ; Gets the LPARAM value that accompanied the window message.
183-
;~ ConsoleWrite('>> PhysicalKeyStatus=' & $oArgs.PhysicalKeyStatus & @CRLF) ; Gets a CoreWebView2PhysicalKeyStatus representing the information passed in the LPARAM of the window message. ==> ; https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2physicalkeystatus?view=webview2-dotnet-1.0.705.50
184-
;~ ConsoleWrite($oArgs.VirtualKey & @CRLF) ; Gets the Win32 virtual key code of the key that was pressed or released.
185-
186-
;~ If $oArgs.VirtualKey = 27 Then ; ESC 27 1b 033 Escape, next character is not echoed ; https://www.autoitscript.com/autoit3/docs/appendix/ascii.htm
187-
;~ $oWebV2M.CancelDownloads($_sURLDownload_InProgress)
188-
;~ EndIf
189-
190-
; Example of checking specific keys
191-
Switch $oArgs.VirtualKey
192-
Case Int($VK_F7)
193-
ConsoleWrite("! Blocked: F7 caret browsing." & @CRLF)
194-
$oArgs.Handled = True
195-
196-
Case Int($VK_N)
197-
; Check if Ctrl is pressed (using IsMenuKeyDown if it concerns Alt or WinAPI for Ctrl)
198-
; If we want to block Ctrl+N (New Window)
199-
If _IsPressed("11") Then ; Need Misc.au3 for _IsPressed
200-
ConsoleWrite("! Blocked: Ctrl+N shortcut." & @CRLF)
201-
$oArgs.Handled = True
202-
203-
EndIf
204-
205-
Case Int($VK_ESCAPE) ; ESC 27 1b 033 Escape, next character is not echoed ; https://www.autoitscript.com/autoit3/docs/appendix/ascii.htm
206-
; Cancels active downloads. If `uri` is empty or omitted, cancels all active downloads.
207-
$oWebV2M.CancelDownloads()
208-
209-
EndSwitch
210-
211-
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix, 0)
212-
$oArgs = 0 ; Explicitly release the COM reference inside the volatile scopeEndFunc
213-
214-
EndFunc ;==>__UserEventHandler__OnAcceleratorKeyPressed00
215-
216-

examples/018-BasicFramesDemo.au3

Lines changed: 11 additions & 77 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, $sHtmlSource
8484
For $IDX_Frame = 0 To $iFrameCount - 1
85+
$oFrame = $oWebV2M.GetFrame($IDX_Frame)
86+
$sHtmlSource = _WebView2_FrameGetHtmlSource($oFrame)
87+
If @error Then ContinueLoop
8588
ConsoleWrite(@CRLF & "======================================================" & @CRLF)
86-
Local $sHtmlSource = Fire_And_Wait($oWebV2M.GetFrameHtmlSource($IDX_Frame), 5000) ; pair with "FRAME_HTML_SOURCE"
8789
ConsoleWrite("! " & @ScriptLineNumber & " : GetFrameHtmlSource(" & $IDX_Frame & ") :" & @CRLF & $sHtmlSource & @CRLF)
8890
Next
8991
ConsoleWrite(@CRLF & "======================================================" & @CRLF)
@@ -181,92 +183,24 @@ 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
224205

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)
270206

271-
Return SetError(0, Int($fDuration), $vResult)
272-
EndFunc ;==>Fire_And_Wait

0 commit comments

Comments
 (0)