|
| 1 | +#WIP - this Example is imported from 1.5.0 UDF - and is in "WORK IN PROGRESS" state |
| 2 | +#AutoIt3Wrapper_UseX64=y |
| 3 | +#AutoIt3Wrapper_Run_AU3Check=Y |
| 4 | +#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y |
| 5 | +#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 |
| 6 | + |
| 7 | +; 018-BasicFramesDemo.au3 |
| 8 | + |
| 9 | +#include <GUIConstantsEx.au3> |
| 10 | +#include <WindowsConstants.au3> |
| 11 | +#include "..\NetWebView2Lib.au3" |
| 12 | + |
| 13 | +Main() |
| 14 | + |
| 15 | +Func Main() |
| 16 | + ConsoleWrite("! MicrosoftEdgeWebview2 : version check: " & _NetWebView2_IsAlreadyInstalled() & ' ERR=' & @error & ' EXT=' & @extended & @CRLF) |
| 17 | + |
| 18 | + Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc) |
| 19 | + #forceref $oMyError |
| 20 | + |
| 21 | + ; Create the UI |
| 22 | + Local $iHeight = 800 |
| 23 | + Local $hGUI = GUICreate("WebView2 .NET Manager - Community Demo", 1100, $iHeight) |
| 24 | + GUICtrlSetFont(-1, 9, 400, 0, "Segoe UI") |
| 25 | + |
| 26 | + WinMove($hGUI, '', Default, Default, 800, 440) |
| 27 | + GUISetState(@SW_SHOW, $hGUI) |
| 28 | + |
| 29 | + ; Initialize WebView2 Manager and register events |
| 30 | + 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") |
| 32 | + If @error Then Return SetError(@error, @extended, $oWebV2M) |
| 33 | + |
| 34 | + ; Initialize JavaScript Bridge |
| 35 | + Local $oJSBridge = _NetWebView2_GetBridge($oWebV2M, "_BridgeMyEventsHandler_") |
| 36 | + If @error Then Return SetError(@error, @extended, $oWebV2M) |
| 37 | + |
| 38 | + Local $sProfileDirectory = @ScriptDir & "\NetWebView2Lib-UserDataFolder" |
| 39 | + _NetWebView2_Initialize($oWebV2M, $hGUI, $sProfileDirectory, 0, 0, 0, 0, True, True, 1.2, "0x2B2B2B", False) |
| 40 | + If @error Then Return SetError(@error, @extended, $oWebV2M) |
| 41 | + |
| 42 | + __Example_Log(@ScriptLineNumber, "After: _NetWebView2_Initialize()" & @CRLF) |
| 43 | + |
| 44 | + ; navigate to HTML string - full fill the object with your own offline content - without downloading any content |
| 45 | + ConsoleWrite(@CRLF) |
| 46 | + |
| 47 | + __Example_Log(@ScriptLineNumber, "Before: _NetWebView2_NavigateToString()") |
| 48 | + GUISetState(@SW_SHOW, $hGUI) |
| 49 | + WinMove($hGUI, '', Default, Default, 1100, 800) |
| 50 | + |
| 51 | + #COMMENT This example is based on ==> ; https://github.com/Danp2/au3WebDriver/blob/1834e95206bd4a6ef6952c47a1f1192042f98c0b/wd_demo.au3#L588-L732 |
| 52 | + #Region - Testing how to manage frames |
| 53 | + |
| 54 | + _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) |
| 56 | +;~ _Demo_NavigateCheckBanner($sSession, "https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe", '//*[@id="snigel-cmp-framework" and @class="snigel-cmp-framework"]') |
| 57 | + If @error Then Return SetError(@error, @extended) |
| 58 | + |
| 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) |
| 61 | + |
| 62 | + Local $iFrameCount = $oWebV2M.GetFrameCount() |
| 63 | + ConsoleWrite(@CRLF) |
| 64 | + ConsoleWrite("! " & @ScriptLineNumber & " : Frames=" & $iFrameCount & @CRLF) |
| 65 | + For $IDX_Frame = 0 To $iFrameCount - 1 |
| 66 | + ConsoleWrite("- IDX=" & $IDX_Frame & @CRLF) |
| 67 | + ConsoleWrite("- URL=" & $oWebV2M.GetFrameUrl($IDX_Frame) & @CRLF) |
| 68 | + ConsoleWrite("- NAME=" & $oWebV2M.GetFrameName($IDX_Frame) & @CRLF) |
| 69 | + ConsoleWrite(@CRLF) |
| 70 | + Next |
| 71 | + #EndRegion ; Example part 1 - testing NetWebView2Lib new methodes: .GetFrameCount() .GetFrameUrl($IDX_Frame) .GetFrameName($IDX_Frame) |
| 72 | + |
| 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) |
| 75 | + |
| 76 | + ConsoleWrite("! " & @ScriptLineNumber & " : GetFrameUrls() :" & @CRLF & $oWebV2M.GetFrameUrls() & @CRLF) |
| 77 | + ConsoleWrite("! " & @ScriptLineNumber & " : GetFrameNames() :" & @CRLF & $oWebV2M.GetFrameNames() & @CRLF) |
| 78 | + #EndRegion ; Example part 2 - testing NetWebView2Lib new methodes: .GetFrameCount() .GetFrameUrl($IDX_Frame) .GetFrameName($IDX_Frame) |
| 79 | + |
| 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 | + For $IDX_Frame = 0 To $iFrameCount - 1 |
| 83 | + ConsoleWrite(@CRLF & "======================================================" & @CRLF) |
| 84 | + Local $sHtmlSource = Fire_And_Wait($oWebV2M.GetFrameHtmlSource($IDX_Frame), 5000) ; pair with "FRAME_HTML_SOURCE" |
| 85 | + ConsoleWrite("! " & @ScriptLineNumber & " : GetFrameHtmlSource(" & $IDX_Frame & ") :" & @CRLF & $sHtmlSource & @CRLF) |
| 86 | + Next |
| 87 | + ConsoleWrite(@CRLF & "======================================================" & @CRLF) |
| 88 | + ConsoleWrite(@CRLF) |
| 89 | + ConsoleWrite(@CRLF) |
| 90 | + #EndRegion ; Example part 3 - testing NetWebView2Lib new methodes .GetFrameHtmlSource($IDX_Frame) |
| 91 | + |
| 92 | + |
| 93 | + #Region ; Example part 4 - Direct Frame Interaction |
| 94 | + |
| 95 | + #cs NOT SUPPORTED YET |
| 96 | + Local $oFrame0 = $oWebV2M.GetFrame(0) |
| 97 | + Local $oFrame1 = $oWebV2M.GetFrame(1) |
| 98 | + Local $oFrame2 = $oWebV2M.GetFrame(2) |
| 99 | + Local $oFrame3 = $oWebV2M.GetFrame(3) |
| 100 | + #CE NOT SUPPORTED YET |
| 101 | + |
| 102 | + Local $oFrame0 = $oWebV2M.GetFrame(0) |
| 103 | + |
| 104 | + ConsoleWrite("+ Example part 4 - Direct Frame Interaction via COM Object" & @CRLF) |
| 105 | + If IsObj($oFrame0) Then |
| 106 | + |
| 107 | + ConsoleWrite("VarGetType($oFrame0)=" & VarGetType($oFrame0) & @CRLF) |
| 108 | + ConsoleWrite("$oFrame0.Name=" & $oFrame0.Name & @CRLF & @CRLF) |
| 109 | + |
| 110 | + ; Direct script execution in the iframe without involving the central Manager |
| 111 | + $oFrame0.ExecuteScript("document.body.style.backgroundColor = 'red';") |
| 112 | + ConsoleWrite("> Executed background color change on Frame(0)" & @CRLF) |
| 113 | + |
| 114 | + ; Try getting the URL via JS |
| 115 | + Local $sUrl = $oFrame0.ExecuteScriptWithResult("window.location.href") |
| 116 | + ConsoleWrite("> Frame(0) URL via JS: " & $sUrl & @CRLF) |
| 117 | + |
| 118 | + Else |
| 119 | + ConsoleWrite("! Error: $oFrame0 is not a valid COM Object" & @CRLF) |
| 120 | + EndIf |
| 121 | + |
| 122 | + #EndRegion ; Example part 4 - Direct Frame Interaction |
| 123 | + |
| 124 | + |
| 125 | + #EndRegion - Testing how to manage frames |
| 126 | + |
| 127 | + |
| 128 | + ; Main Loop |
| 129 | + While 1 |
| 130 | + Switch GUIGetMsg() |
| 131 | + Case $GUI_EVENT_CLOSE |
| 132 | + ExitLoop |
| 133 | + EndSwitch |
| 134 | + WEnd |
| 135 | + |
| 136 | + GUIDelete($hGUI) |
| 137 | + |
| 138 | + |
| 139 | + _NetWebView2_CleanUp($oWebV2M, $oJSBridge) |
| 140 | +EndFunc ;==>Main |
| 141 | + |
| 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 |
| 163 | + |
| 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 |
| 172 | + |
| 173 | +; ============================================================================== |
| 174 | +; MyHook_ Events |
| 175 | +; ============================================================================== |
| 176 | +Func MyHook_OnMessageReceived($oWebV2M, $hGUI, $sMsg) |
| 177 | + #forceref $oWebV2M, $hGUI, $sMsg |
| 178 | + ConsoleWrite("> [MyHook] OnMessageReceived: GUI:" & $hGUI & " Msg: " & (StringLen($sMsg) > 30 ? StringLeft($sMsg, 30) & "..." : $sMsg) & @CRLF) |
| 179 | + Local $iSplitPos = StringInStr($sMsg, "|") |
| 180 | + Local $sCommand = $iSplitPos ? StringStripWS(StringLeft($sMsg, $iSplitPos - 1), 3) : $sMsg |
| 181 | + Local $sData = $iSplitPos ? StringTrimLeft($sMsg, $iSplitPos) : "" |
| 182 | +;~ Local $aParts |
| 183 | + |
| 184 | + Switch $sCommand |
| 185 | + Case "INIT_READY" |
| 186 | + |
| 187 | + Case "FRAME_HTML_SOURCE" |
| 188 | + $iSplitPos = StringInStr($sData, "|") |
| 189 | + Local $sIDX = StringLeft($sData, $iSplitPos - 1) |
| 190 | + ConsoleWrite(" >> $sIDX=" & $sIDX & @CRLF) |
| 191 | + Local $sHtmlSource = StringTrimLeft($sData, $iSplitPos) |
| 192 | + If $sHtmlSource = "null" Then $sHtmlSource = "!! <Inaccessible>" |
| 193 | + Fire_And_Wait($sHtmlSource) |
| 194 | + EndSwitch |
| 195 | +EndFunc ;==>MyHook_OnMessageReceived |
| 196 | + |
| 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 | + |
| 218 | +Func __Example_Log($s_ScriptLineNumber, $sString, $iError = @error, $iExtended = @extended) |
| 219 | + ConsoleWrite(@ScriptName & ' SLN=' & $s_ScriptLineNumber & ' [' & $iError & '/' & $iExtended & '] ::: ' & $sString & @CRLF) |
| 220 | + Return SetError($iError, $iExtended, '') |
| 221 | +EndFunc ;==>__Example_Log |
| 222 | + |
| 223 | +; #FUNCTION# ==================================================================================================================== |
| 224 | +; Name...........: Fire_And_Wait |
| 225 | +; Description....: Synchronizes asynchronous events by waiting for a response or a timeout. |
| 226 | +; Syntax.........: Fire_And_Wait([$sData = "" [, $iTimeout = 5000]]) |
| 227 | +; Parameters.....: $sData - [Optional] Data string. |
| 228 | +; If provided: Acts as a "Signal" (setter) from the Event Handler. |
| 229 | +; If empty: Acts as a "Listener" (getter) from the Main Script. |
| 230 | +; $iTimeout - [Optional] Maximum wait time in milliseconds (Default is 5000ms). |
| 231 | +; Return values..: Success - Returns the stored data string. |
| 232 | +; Sets @extended to the duration of the wait in ms. |
| 233 | +; Failure - Returns an empty string and sets @error: |
| 234 | +; |1 - Timeout reached. |
| 235 | +; Author.........: YourName |
| 236 | +; Modified.......: 2026-02-23 |
| 237 | +; Remarks........: This function uses static variables to bridge the gap between async COM events and sync script execution. |
| 238 | +; It effectively pauses the script execution until the WebView2 event fires back with data. |
| 239 | +; =============================================================================================================================== |
| 240 | +Func Fire_And_Wait($sData = "", $iTimeout = 5000) |
| 241 | + Local Static $vStoredData = "" |
| 242 | + Local Static $hJobTimer = 0 |
| 243 | + |
| 244 | + ; === Part A: Response (From Event Handler) === |
| 245 | + If $sData <> "" Then |
| 246 | + $vStoredData = $sData |
| 247 | + Return True |
| 248 | + EndIf |
| 249 | + |
| 250 | + ; === Part B: Fire and Wait (From Main Script) === |
| 251 | + $vStoredData = "" |
| 252 | + $hJobTimer = TimerInit() |
| 253 | + |
| 254 | + While $vStoredData = "" |
| 255 | + If TimerDiff($hJobTimer) > $iTimeout Then |
| 256 | + ConsoleWrite("! Fire_And_Wait | TIMEOUT after " & Round(TimerDiff($hJobTimer), 2) & " ms" & @CRLF) |
| 257 | + Return SetError(1, 0, "") |
| 258 | + EndIf |
| 259 | + Sleep(10) |
| 260 | + WEnd |
| 261 | + |
| 262 | + Local $fDuration = TimerDiff($hJobTimer) |
| 263 | + Local $vResult = $vStoredData |
| 264 | + $vStoredData = "" ; Reset for next use |
| 265 | + |
| 266 | + ConsoleWrite("> Fire_And_Wait | Duration: " & Round($fDuration, 0) & " ms | Status: SUCCESS" & @CRLF) |
| 267 | + |
| 268 | + Return SetError(0, Int($fDuration), $vResult) |
| 269 | +EndFunc ;==>Fire_And_Wait |
0 commit comments