Skip to content

Commit a632f41

Browse files
authored
Update jsonTree.au3
1 parent b87eea2 commit a632f41

1 file changed

Lines changed: 55 additions & 153 deletions

File tree

Lines changed: 55 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
#AutoIt3Wrapper_UseX64=y
2+
;~ #AutoIt3Wrapper_UseX64=n
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+
27
; Html_Gui.au3
8+
#include <Array.au3>
39
#include <GUIConstantsEx.au3>
410
#include <WindowsConstants.au3>
5-
#include <Array.au3>
11+
#include "..\..\NetWebView2Lib.au3"
612

7-
; Register exit function to ensure clean WebView2 shutdown
8-
OnAutoItExitRegister("_ExitApp")
913

10-
; Global objects
11-
Global $oWeb, $oJS
12-
Global $oMyError = ObjEvent("AutoIt.Error", "_ErrFunc") ; COM Error Handler
13-
Global $g_DebugInfo = True
14-
Global $g_sProfilePath = @ScriptDir & "\UserDataFolder"
15-
Global $hGUI
14+
_Example()
1615

17-
Main()
18-
19-
Func Main()
16+
#Region ; UDF TESTING EXAMPLE
17+
Func _Example()
18+
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc)
19+
#forceref $oMyError
2020
; Create GUI with resizing support
21-
$hGUI = GUICreate("WebView2AutoIt JSON Viewer", 500, 650, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
21+
Local $hGUI = GUICreate("WebView2AutoIt JSON Viewer", 500, 650, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
2222
GUISetBkColor(0x2B2B2B, $hGUI)
2323

2424
; GUI Controls for JSON Tree interaction
@@ -42,32 +42,41 @@ Func Main()
4242
GUICtrlSetResizing(-1, $GUI_DOCKALL)
4343
GUICtrlSetColor(-1, 0x00CCFF) ; Light Blue
4444

45+
;~ Local $sActiveX_Dll = @ScriptDir & '\..\..\bin\NetWebView2Lib.dll'
46+
;~ _NetWebView2_StartUp($sActiveX_Dll)
47+
_NetWebView2_StartUp('Z:\!!!_SVN_AU3\UDF_Forum\Other_Members\Official_AutoIt_Forum\NetWebView2Lib-main\bin\NetWebView2Lib.dll')
48+
If @error Then Return SetError(@error, @extended, -1)
49+
4550
; Initialize WebView2 Manager and register events
46-
$oWeb = ObjCreate("NetWebView2.Manager")
47-
ObjEvent($oWeb, "WebEvents_", "IWebViewEvents")
51+
Local $oWebV2M = _NetWebView2_CreateManager()
52+
$_g_oWeb = $oWebV2M
53+
If @error Then Return SetError(@error, @extended, $oWebV2M)
54+
55+
ObjEvent($oWebV2M, "__NetWebView2_WebEvents_", "IWebViewEvents")
4856

4957
; Important: Pass $hGUI in parentheses to maintain Pointer type for COM
50-
$oWeb.Initialize(($hGUI), $g_sProfilePath, 0, 50, 500, 600)
58+
Local $sProfileDirectory = @TempDir & "\NetWebView2Lib-UserDataFolder"
59+
$oWebV2M.Initialize($hGUI, $sProfileDirectory, 0, 50, 500, 600)
5160

5261
; Initialize JavaScript Bridge
53-
$oJS = $oWeb.GetBridge()
54-
ObjEvent($oJS, "JavaScript_", "IBridgeEvents")
62+
Local $oJS = $oWebV2M.GetBridge()
63+
ObjEvent($oJS, "__NetWebView2_JSEvents_", "IBridgeEvents")
5564

5665
; Wait for WebView2 to be ready
5766
Do
5867
Sleep(50)
59-
Until $oWeb.IsReady
68+
Until $oWebV2M.IsReady
6069

6170
; WebView2 Configuration
62-
$oWeb.SetAutoResize(True) ; Using SetAutoResize(True) to skip WM_SIZE
63-
$oWeb.BackColor = "0x2B2B2B"
64-
$oWeb.AreDevToolsEnabled = True ; Allow F12
65-
$oWeb.ZoomFactor = 1.2
71+
$oWebV2M.SetAutoResize(True) ; Using SetAutoResize(True) to skip WM_SIZE
72+
$oWebV2M.BackColor = "0x2B2B2B"
73+
$oWebV2M.AreDevToolsEnabled = True ; Allow F12
74+
$oWebV2M.ZoomFactor = 1.2
6675

6776
; Initial JSON display
6877
Local $sMyJson = '{"Game": "Witcher 3", "ID": 1, "Meta": {"Developer": "CD Projekt", "Year": 2015 }, "Tags": ["RPG", "Open World"]}'
6978

70-
_Web_jsonTree($oWeb, $sMyJson) ; 🏆 https://github.com/summerstyle/jsonTreeViewer
79+
_Web_jsonTree($oWebV2M, $sMyJson) ; 🏆 https://github.com/summerstyle/jsonTreeViewer
7180

7281
GUISetState(@SW_SHOW)
7382

@@ -81,157 +90,56 @@ Func Main()
8190

8291
Case $idExpand
8392
; Call JavaScript expand method on the global tree object
84-
$oWeb.ExecuteScript("if(window.tree) window.tree.expand();")
93+
$oWebV2M.ExecuteScript("if(window.tree) window.tree.expand();")
8594

8695
Case $idCollapse
8796
; Call JavaScript collapse method
88-
$oWeb.ExecuteScript("if(window.tree) window.tree.collapse();")
97+
$oWebV2M.ExecuteScript("if(window.tree) window.tree.collapse();")
8998

9099
Case $idFind
91100
Local $sInput = InputBox("JSON Search", "Enter key or value:", $sLastSearch, "", 200, 130, Default, Default, Default, $hGUI)
92101
If Not @error And StringLen(StringStripWS($sInput, 3)) > 0 Then
93102
$sLastSearch = StringStripWS($sInput, 3)
94-
_Web_jsonTreeFind($sLastSearch, False) ; New search
103+
_Web_jsonTreeFind($oWebV2M, $sLastSearch, False) ; New search
95104
EndIf
96105

97106
Case $idLoadFile
98107
Local $sFilePath = FileOpenDialog("Select JSON File", @ScriptDir, "JSON Files (*.json;*.txt)", 1)
99108
If Not @error Then
100109
Local $sFileData = FileRead($sFilePath)
101110
If $sFileData <> "" Then
102-
_Web_jsonTree($oWeb, $sFileData) ; Re-render tree with new data
103-
__DW("+ Loaded JSON from: " & $sFilePath & @CRLF)
111+
_Web_jsonTree($oWebV2M, $sFileData) ; Re-render tree with new data
112+
__NetWebView2_Log(@ScriptLineNumber, "+ Loaded JSON from: " & $sFilePath)
104113
EndIf
105114
EndIf
106115

107116
EndSwitch
108117
WEnd
109-
EndFunc ;==>Main
110-
111-
#Region ; === EVENT HANDLERS ===
112-
113-
; Handles native WebView2 events
114-
Func WebEvents_OnMessageReceived($sMsg)
115-
__DW("+++ [WebEvents]: " & (StringLen($sMsg) > 150 ? StringLeft($sMsg, 150) & "..." : $sMsg) & @CRLF, 0)
116-
Local $iSplitPos = StringInStr($sMsg, "|")
117-
Local $sCommand = $iSplitPos ? StringStripWS(StringLeft($sMsg, $iSplitPos - 1), 3) : $sMsg
118-
Local $sData = $iSplitPos ? StringTrimLeft($sMsg, $iSplitPos) : ""
119-
Local $aParts
120-
121-
Switch $sCommand
122-
Case "INIT_READY"
123-
$oWeb.ExecuteScript('window.chrome.webview.postMessage(JSON.stringify({ "type": "COM_TEST", "status": "OK" }));')
124-
125-
Case "WINDOW_RESIZED"
126-
$aParts = StringSplit($sData, "|")
127-
If $aParts[0] >= 2 Then
128-
Local $iW = Int($aParts[1]), $iH = Int($aParts[2])
129-
; Filter minor resize glitches
130-
If $iW > 50 And $iH > 50 Then __DW("WINDOW_RESIZED : " & $iW & "x" & $iH & @CRLF)
131-
EndIf
132-
EndSwitch
133-
EndFunc ;==>WebEvents_OnMessageReceived
134-
135-
; Handles custom messages from JavaScript (window.chrome.webview.postMessage)
136-
Func JavaScript_OnMessageReceived($sMsg)
137-
__DW(">>> [JavaScript]: " & (StringLen($sMsg) > 150 ? StringLeft($sMsg, 150) & "..." : $sMsg) & @CRLF, 0)
138-
Local $sFirstChar = StringLeft($sMsg, 1)
139-
140-
; 1. Modern JSON Messaging
141-
If $sFirstChar = "{" Or $sFirstChar = "[" Then
142-
__DW("+> : Processing JSON message..." & @CRLF)
143-
Local $oJson = ObjCreate("NetJson.Parser")
144-
If Not IsObj($oJson) Then Return ConsoleWrite("!> Error: Failed to create NetJson object." & @CRLF)
145-
146-
$oJson.Parse($sMsg)
147-
Local $sJobType = $oJson.GetTokenValue("type")
148-
149-
Switch $sJobType
150-
Case "COM_TEST"
151-
__DW("- COM_TEST Confirmed: " & $oJson.GetTokenValue("status") & @CRLF)
152-
EndSwitch
153-
154-
Else
155-
; 2. Legacy / Native Pipe-Delimited Messaging
156-
__DW("+> : Processing Delimited message..." & @CRLF, 0)
157-
Local $sCommand, $sData, $iSplitPos
158-
$iSplitPos = StringInStr($sMsg, "|") - 1
159-
160-
If $iSplitPos < 0 Then
161-
$sCommand = StringStripWS($sMsg, 3)
162-
$sData = ""
163-
Else
164-
$sCommand = StringStripWS(StringLeft($sMsg, $iSplitPos), 3)
165-
$sData = StringTrimLeft($sMsg, $iSplitPos + 1)
166-
EndIf
167-
168-
Switch $sCommand
169-
Case "JSON_CLICKED"
170-
Local $aClickData = StringSplit($sData, "=", 2) ; Split "Key = Value"
171-
If UBound($aClickData) >= 2 Then
172-
Local $sKey = StringStripWS($aClickData[0], 3)
173-
Local $sVal = StringStripWS($aClickData[1], 3)
174-
__DW("+++ Property: " & $sKey & " | Value: " & $sVal & @CRLF)
175-
EndIf
176118

177-
Case "COM_TEST"
178-
__DW("- Status: Legacy COM_TEST: " & $sData & @CRLF)
179-
180-
Case "ERROR"
181-
__DW("! Status: " & $sData & @CRLF)
182-
EndSwitch
183-
EndIf
184-
EndFunc ;==>JavaScript_OnMessageReceived
185-
186-
Func WebEvents_OnContextMenuRequested($sLink, $iX, $iY, $sSelection)
187-
#forceref $sLink, $iX, $iY, $sSelection
188-
EndFunc ;==>WebEvents_OnContextMenuRequested
119+
If IsObj($oWebV2M) Then $oWebV2M.Cleanup()
120+
$oWebV2M = 0
121+
$oJS = 0
122+
_NetWebView2_ShutDown()
189123

190-
#EndRegion ; === EVENT HANDLERS ===
124+
EndFunc ;==>Main
191125

192126
#Region ; === UTILS ===
193127

194-
Func _ErrFunc($oError) ; Global COM Error Handler
195-
ConsoleWrite('@@ Line(' & $oError.scriptline & ') : COM Error Number: (0x' & Hex($oError.number, 8) & ') ' & $oError.windescription & @CRLF)
196-
EndFunc ;==>_ErrFunc
197-
198-
; Debug Write utility
199-
Func __DW($sString, $iErrorNoLineNo = 1, $iLine = @ScriptLineNumber, $iError = @error, $iExtended = @extended)
200-
If Not $g_DebugInfo Then Return SetError($iError, $iExtended, 0)
201-
Local $iReturn
202-
If $iErrorNoLineNo = 1 Then
203-
If $iError Then
204-
$iReturn = ConsoleWrite("@@(" & $iLine & ") :: @error:" & $iError & ", @extended:" & $iExtended & ", " & $sString)
205-
Else
206-
$iReturn = ConsoleWrite("+>(" & $iLine & ") :: " & $sString)
207-
EndIf
208-
Else
209-
$iReturn = ConsoleWrite($sString)
210-
EndIf
211-
Return SetError($iError, $iExtended, $iReturn)
212-
EndFunc ;==>__DW
213-
214-
Func _NetJson_New($sInitialJson = "{}")
215-
Local $oParser = ObjCreate("NetJson.Parser")
216-
If Not IsObj($oParser) Then Return SetError(1, 0, 0)
217-
If $sInitialJson <> "" Then $oParser.Parse($sInitialJson)
218-
Return $oParser
219-
EndFunc ;==>_NetJson_New
220-
221128
; #FUNCTION# ====================================================================================================================
222129
; Name...........: _Web_jsonTree
223130
; Description....: Renders JSON data using the jsonTree library by summerstyle.
224131
; Author.........: summerstyle (https://github.com/summerstyle/jsonTreeViewer)
225132
; Integration....: Adapted for AutoIt WebView2
226133
; ===============================================================================================================================
227-
Func _Web_jsonTree(ByRef $oWeb, $sJson)
134+
Func _Web_jsonTree(ByRef $oWebV2M, $sJson)
228135
; 1. Prepare JSON (Minify to prevent script errors from line breaks)
229-
Local $oJsonObj = _NetJson_New($sJson)
230-
$sJson = $oJsonObj.GetMinifiedJson()
136+
Local $oJSON = _NetJson_CreateParser($sJson)
137+
;~ _NetWebView2_ObjName_FlagsValue($oJSON)
138+
$sJson = $oJSON.GetMinifiedJson()
231139

232140
; 2. Load local library files
233-
Local $sJsLib = FileRead(@ScriptDir & "\JS_Lib\jsonTree.js")
234-
Local $sCssLib = FileRead(@ScriptDir & "\JS_Lib\jsonTreeDark.css")
141+
Local $sJsLib = FileRead(@ScriptDir & ".\JS_Lib\jsonTree.js")
142+
Local $sCssLib = FileRead(@ScriptDir & ".\JS_Lib\jsonTreeDark.css")
235143

236144
; 3. Build HTML with embedded Logic
237145
Local $sHTML = "<html><head><meta charset=""utf-8""><style>" & _
@@ -271,16 +179,16 @@ Func _Web_jsonTree(ByRef $oWeb, $sJson)
271179
"</script></body></html>"
272180

273181
; 4. Navigate to the generated HTML
274-
$oWeb.NavigateToString($sHTML)
275-
__DW("+ JSON Tree Rendered & Listeners Active." & @CRLF)
182+
$oWebV2M.NavigateToString($sHTML)
183+
__NetWebView2_Log(@ScriptLineNumber, "+ JSON Tree Rendered & Listeners Active")
276184
EndFunc ;==>_Web_jsonTree
277185

278186
; #FUNCTION# ====================================================================================================================
279187
; Name...........: _Web_jsonTreeFind
280188
; Description....: Searches for a string in labels and values and highlights matching nodes.
281189
; Parameters.....: $sSearch - The string to find
282190
; ===============================================================================================================================
283-
Func _Web_jsonTreeFind($sSearch, $bNext = False)
191+
Func _Web_jsonTreeFind(ByRef $oWebV2M, $sSearch, $bNext = False)
284192
Local $sJS = _
285193
"var term = '" & $sSearch & "'.toLowerCase();" & _
286194
"if (!window.searchIndices || window.lastTerm !== term) {" & _
@@ -327,14 +235,8 @@ Func _Web_jsonTreeFind($sSearch, $bNext = False)
327235
; Replace the AutoIt variable $bNext with JS boolean
328236
;~ $sJS = StringReplace($sJS, "$bNext", ($bNext ? "true" : "false"))
329237
ConsoleWrite("$sJS=" & $sJS & @CRLF)
330-
$oWeb.ExecuteScript($sJS)
238+
$oWebV2M.ExecuteScript($sJS)
331239
EndFunc ;==>_Web_jsonTreeFind
332-
333-
Func _ExitApp()
334-
If IsObj($oWeb) Then $oWeb.Cleanup()
335-
$oWeb = 0
336-
$oJS = 0
337-
Exit
338-
EndFunc ;==>_ExitApp
339-
340240
#EndRegion ; === UTILS ===
241+
#EndRegion ; UDF TESTING EXAMPLE
242+

0 commit comments

Comments
 (0)