|
| 1 | +#AutoIt3Wrapper_UseX64=y |
| 2 | +#include <GUIConstantsEx.au3> |
| 3 | +#include <WindowsConstants.au3> |
| 4 | + |
| 5 | +; ============================================================================== |
| 6 | +; WebView2 Multi-Channel Presentation Script |
| 7 | +; ============================================================================== |
| 8 | + |
| 9 | +; Register the exit function |
| 10 | +OnAutoItExitRegister("_CleanExit") |
| 11 | + |
| 12 | +; Global objects |
| 13 | +Global $oManager, $oBridge |
| 14 | +Global $oEvtManager, $oEvtBridge |
| 15 | + |
| 16 | +; COM Error Handler |
| 17 | +Global $oMyError = ObjEvent("AutoIt.Error", "_ErrFunc") |
| 18 | + |
| 19 | +; GUI & Controls |
| 20 | +Global $hGUI, $idLabelStatus |
| 21 | + |
| 22 | +Main() |
| 23 | + |
| 24 | +Func Main() |
| 25 | + ; 1. Create the UI |
| 26 | + $hGUI = GUICreate("WebView2 .NET Manager - Community Demo", 900, 650) |
| 27 | + $idLabelStatus = GUICtrlCreateLabel("Status: Initializing Engine...", 10, 620, 880, 20) |
| 28 | + GUICtrlSetFont(-1, 9, 400, 0, "Segoe UI") |
| 29 | + GUISetState(@SW_SHOW) |
| 30 | + |
| 31 | + ; 2. Instantiate the .NET Manager |
| 32 | + $oManager = ObjCreate("NetWebView2.Manager") |
| 33 | + If Not IsObj($oManager) Then |
| 34 | + MsgBox(16, "Error", "Could not create WebView2 Manager. Please register the DLL.") |
| 35 | + Exit |
| 36 | + EndIf |
| 37 | + |
| 38 | + ; 3. Setup Events (Crucial: Define interfaces explicitly) |
| 39 | + ; Channel 1: Manager Events (INIT_READY, NAV_COMPLETED, etc.) |
| 40 | + $oEvtManager = ObjEvent($oManager, "WebView_", "IWebViewEvents") |
| 41 | + |
| 42 | + ; Channel 2: JavaScript Bridge (Messages from JS to AutoIt) |
| 43 | + $oBridge = $oManager.GetBridge() |
| 44 | + $oEvtBridge = ObjEvent($oBridge, "Bridge_", "IBridgeEvents") |
| 45 | + |
| 46 | + ; 4. Initialize the Browser |
| 47 | + ; We pass an empty string for the URL to prevent "ConnectionAborted" |
| 48 | + ; and wait for our INIT_READY signal. |
| 49 | + $oManager.Initialize(($hGUI), "", 10, 10, 880, 600) |
| 50 | + |
| 51 | + ; Main Loop |
| 52 | + While 1 |
| 53 | + Switch GUIGetMsg() |
| 54 | + Case $GUI_EVENT_CLOSE |
| 55 | + ExitLoop |
| 56 | + EndSwitch |
| 57 | + WEnd |
| 58 | + |
| 59 | + GUIDelete($hGUI) |
| 60 | +EndFunc ;==>Main |
| 61 | + |
| 62 | +Func _CleanExit() |
| 63 | + ; Check if the object exists before calling methods to avoid COM errors during crash |
| 64 | + If IsObj($oManager) Then |
| 65 | + $oManager.Cleanup() |
| 66 | + EndIf |
| 67 | + |
| 68 | + ; Release the event sinks |
| 69 | + $oManager = 0 |
| 70 | + $oBridge = 0 |
| 71 | + $oEvtManager = 0 |
| 72 | + $oEvtBridge = 0 |
| 73 | + $oMyError = 0 |
| 74 | + |
| 75 | + ConsoleWrite(">>> Application exited cleanly." & @CRLF) |
| 76 | +EndFunc ;==>_CleanExit |
| 77 | + |
| 78 | +; ============================================================================== |
| 79 | +; ; Function to update a text element inside the WebView UI |
| 80 | +; ============================================================================== |
| 81 | +Func UpdateWebUI($sElementId, $sNewText) |
| 82 | + ; Escape backslashes, single quotes and handle new lines for JavaScript safety |
| 83 | + Local $sCleanText = StringReplace($sNewText, "\", "\\") |
| 84 | + $sCleanText = StringReplace($sCleanText, "'", "\'") |
| 85 | + $sCleanText = StringReplace($sCleanText, @CRLF, "\n") |
| 86 | + $sCleanText = StringReplace($sCleanText, @LF, "\n") |
| 87 | + |
| 88 | + Local $sJS = "document.getElementById('" & $sElementId & "').innerText = '" & $sCleanText & "';" |
| 89 | + |
| 90 | + If IsObj($oManager) Then |
| 91 | + $oManager.ExecuteScript($sJS) |
| 92 | + EndIf |
| 93 | +EndFunc ;==>UpdateWebUI |
| 94 | + |
| 95 | +; ============================================================================== |
| 96 | +; EVENT HANDLER: WebView Manager (C# Internal Events) |
| 97 | +; ============================================================================== |
| 98 | +Func WebView_OnMessageReceived($sMessage) |
| 99 | + ConsoleWrite(">>> [CORE EVENT]: " & $sMessage & @CRLF) |
| 100 | + |
| 101 | + ; Separating messages that have parameters (e.g. TITLE_CHANGED|...) |
| 102 | + Local $aParts = StringSplit($sMessage, "|") |
| 103 | + Local $sCommand = StringStripWS($aParts[1], 3) |
| 104 | + |
| 105 | + Switch $sCommand |
| 106 | + Case "INIT_READY" |
| 107 | + GUICtrlSetData($idLabelStatus, "Status: Engine Ready. Loading HTML UI...") |
| 108 | + $oManager.NavigateToString(_GetDemoHTML()) |
| 109 | + |
| 110 | + Case "NAV_STARTING" |
| 111 | + GUICtrlSetData($idLabelStatus, "Status: Navigation started...") |
| 112 | + |
| 113 | + Case "NAV_COMPLETED" |
| 114 | + GUICtrlSetData($idLabelStatus, "Status: Application Ready.") |
| 115 | + |
| 116 | + Case "TITLE_CHANGED" |
| 117 | + ; If you want to change the title of your GUI based on the page |
| 118 | + If $aParts[0] > 1 Then WinSetTitle($hGUI, "", "WebView2 - " & $aParts[2]) |
| 119 | + |
| 120 | + Case "ERROR", "NAV_ERROR" |
| 121 | + Local $sErr = ($aParts[0] > 1) ? $aParts[2] : "Unknown" |
| 122 | + GUICtrlSetData($idLabelStatus, "Status: Error " & $sErr) |
| 123 | + MsgBox(16, "WebView2 Error", $sMessage) |
| 124 | + EndSwitch |
| 125 | +EndFunc ;==>WebView_OnMessageReceived |
| 126 | + |
| 127 | +; ============================================================================== |
| 128 | +; EVENT HANDLER: Bridge (JavaScript Messages) |
| 129 | +; ============================================================================== |
| 130 | +Func Bridge_OnMessageReceived($sMessage) |
| 131 | + Local Static $iMsgCnt = 0 |
| 132 | + ConsoleWrite(">>> [JS MESSAGE]: " & $sMessage & @CRLF) |
| 133 | + |
| 134 | + If $sMessage = "CLOSE_APP" Then |
| 135 | + If MsgBox(36, "Confirm", "Exit Application?", 0, $hGUI) = 6 Then Exit |
| 136 | + Else |
| 137 | + MsgBox(64, "JS Notification", "Message from Browser: " & $sMessage) |
| 138 | + $iMsgCnt += 1 |
| 139 | + UpdateWebUI("mainTitle", $iMsgCnt & " Hallo from AutoIt!") |
| 140 | + EndIf |
| 141 | +EndFunc ;==>Bridge_OnMessageReceived |
| 142 | + |
| 143 | +; ============================================================================== |
| 144 | +; HELPER: Demo HTML Content |
| 145 | +; ============================================================================== |
| 146 | +Func _GetDemoHTML() |
| 147 | + Local $sH = '<html><head><style>' & _ |
| 148 | + 'body { font-family: "Segoe UI", sans-serif; background: #202020; color: white; padding: 40px; text-align: center; }' & _ |
| 149 | + '.card { background: #2d2d2d; padding: 20px; border-radius: 8px; border: 1px solid #444; }' & _ |
| 150 | + 'button { padding: 12px 24px; cursor: pointer; background: #0078d4; color: white; border: none; border-radius: 4px; font-size: 16px; margin: 5px; }' & _ |
| 151 | + 'button:hover { background: #005a9e; }' & _ |
| 152 | + '</style></head><body>' & _ |
| 153 | + '<div class="card">' & _ |
| 154 | + ' <h1 id="mainTitle">WebView2 + AutoIt .NET Manager</h1>' & _ ; Fixed ID attribute |
| 155 | + ' <p id="statusMsg">The communication is now 100% Event-Driven (No Sleep needed).</p>' & _ |
| 156 | + ' <button onclick="window.chrome.webview.postMessage(''Hello from JavaScript!'')">Send Ping</button>' & _ |
| 157 | + ' <button onclick="window.chrome.webview.postMessage(''CLOSE_APP'')">Exit App</button>' & _ |
| 158 | + '</div>' & _ |
| 159 | + '</body></html>' |
| 160 | + Return $sH |
| 161 | +EndFunc ;==>_GetDemoHTML |
| 162 | + |
| 163 | +; ============================================================================== |
| 164 | +; COM ERROR HANDLER |
| 165 | +; ============================================================================== |
| 166 | +Func _ErrFunc($oError) |
| 167 | + ; Do anything here. |
| 168 | + ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _ |
| 169 | + @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _ |
| 170 | + @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _ |
| 171 | + @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _ |
| 172 | + @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _ |
| 173 | + @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _ |
| 174 | + @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _ |
| 175 | + @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _ |
| 176 | + @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _ |
| 177 | + @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF) |
| 178 | +EndFunc ;==>_ErrFunc |
0 commit comments