Skip to content

Commit 322f9a2

Browse files
authored
v2.0.0-beta.3
1 parent 85ef3a1 commit 322f9a2

17 files changed

Lines changed: 3017 additions & 411 deletions

NetWebView2Lib.au3

Lines changed: 260 additions & 120 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 105 additions & 72 deletions
Large diffs are not rendered by default.

bin/NetWebView2Lib.dll

7 KB
Binary file not shown.

bin/NetWebView2Lib.tlb

3.17 KB
Binary file not shown.

examples/000-NetJson.Parser.au3

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
;~ #AutoIt3Wrapper_UseX64=y
2+
3+
; NetWebView2Lib.JsonParser - Tutorial Script
4+
5+
#include <Array.au3>
6+
#include <MsgBoxConstants.au3>
7+
8+
; Global objects handler for COM objects
9+
Global $oMyError = ObjEvent("AutoIt.Error", "_ErrFunc")
10+
11+
; Initialize the COM Object
12+
Local $oJson = ObjCreate("NetJson.Parser")
13+
If Not IsObj($oJson) Then
14+
MsgBox(16, "Error", "Could not create NetWebView2Lib.JsonParser. Make sure the DLL is registered.")
15+
Exit
16+
EndIf
17+
18+
ConsoleWrite(@CRLF & "=== STARTING NETJSON TUTORIAL ===" & @CRLF)
19+
20+
; ---------------------------------------------------------
21+
ConsoleWrite(@CRLF & "+> 1. PARSING & BASICS <+" & @CRLF)
22+
; ---------------------------------------------------------
23+
Local $sRaw = '{"user": "John", "roles": ["Admin", "Tester"], "active": true}'
24+
$oJson.Parse($sRaw)
25+
ConsoleWrite("Full JSON: " & $oJson.GetJson() & @CRLF)
26+
27+
; Check if a path exists
28+
If $oJson.Exists("user") Then
29+
ConsoleWrite("- User exists: " & $oJson.GetTokenValue("user") & @CRLF)
30+
EndIf
31+
32+
; ---------------------------------------------------------
33+
ConsoleWrite(@CRLF & "+> 2. ARRAY OPERATIONS <+" & @CRLF)
34+
; ---------------------------------------------------------
35+
; Get length of the 'roles' array
36+
Local $iRolesCount = $oJson.GetArrayLength("roles")
37+
ConsoleWrite("- Roles count: " & $iRolesCount & @CRLF)
38+
39+
; Get specific element from array
40+
ConsoleWrite("- First role: " & $oJson.GetTokenValue("roles[0]") & @CRLF)
41+
42+
; ---------------------------------------------------------
43+
ConsoleWrite(@CRLF & "+> 2b. DEEP PATH NOTATION (The Power of JSON Path) <+" & @CRLF)
44+
; ---------------------------------------------------------
45+
Local $sComplex = '{"store": {"book": [{"title": "Coding 101", "price": 10}, {"title": "AutoIt Guru", "price": 25}], "location": "Athens"}}'
46+
$oJson.Parse($sComplex)
47+
48+
; Direct access to a deep value using dots and brackets
49+
; Path: store -> book -> second element [1] -> title
50+
Local $sTitle = $oJson.GetTokenValue("store.book[1].title")
51+
Local $iPrice = $oJson.GetTokenValue("store.book[1].price")
52+
53+
ConsoleWrite("- Deep Search (Title): " & $sTitle & @CRLF)
54+
ConsoleWrite("- Deep Search (Price): " & $iPrice & @CRLF)
55+
56+
; Check existence of a deep path
57+
If $oJson.Exists("store.location") Then
58+
ConsoleWrite("- Store Location: " & $oJson.GetTokenValue("store.location") & @CRLF)
59+
EndIf
60+
61+
; ---------------------------------------------------------
62+
ConsoleWrite(@CRLF & "+> 3. MODIFICATION (SetTokenValue) <+" & @CRLF)
63+
; ---------------------------------------------------------
64+
$oJson.SetTokenValue("user", "George")
65+
$oJson.SetTokenValue("active", "false") ; Note: Values are sent as strings
66+
ConsoleWrite("- Updated User: " & $oJson.GetTokenValue("user") & @CRLF)
67+
68+
; ---------------------------------------------------------
69+
ConsoleWrite(@CRLF & "+> 4. FILE I/O <+" & @CRLF)
70+
; ---------------------------------------------------------
71+
; Save current state to a file
72+
$oJson.SaveToFile(@ScriptDir & "\settings.json")
73+
ConsoleWrite("JSON saved to file." & @CRLF)
74+
75+
; Clear and Reload from file
76+
$oJson.Clear()
77+
ConsoleWrite("- After Clear, Json is: " & $oJson.GetJson() & @CRLF)
78+
79+
$oJson.LoadFromFile(@ScriptDir & "\settings.json")
80+
ConsoleWrite("- Reloaded from file, User is: " & $oJson.GetTokenValue("user") & @CRLF)
81+
82+
; ---------------------------------------------------------
83+
ConsoleWrite(@CRLF & "+> 5. FORMATTING (Pretty vs Minified) <+" & @CRLF)
84+
; ---------------------------------------------------------
85+
ConsoleWrite(@CRLF & "--- PRETTY JSON ---" & @CRLF)
86+
ConsoleWrite($oJson.GetPrettyJson() & @CRLF)
87+
88+
ConsoleWrite(@CRLF & "--- MINIFIED JSON ---" & @CRLF)
89+
ConsoleWrite($oJson.GetMinifiedJson() & @CRLF)
90+
91+
; ---------------------------------------------------------
92+
ConsoleWrite(@CRLF & "+> 6. ESCAPING TOOLS (Utility Methods) <+" & @CRLF)
93+
; ---------------------------------------------------------
94+
Local $sDirtyString = 'Hello "World" \ Name'
95+
Local $sEscaped = $oJson.EscapeString($sDirtyString)
96+
ConsoleWrite("- Escaped: " & $sEscaped & @CRLF)
97+
ConsoleWrite("- Unescaped: " & $oJson.UnescapeString($sEscaped) & @CRLF)
98+
99+
; ---------------------------------------------------------
100+
ConsoleWrite(@CRLF & "+> 7. ADVANCED DATA INTELLIGENCE (v1.4.1) <+" & @CRLF)
101+
; ---------------------------------------------------------
102+
103+
; ---------------------------------------------------------
104+
ConsoleWrite(@CRLF & "+> 7.1 MERGE EXAMPLE <+" & @CRLF)
105+
; ---------------------------------------------------------
106+
; Let's merge some new data into our current JSON
107+
Local $sUpdate = '{"user": "Admin_SmartUser", "preferences": {"theme": "Dark", "notifications": true}}'
108+
$oJson.Merge($sUpdate)
109+
ConsoleWrite("- After Merge (User Updated & Prefs Added): " & $oJson.GetTokenValue("user") & @CRLF)
110+
ConsoleWrite("- New Nested Value: " & $oJson.GetTokenValue("preferences.theme") & @CRLF)
111+
112+
; ---------------------------------------------------------
113+
ConsoleWrite(@CRLF & "+> 7.2 TYPE CHECKING <+" & @CRLF)
114+
; ---------------------------------------------------------
115+
; Check what kind of data we have at a specific path
116+
Local $sThemeType = $oJson.GetTokenType("preferences")
117+
Local $sUserType = $oJson.GetTokenType("user")
118+
ConsoleWrite("- 'preferences' type: " & $sThemeType & @CRLF) ; Should return Object
119+
ConsoleWrite("- 'user' type: " & $sUserType & @CRLF) ; Should return String
120+
121+
; ---------------------------------------------------------
122+
ConsoleWrite(@CRLF & "+> 7.3 SEARCH (JSONPath) <+" & @CRLF)
123+
; ---------------------------------------------------------
124+
; $..* Returns everything in a flat list (all values).
125+
; $.store.book[*].author Returns all authors in the book array.
126+
; $..book[0] Returns the first book, wherever the book array is.
127+
; $..price Returns all prices from all objects.
128+
; Let's find all titles in our store (from section 2b)
129+
Local $sTitlesFound = $oJson.Search("$..title")
130+
ConsoleWrite("- Search results (All Titles): " & $sTitlesFound & @CRLF)
131+
132+
; Query Explanation:
133+
; $..book -> Search everywhere for the array "book"
134+
; [?(@.price > 15)] -> Filter the objects where the price property is > 15
135+
; .title -> Of the ones found, return only the title
136+
Local $sExpensiveBooks = $oJson.Search("$..book[?(@.price > 15)].title")
137+
138+
ConsoleWrite("- Books pricier than 15: " & $sExpensiveBooks & @CRLF)
139+
; Expected result: ["AutoIt Guru"]
140+
Local $sFullObjects = $oJson.Search("$..book[?(@.price > 15)]")
141+
ConsoleWrite("- Full Objects : " & $sFullObjects & @CRLF)
142+
143+
; ---------------------------------------------------------
144+
ConsoleWrite(@CRLF & "+> 7.4 FLATTEN <+" & @CRLF)
145+
; ---------------------------------------------------------
146+
; Convert the complex nested structure into a flat key-value list
147+
Local $sFlatJson = $oJson.Flatten()
148+
ConsoleWrite("- FLATTENED VIEW (Key.Path = Value)" & @CRLF)
149+
ConsoleWrite($sFlatJson & @CRLF)
150+
151+
; ---------------------------------------------------------
152+
ConsoleWrite(@CRLF & "+> 7.5 REMOVE TOKEN <+" & @CRLF)
153+
; ---------------------------------------------------------
154+
; Remove the 'preferences' object completely
155+
Local $bRemoved = $oJson.RemoveToken("preferences")
156+
157+
If $bRemoved Then
158+
ConsoleWrite("- 'preferences' removal command sent." & @CRLF)
159+
Else
160+
ConsoleWrite("! Error: Path 'preferences' not found to remove." & @CRLF)
161+
EndIf
162+
163+
If $oJson.Exists("preferences") Then
164+
ConsoleWrite("! Verify existence: Still there (Something went wrong)" & @CRLF)
165+
ConsoleWrite("! Current JSON: " & $oJson.GetMinifiedJson() & @CRLF)
166+
Else
167+
ConsoleWrite("- Verify existence: Gone! (Success)" & @CRLF)
168+
EndIf
169+
170+
; ---------------------------------------------------------
171+
ConsoleWrite(@CRLF & "+> 7.6 CLONE LOGIC <+" & @CRLF)
172+
; ---------------------------------------------------------
173+
; Check if we can backup our data
174+
If $oJson.CloneTo("BackupInstance") Then
175+
ConsoleWrite("- Data integrity check for cloning: OK" & @CRLF)
176+
EndIf
177+
178+
; ---------------------------------------------------------
179+
ConsoleWrite(@CRLF & "+> 7.7 FLATTEN TO TABLE (_ArrayFromString ready) <+" & @CRLF)
180+
; ---------------------------------------------------------
181+
Local $sTable = $oJson.FlattenToTable("|", @CRLF)
182+
Local $aFinalGrid = _ArrayFromString($sTable, "|", @CRLF, True)
183+
184+
If Not @error Then
185+
ConsoleWrite("- Array successfully created from FlattenToTable!" & @CRLF)
186+
;_ArrayDisplay($aFinalGrid, "v1.4.1 Final Table View")
187+
For $i = 0 To UBound($aFinalGrid) - 1
188+
ConsoleWrite($i & ") " & $aFinalGrid[$i][0] & " = " & $aFinalGrid[$i][1] & @CRLF)
189+
Next
190+
EndIf
191+
192+
; ---------------------------------------------------------
193+
ConsoleWrite(@CRLF & "--- TUTORIAL COMPLETED ---" & @CRLF)
194+
; ---------------------------------------------------------
195+
; Clean up object
196+
$oJson = Null
197+
198+
; ---------------------------------------------------------
199+
200+
Func _ErrFunc($oError) ; User's COM error function. Will be called if COM error occurs
201+
; Do anything here.
202+
ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
203+
@TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
204+
@TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
205+
@TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
206+
@TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
207+
@TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
208+
@TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
209+
@TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
210+
@TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
211+
@TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
212+
EndFunc ;==>_ErrFunc

