Skip to content

Commit a3fa9cb

Browse files
authored
Initial Release of NetWebView2Lib v1.3.0
1 parent ced89bf commit a3fa9cb

96 files changed

Lines changed: 99586 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/0-NetJson.Parser.au3

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
;~ #AutoIt3Wrapper_UseX64=y
2+
3+
; NetWebView2Lib.JsonParser - Tutorial Script
4+
5+
#include <MsgBoxConstants.au3>
6+
7+
; Global objects handler for COM objects
8+
Global $oMyError = ObjEvent("AutoIt.Error", "_ErrFunc")
9+
10+
; Initialize the COM Object
11+
Local $oJson = ObjCreate("NetJson.Parser")
12+
If Not IsObj($oJson) Then
13+
MsgBox(16, "Error", "Could not create NetWebView2Lib.JsonParser. Make sure the DLL is registered.")
14+
Exit
15+
EndIf
16+
17+
ConsoleWrite("=== STARTING NETJSON TUTORIAL ===" & @CRLF)
18+
19+
; ---------------------------------------------------------
20+
ConsoleWrite("+> 1. PARSING & BASICS <+" & @CRLF)
21+
; ---------------------------------------------------------
22+
Local $sRaw = '{"user": "John", "roles": ["Admin", "Tester"], "active": true}'
23+
$oJson.Parse($sRaw)
24+
ConsoleWrite("Full JSON: " & $oJson.GetJson() & @CRLF)
25+
26+
; Check if a path exists
27+
If $oJson.Exists("user") Then
28+
ConsoleWrite("- User exists: " & $oJson.GetTokenValue("user") & @CRLF)
29+
EndIf
30+
31+
; ---------------------------------------------------------
32+
ConsoleWrite("+> 2. ARRAY OPERATIONS <+" & @CRLF)
33+
; ---------------------------------------------------------
34+
; Get length of the 'roles' array
35+
Local $iRolesCount = $oJson.GetArrayLength("roles")
36+
ConsoleWrite("- Roles count: " & $iRolesCount & @CRLF)
37+
38+
; Get specific element from array
39+
ConsoleWrite("- First role: " & $oJson.GetTokenValue("roles[0]") & @CRLF)
40+
41+
; ---------------------------------------------------------
42+
ConsoleWrite("+> 2b. DEEP PATH NOTATION (The Power of JSON Path) <+" & @CRLF)
43+
; ---------------------------------------------------------
44+
Local $sComplex = '{"store": {"book": [{"title": "Coding 101", "price": 10}, {"title": "AutoIt Guru", "price": 25}], "location": "Athens"}}'
45+
$oJson.Parse($sComplex)
46+
47+
; Direct access to a deep value using dots and brackets
48+
; Path: store -> book -> second element [1] -> title
49+
Local $sTitle = $oJson.GetTokenValue("store.book[1].title")
50+
Local $iPrice = $oJson.GetTokenValue("store.book[1].price")
51+
52+
ConsoleWrite("- Deep Search (Title): " & $sTitle & @CRLF)
53+
ConsoleWrite("- Deep Search (Price): " & $iPrice & @CRLF)
54+
55+
; Check existence of a deep path
56+
If $oJson.Exists("store.location") Then
57+
ConsoleWrite("- Store Location: " & $oJson.GetTokenValue("store.location") & @CRLF)
58+
EndIf
59+
60+
; ---------------------------------------------------------
61+
ConsoleWrite("+> 3. MODIFICATION (SetTokenValue) <+" & @CRLF)
62+
; ---------------------------------------------------------
63+
$oJson.SetTokenValue("user", "George")
64+
$oJson.SetTokenValue("active", "false") ; Note: Values are sent as strings
65+
ConsoleWrite("- Updated User: " & $oJson.GetTokenValue("user") & @CRLF)
66+
67+
; ---------------------------------------------------------
68+
ConsoleWrite("+> 4. FILE I/O <+" & @CRLF)
69+
; ---------------------------------------------------------
70+
; Save current state to a file
71+
$oJson.SaveToFile(@ScriptDir & "\settings.json")
72+
ConsoleWrite("JSON saved to file." & @CRLF)
73+
74+
; Clear and Reload from file
75+
$oJson.Clear()
76+
ConsoleWrite("- After Clear, Json is: " & $oJson.GetJson() & @CRLF)
77+
78+
$oJson.LoadFromFile(@ScriptDir & "\settings.json")
79+
ConsoleWrite("- Reloaded from file, User is: " & $oJson.GetTokenValue("user") & @CRLF)
80+
81+
; ---------------------------------------------------------
82+
ConsoleWrite("+> 5. FORMATTING (Pretty vs Minified) <+" & @CRLF)
83+
; ---------------------------------------------------------
84+
ConsoleWrite(@CRLF & "--- PRETTY JSON ---" & @CRLF)
85+
ConsoleWrite($oJson.GetPrettyJson() & @CRLF)
86+
87+
ConsoleWrite(@CRLF & "--- MINIFIED JSON ---" & @CRLF)
88+
ConsoleWrite($oJson.GetMinifiedJson() & @CRLF)
89+
90+
; ---------------------------------------------------------
91+
ConsoleWrite("+> 6. ESCAPING TOOLS (Utility Methods) <+" & @CRLF)
92+
; ---------------------------------------------------------
93+
Local $sDirtyString = 'Hello "World" \ Name'
94+
Local $sEscaped = $oJson.EscapeString($sDirtyString)
95+
ConsoleWrite("- Escaped: " & $sEscaped & @CRLF)
96+
ConsoleWrite("- Unescaped: " & $oJson.UnescapeString($sEscaped) & @CRLF)
97+
98+
ConsoleWrite("--- TUTORIAL COMPLETED ---" & @CRLF)
99+
100+
; Clean up object
101+
$oJson = Null
102+
103+
; ---------------------------------------------------------
104+
105+
Func _ErrFunc($oError) ; User's COM error function. Will be called if COM error occurs
106+
; Do anything here.
107+
ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
108+
@TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
109+
@TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
110+
@TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
111+
@TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
112+
@TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
113+
@TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
114+
@TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
115+
@TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
116+
@TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
117+
EndFunc ;==>_ErrFunc

examples/1-BasicDemo.au3

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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

Comments
 (0)