22#include < GUIConstantsEx.au3>
33#include < WindowsConstants.au3>
44
5+ #include " ..\NetWebView2Lib.au3"
6+
57; ==============================================================================
68; WebView2 Multi-Channel Presentation Script
79; ==============================================================================
810
9- ; Register the exit function
10- OnAutoItExitRegister (" _CleanExit" )
11-
1211; Global objects
13- Global $oManager , $oBridge
14- Global $oEvtManager , $oEvtBridge
15-
16- ; COM Error Handler
17- Global $oMyError = ObjEvent (" AutoIt.Error" , " _ErrFunc" )
1812
1913; GUI & Controls
2014Global $hGUI , $idLabelStatus
2115
2216Main()
2317
2418Func 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 )
19+ Local $oMyError = ObjEvent (" AutoIt.Error" , __NetWebView2_COMErrFunc)
20+ #forceref $oMyError
21+
22+ ; Create the UI
23+ Local $iHeight = 400
24+ $hGUI = GUICreate (" WebView2 .NET Manager - Community Demo" , 800 , $iHeight )
25+ $idLabelStatus = GUICtrlCreateLabel (" Status: Initializing Engine..." , 10 , $iHeight - 20 , 880 , 20 )
2826 GUICtrlSetFont (- 1 , 9 , 400 , 0 , " Segoe UI" )
2927 GUISetState (@SW_SHOW )
3028
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
29+ ; Initialize WebView2 Manager and register events
30+ Local $oWebV2M = _NetWebView2_CreateManager()
31+ $_g_oWeb = $oWebV2M
32+ If @error Then Return SetError (@error , @extended , $oWebV2M )
3733
38- ; 3. Setup Events (Crucial: Define interfaces explicitly)
39- ; Channel 1: Manager Events (INIT_READY, NAV_COMPLETED, etc. )
40- $oEvtManager = ObjEvent ( $oManager , " WebView_ " , " IWebViewEvents " )
34+ ; Initialize JavaScript Bridge
35+ Local $oJSBridge = _NetWebView2_GetBridge( $oWebV2M , " _BridgeMyEventsHandler_ " )
36+ If @error Then Return SetError ( @error , @extended , $oWebV2M )
4137
42- ; Channel 2: JavaScript Bridge (Messages from JS to AutoIt)
43- $oBridge = $oManager .GetBridge()
44- $oEvtBridge = ObjEvent ($oBridge , " Bridge_" , " IBridgeEvents" )
38+ Local $sProfileDirectory = @TempDir & " \NetWebView2Lib-UserDataFolder"
39+ _NetWebView2_Initialize($oWebV2M , $hGUI , $sProfileDirectory , 0 , 0 , 0 , 0 , True , True , True , 1.2 , " 0x2B2B2B" )
4540
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 )
41+ _NetWebView2_NavigateToString($_g_oWeb , __GetDemoHTML())
5042
5143 ; Main Loop
5244 While 1
@@ -57,77 +49,30 @@ Func Main()
5749 WEnd
5850
5951 GUIDelete ($hGUI )
60- EndFunc ; ==>Main
6152
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
53+ _NetWebView2_CleanUp($oWebV2M , $oJSBridge )
54+ EndFunc ; ==>Main
7755
7856; ==============================================================================
7957; ; Function to update a text element inside the WebView UI
8058; ==============================================================================
81- Func UpdateWebUI($sElementId , $sNewText )
59+ Func UpdateWebUI(ByRef $oWeb , $sElementId , $sNewText )
60+ If Not IsObj ($oWeb ) Then Return ' '
61+
8262 ; Escape backslashes, single quotes and handle new lines for JavaScript safety
8363 Local $sCleanText = StringReplace ($sNewText , " \" , " \\" )
8464 $sCleanText = StringReplace ($sCleanText , " '" , " \'" )
8565 $sCleanText = StringReplace ($sCleanText , @CRLF , " \n" )
8666 $sCleanText = StringReplace ($sCleanText , @LF , " \n" )
8767
88- Local $sJS = " document.getElementById('" & $sElementId & " ').innerText = '" & $sCleanText & " ';"
89-
90- If IsObj ($oManager ) Then
91- $oManager .ExecuteScript($sJS )
92- EndIf
68+ Local $sJavaScript = " document.getElementById('" & $sElementId & " ').innerText = '" & $sCleanText & " ';"
69+ _NetWebView2_ExecuteScript($oWeb , $sJavaScript )
9370EndFunc ; ==>UpdateWebUI
9471
9572; ==============================================================================
96- ; EVENT HANDLER: WebView Manager (C# Internal Events )
73+ ; MY EVENT HANDLER: Bridge (JavaScript Messages )
9774; ==============================================================================
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 )
75+ Func _BridgeMyEventsHandler_OnMessageReceived($sMessage )
13176 Local Static $iMsgCnt = 0
13277 ConsoleWrite (" >>> [JS MESSAGE]: " & $sMessage & @CRLF )
13378
@@ -136,14 +81,14 @@ Func Bridge_OnMessageReceived($sMessage)
13681 Else
13782 MsgBox (64 , " JS Notification" , " Message from Browser: " & $sMessage )
13883 $iMsgCnt += 1
139- UpdateWebUI(" mainTitle" , $iMsgCnt & " Hallo from AutoIt!" )
84+ UpdateWebUI($_g_oWeb , " mainTitle" , $iMsgCnt & " Hello from AutoIt!" )
14085 EndIf
141- EndFunc ; ==>Bridge_OnMessageReceived
86+ EndFunc ; ==>_BridgeMyEventsHandler_OnMessageReceived
14287
14388; ==============================================================================
14489; HELPER: Demo HTML Content
14590; ==============================================================================
146- Func _GetDemoHTML ()
91+ Func __GetDemoHTML ()
14792 Local $sH = ' <html><head><style>' & _
14893 ' body { font-family: "Segoe UI", sans-serif; background: #202020; color: white; padding: 40px; text-align: center; }' & _
14994 ' .card { background: #2d2d2d; padding: 20px; border-radius: 8px; border: 1px solid #444; }' & _
@@ -158,21 +103,4 @@ Func _GetDemoHTML()
158103 ' </div>' & _
159104 ' </body></html>'
160105 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
106+ EndFunc ; ==>__GetDemoHTML
0 commit comments