examples/001-BasicDemo.au3

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#AutoIt3Wrapper_UseX64=y
2+
#AutoIt3Wrapper_Run_AU3Check=Y
3+
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y
4+
;~ #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
5+
6+
; 001-BasicDemo.au3
7+
8+
#include <GUIConstantsEx.au3>
9+
#include <WindowsConstants.au3>
10+
#include "..\NetWebView2Lib.au3"
11+
12+
; ==============================================================================
13+
; WebView2 Multi-Channel Presentation Script
14+
; ==============================================================================
15+
16+
Main()
17+
18+
Func Main()
19+
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc)
20+
#forceref $oMyError
21+
22+
; Create the UI
23+
Local $iHeight = 800
24+
Local $hGUI = GUICreate("WebView2 .NET Manager - Community Demo", 1100, $iHeight)
25+
GUICtrlSetFont(-1, 9, 400, 0, "Segoe UI")
26+
27+
WinMove($hGUI, '', Default, Default, 800, 440)
28+
GUISetState(@SW_SHOW, $hGUI)
29+
30+
; Initialize WebView2 Manager and register events
31+
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", "", "--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 = @TempDir & "\NetWebView2Lib-UserDataFolder"
39+
_NetWebView2_Initialize($oWebV2M, $hGUI, $sProfileDirectory, 0, 0, 0, 0, True, True, True, 1.2, "0x2B2B2B")
40+
__NetWebView2_Log(@ScriptLineNumber, "After: _NetWebView2_Initialize()", 1)
41+
42+
; navigate to HTML string - full fill the object with your own offline content - without downloading any content
43+
_NetWebView2_NavigateToString($oWebV2M, __GetDemoHTML())
44+
MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, 'Watch Point - AFTER:' & @CRLF & 'navigate to string')
45+
46+
GUISetState(@SW_HIDE, $hGUI)
47+
WinMove($hGUI, '', Default, Default, 1100, 800)
48+
49+
; navigate to a given URL - online content
50+
_NetWebView2_Navigate($oWebV2M, 'https://www.microsoft.com', $NETWEBVIEW2_MESSAGE__TITLE_CHANGED, 5 * 1000)
51+
GUISetState(@SW_SHOW, $hGUI)
52+
MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, 'Watch Point - AFTER:' & @CRLF & 'navigate to a given URL - online content')
53+
54+
; navigate to fake/broken url
55+
_NetWebView2_Navigate($oWebV2M, 'htpppps://www.microsoft.com', $NETWEBVIEW2_MESSAGE__TITLE_CHANGED, 5 * 1000)
56+
MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, 'Watch Point - AFTER:' & @CRLF & 'navigate to fake/broken url')
57+
58+
; navigate to fake not ex url
59+
__NetWebView2_Log(@ScriptLineNumber, "Before: https://w2ww.microsoft.com", 1)
60+
_NetWebView2_Navigate($oWebV2M, 'https://w2ww.microsoft.com', $NETWEBVIEW2_MESSAGE__TITLE_CHANGED, 5 * 1000)
61+
__NetWebView2_Log(@ScriptLineNumber, "After: https://w2ww.microsoft.com", 1)
62+
MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, 'Watch Point - AFTER:' & @CRLF & 'navigate to fake/broken url' & @CRLF & 'HostNameNotResolved')
63+
64+
; Main Loop
65+
While 1
66+
Switch GUIGetMsg()
67+
Case $GUI_EVENT_CLOSE
68+
ExitLoop
69+
EndSwitch
70+
WEnd
71+
72+
GUIDelete($hGUI)
73+
74+
_NetWebView2_CleanUp($oWebV2M)
75+
EndFunc ;==>Main
76+
77+
; ==============================================================================
78+
; ; Function to update a text element inside the WebView UI
79+
; ==============================================================================
80+
Func UpdateWebUI($oWebV2M, $sElementId, $sNewText)
81+
If Not IsObj($oWebV2M) Then Return ''
82+
83+
; Escape backslashes, single quotes and handle new lines for JavaScript safety
84+
Local $sCleanText = StringReplace($sNewText, "\", "\\")
85+
$sCleanText = StringReplace($sCleanText, "'", "\'")
86+
$sCleanText = StringReplace($sCleanText, @CRLF, "\n")
87+
$sCleanText = StringReplace($sCleanText, @LF, "\n")
88+
89+
Local $sJavaScript = "document.getElementById('" & $sElementId & "').innerText = '" & $sCleanText & "';"
90+
_NetWebView2_ExecuteScript($oWebV2M, $sJavaScript)
91+
EndFunc ;==>UpdateWebUI
92+
93+
; ==============================================================================
94+
; MY EVENT HANDLER: Bridge (JavaScript Messages)
95+
; ==============================================================================
96+
Func _BridgeMyEventsHandler_OnMessageReceived($oWebV2M, $hGUI, $sMessage)
97+
Local Static $iMsgCnt = 0
98+
99+
If $sMessage = "CLOSE_APP" Then
100+
If MsgBox(36, "Confirm", "Exit Application?", 0, $hGUI) = 6 Then Exit
101+
Else
102+
MsgBox(64, "JS Notification", "Message from Browser: " & $sMessage)
103+
$iMsgCnt += 1
104+
UpdateWebUI($oWebV2M, "mainTitle", $iMsgCnt & " Hello from AutoIt!")
105+
EndIf
106+
EndFunc ;==>_BridgeMyEventsHandler_OnMessageReceived
107+
108+
; ==============================================================================
109+
; HELPER: Demo HTML Content
110+
; ==============================================================================
111+
Func __GetDemoHTML()
112+
Local $sH = _
113+
'<html><head><style>' & _
114+
'body { font-family: "Segoe UI", sans-serif; background: #202020; color: white; padding: 40px; text-align: center; }' & _
115+
'.card { background: #2d2d2d; padding: 20px; border-radius: 8px; border: 1px solid #444; }' & _
116+
'button { padding: 12px 24px; cursor: pointer; background: #0078d4; color: white; border: none; border-radius: 4px; font-size: 16px; margin: 5px; }' & _
117+
'button:hover { background: #005a9e; }' & _
118+
'</style></head><body>' & _
119+
'<div class="card">' & _
120+
' <h1 id="mainTitle">WebView2 + AutoIt .NET Manager</h1>' & _ ; Fixed ID attribute
121+
' <p id="statusMsg">The communication is now 100% Event-Driven (No Sleep needed).</p>' & _
122+
' <button onclick="window.chrome.webview.postMessage(''Hello from JavaScript!'')">Send Ping</button>' & _
123+
' <button onclick="window.chrome.webview.postMessage(''CLOSE_APP'')">Exit App</button>' & _
124+
'</div>' & _
125+
'</body></html>'
126+
Return $sH
127+
EndFunc ;==>__GetDemoHTML

0 commit comments

Comments
 (0)