Skip to content

Commit b87eea2

Browse files
authored
Update NetWebView2Lib.au3
1 parent 1f54dec commit b87eea2

1 file changed

Lines changed: 24 additions & 230 deletions

File tree

NetWebView2Lib.au3

Lines changed: 24 additions & 230 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#Region ; *** Dynamically added Include files ***
2+
#include <WinAPIShPath.au3> ; added:01/18/26 02:35:43
3+
#EndRegion ; *** Dynamically added Include files ***
14
;~ #AutoIt3Wrapper_UseX64=y
25
#AutoIt3Wrapper_UseX64=n
36
#AutoIt3Wrapper_Run_AU3Check=Y
@@ -8,19 +11,30 @@
811
#include <GUIConstantsEx.au3>
912
#include <WindowsConstants.au3>
1013

11-
1214
; Global objects
1315
Global $_g_hNetWebView2Lib_DLL = ''
1416
Global $_g_oWeb
1517
Global $g_DebugInfo = True
16-
_Example()
1718

1819
#Region ; NetWebView2Lib UDF
1920
Func _NetWebView2_StartUp($sDLLFileFullPath)
21+
$sDLLFileFullPath = _WinAPI_PathCanonicalize($sDLLFileFullPath)
22+
;~ ConsoleWrite($sDLLFileFullPath & @CRLF)
2023
$_g_hNetWebView2Lib_DLL = DllOpen($sDLLFileFullPath)
24+
25+
;~ ConsoleWrite("! " & VarGetType($_g_hNetWebView2Lib_DLL) & @CRLF)
26+
;~ ConsoleWrite("! " & $_g_hNetWebView2Lib_DLL & @CRLF)
27+
If $_g_hNetWebView2Lib_DLL = -1 Then
28+
__NetWebView2_Log(@ScriptLineNumber, 'Error loading AcitevX DLL : ' & $_g_hNetWebView2Lib_DLL)
29+
Return SetError(1, @extended, $_g_hNetWebView2Lib_DLL)
30+
EndIf
2131
Return SetError(@error, @extended, $_g_hNetWebView2Lib_DLL)
2232
EndFunc ;==>_NetWebView2_StartUp
2333

34+
Func _NetWebView2_ShutDown()
35+
DllClose($_g_hNetWebView2Lib_DLL)
36+
EndFunc ;==>_NetWebView2_ShutDown
37+
2438
; #FUNCTION# ====================================================================================================================
2539
; Name ..........: _NetWebView2_CreateManager
2640
; Description ...:
@@ -37,8 +51,10 @@ EndFunc ;==>_NetWebView2_StartUp
3751
Func _NetWebView2_CreateManager()
3852
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc) ; Local COM Error Handler
3953
#forceref $oMyError
40-
Local $oWebV2M = ObjCreate("NetWebView2.Manager", "{CCB12345-6789-4ABC-DEF0-1234567890AB}", $_g_hNetWebView2Lib_DLL) ; NOT REGISTERED VERSION
54+
Local $oWebV2M = ObjCreate("NetWebView2.Manager") ; REGISTERED VERSION
55+
;~ Local $oWebV2M = ObjCreate("NetWebView2.Manager", "{CCB12345-6789-4ABC-DEF0-1234567890AB}", $_g_hNetWebView2Lib_DLL) ; NOT REGISTERED VERSION
4156
;~ _NetWebView2_ObjName_FlagsValue($oWebV2M)
57+
If @error Then __NetWebView2_Log(@ScriptLineNumber, "! [NetWebView2Lib]: Manager Creation ERROR")
4258
Return SetError(@error, @extended, $oWebV2M)
4359
EndFunc ;==>_NetWebView2_CreateManager
4460

@@ -59,7 +75,8 @@ Func _NetJson_CreateParser($sInitialJson = "{}")
5975
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc) ; Local COM Error Handler
6076
#forceref $oMyError
6177
If $sInitialJson = "" Or $sInitialJson = Default Then $sInitialJson = "{}"
62-
Local $oParser = ObjCreate("NetJson.Parser", "{D1E2F3A4-B5C6-4D7E-8F9A-0B1C2D3E4F5A}", $_g_hNetWebView2Lib_DLL) ; NOT REGISTERED VERSION
78+
Local $oParser = ObjCreate("NetJson.Parser") ; REGISTERED VERSION
79+
;~ Local $oParser = ObjCreate("NetJson.Parser", "{D1E2F3A4-B5C6-4D7E-8F9A-0B1C2D3E4F5A}", $_g_hNetWebView2Lib_DLL) ; NOT REGISTERED VERSION
6380
If Not IsObj($oParser) Then Return SetError(1, 0, 0)
6481
$oParser.Parse($sInitialJson)
6582
Return $oParser
@@ -235,14 +252,14 @@ Func __NetWebView2_JSEvents_OnMessageReceived($sMsg)
235252
If UBound($aClickData) >= 2 Then
236253
Local $sKey = StringStripWS($aClickData[0], 3)
237254
Local $sVal = StringStripWS($aClickData[1], 3)
238-
__NetWebView2_Log(@ScriptLineNumber, "+++ [JavaScriptEvents]: Property: " & $sKey & " | Value: " & $sVal & @CRLF)
255+
__NetWebView2_Log(@ScriptLineNumber, "+++ [JavaScriptEvents]: Property: " & $sKey & " | Value: " & $sVal)
239256
EndIf
240257

241258
Case "COM_TEST"
242-
__NetWebView2_Log(@ScriptLineNumber, "- [JavaScriptEvents]: Status: Legacy COM_TEST: " & $sData & @CRLF)
259+
__NetWebView2_Log(@ScriptLineNumber, "- [JavaScriptEvents]: Status: Legacy COM_TEST: " & $sData)
243260

244261
Case "ERROR"
245-
__NetWebView2_Log(@ScriptLineNumber, "! [JavaScriptEvents]: Status: " & $sData & @CRLF)
262+
__NetWebView2_Log(@ScriptLineNumber, "! [JavaScriptEvents]: Status: " & $sData)
246263
EndSwitch
247264
EndIf
248265
EndFunc ;==>__NetWebView2_JSEvents_OnMessageReceived
@@ -256,226 +273,3 @@ EndFunc ;==>__NetWebView2_WebEvents_OnContextMenuRequested
256273
#EndRegion ; === EVENT HANDLERS ===
257274

258275
#EndRegion ; NetWebView2Lib UDF
259-
260-
#Region ; UDF TESTING EXAMPLE
261-
Func _Example()
262-
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc)
263-
#forceref $oMyError
264-
; Create GUI with resizing support
265-
Local $hGUI = GUICreate("WebView2AutoIt JSON Viewer", 500, 650, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
266-
GUISetBkColor(0x2B2B2B, $hGUI)
267-
268-
; GUI Controls for JSON Tree interaction
269-
Local $idExpand = GUICtrlCreateLabel("Expand All", 10, 10, 90, 30)
270-
GUICtrlSetFont(-1, 12, Default, $GUI_FONTUNDER, "Segoe UI")
271-
GUICtrlSetResizing(-1, $GUI_DOCKALL)
272-
GUICtrlSetColor(-1, 0x00FF00) ; Green
273-
274-
Local $idCollapse = GUICtrlCreateLabel("Collapse All", 110, 10, 90, 30)
275-
GUICtrlSetFont(-1, 12, Default, $GUI_FONTUNDER, "Segoe UI")
276-
GUICtrlSetResizing(-1, $GUI_DOCKALL)
277-
GUICtrlSetColor(-1, 0xFF4D4D) ; Red
278-
279-
Local $idFind = GUICtrlCreateLabel("Search", 210, 10, 60, 30)
280-
GUICtrlSetFont(-1, 12, Default, $GUI_FONTUNDER, "Segoe UI")
281-
GUICtrlSetResizing(-1, $GUI_DOCKALL)
282-
GUICtrlSetColor(-1, 0xFFD700) ; Gold
283-
284-
Local $idLoadFile = GUICtrlCreateLabel("Load JSON", 280, 10, 90, 30)
285-
GUICtrlSetFont(-1, 12, Default, $GUI_FONTUNDER, "Segoe UI")
286-
GUICtrlSetResizing(-1, $GUI_DOCKALL)
287-
GUICtrlSetColor(-1, 0x00CCFF) ; Light Blue
288-
289-
_NetWebView2_StartUp(@ScriptDir & '\bin\NetWebView2Lib.dll')
290-
291-
; Initialize WebView2 Manager and register events
292-
Local $oWebV2M = _NetWebView2_CreateManager()
293-
$_g_oWeb = $oWebV2M
294-
If @error Then Return SetError(@error, @extended, $oWebV2M)
295-
296-
ObjEvent($oWebV2M, "__NetWebView2_WebEvents_", "IWebViewEvents")
297-
298-
; Important: Pass $hGUI in parentheses to maintain Pointer type for COM
299-
Local $sProfileDirectory = @TempDir & "\NetWebView2Lib-UserDataFolder"
300-
$oWebV2M.Initialize($hGUI, $sProfileDirectory, 0, 50, 500, 600)
301-
302-
; Initialize JavaScript Bridge
303-
Local $oJS = $oWebV2M.GetBridge()
304-
ObjEvent($oJS, "__NetWebView2_JSEvents_", "IBridgeEvents")
305-
306-
; Wait for WebView2 to be ready
307-
Do
308-
Sleep(50)
309-
Until $oWebV2M.IsReady
310-
311-
; WebView2 Configuration
312-
$oWebV2M.SetAutoResize(True) ; Using SetAutoResize(True) to skip WM_SIZE
313-
$oWebV2M.BackColor = "0x2B2B2B"
314-
$oWebV2M.AreDevToolsEnabled = True ; Allow F12
315-
$oWebV2M.ZoomFactor = 1.2
316-
317-
; Initial JSON display
318-
Local $sMyJson = '{"Game": "Witcher 3", "ID": 1, "Meta": {"Developer": "CD Projekt", "Year": 2015 }, "Tags": ["RPG", "Open World"]}'
319-
320-
_Web_jsonTree($oWebV2M, $sMyJson) ; 🏆 https://github.com/summerstyle/jsonTreeViewer
321-
322-
GUISetState(@SW_SHOW)
323-
324-
Local $sLastSearch = ""
325-
326-
; Main Application Loop
327-
While 1
328-
Switch GUIGetMsg()
329-
Case $GUI_EVENT_CLOSE
330-
Exit
331-
332-
Case $idExpand
333-
; Call JavaScript expand method on the global tree object
334-
$oWebV2M.ExecuteScript("if(window.tree) window.tree.expand();")
335-
336-
Case $idCollapse
337-
; Call JavaScript collapse method
338-
$oWebV2M.ExecuteScript("if(window.tree) window.tree.collapse();")
339-
340-
Case $idFind
341-
Local $sInput = InputBox("JSON Search", "Enter key or value:", $sLastSearch, "", 200, 130, Default, Default, Default, $hGUI)
342-
If Not @error And StringLen(StringStripWS($sInput, 3)) > 0 Then
343-
$sLastSearch = StringStripWS($sInput, 3)
344-
_Web_jsonTreeFind($oWebV2M, $sLastSearch, False) ; New search
345-
EndIf
346-
347-
Case $idLoadFile
348-
Local $sFilePath = FileOpenDialog("Select JSON File", @ScriptDir, "JSON Files (*.json;*.txt)", 1)
349-
If Not @error Then
350-
Local $sFileData = FileRead($sFilePath)
351-
If $sFileData <> "" Then
352-
_Web_jsonTree($oWebV2M, $sFileData) ; Re-render tree with new data
353-
__NetWebView2_Log(@ScriptLineNumber, "+ Loaded JSON from: " & $sFilePath)
354-
EndIf
355-
EndIf
356-
357-
EndSwitch
358-
WEnd
359-
360-
If IsObj($oWebV2M) Then $oWebV2M.Cleanup()
361-
$oWebV2M = 0
362-
$oJS = 0
363-
364-
EndFunc ;==>Main
365-
366-
#Region ; === UTILS ===
367-
368-
; #FUNCTION# ====================================================================================================================
369-
; Name...........: _Web_jsonTree
370-
; Description....: Renders JSON data using the jsonTree library by summerstyle.
371-
; Author.........: summerstyle (https://github.com/summerstyle/jsonTreeViewer)
372-
; Integration....: Adapted for AutoIt WebView2
373-
; ===============================================================================================================================
374-
Func _Web_jsonTree(ByRef $oWebV2M, $sJson)
375-
; 1. Prepare JSON (Minify to prevent script errors from line breaks)
376-
Local $oJSON = _NetJson_CreateParser($sJson)
377-
;~ _NetWebView2_ObjName_FlagsValue($oJSON)
378-
$sJson = $oJSON.GetMinifiedJson()
379-
380-
; 2. Load local library files
381-
Local $sJsLib = FileRead(@ScriptDir & "\examples\v1.4.2_jsonTree\JS_Lib\jsonTree.js")
382-
Local $sCssLib = FileRead(@ScriptDir & "\examples\v1.4.2_jsonTree\JS_Lib\jsonTreeDark.css")
383-
384-
; 3. Build HTML with embedded Logic
385-
Local $sHTML = "<html><head><meta charset=""utf-8""><style>" & _
386-
$sCssLib & _
387-
"</style></head><body>" & _
388-
"<div id='tree-container' class='jsontree_tree'></div>" & _
389-
" <div style='position:fixed; bottom:5px; right:10px; font-size:10px; color:#555; font-family:sans-serif;'>" & _
390-
" Powered by <a href='https://github.com/summerstyle/jsonTreeViewer' style='color:#777; text-decoration:none;'>jsonTree</a>" & _
391-
" </div>" & _
392-
"<script>" & @CRLF & _
393-
$sJsLib & @CRLF & _
394-
";" & @CRLF & _ ; Ensure library/code separation
395-
"try {" & @CRLF & _
396-
" var data = " & $sJson & ";" & @CRLF & _
397-
" var container = document.getElementById('tree-container');" & @CRLF & _
398-
" if (typeof jsonTree !== 'undefined') {" & @CRLF & _
399-
" window.tree = jsonTree.create(data, container);" & @CRLF & _ ; Assign to window for global access
400-
" window.tree.expand(1);" & @CRLF & _
401-
" container.addEventListener('click', function(e) {" & @CRLF & _
402-
" var node = e.target.closest('.jsontree_node');" & @CRLF & _
403-
" if (node) {" & @CRLF & _
404-
" var labelEl = node.querySelector('.jsontree_label');" & @CRLF & _
405-
" var valueEl = node.querySelector('.jsontree_value');" & @CRLF & _
406-
" if (labelEl && valueEl) {" & @CRLF & _
407-
" var msg = 'JSON_CLICKED|' + labelEl.innerText + ' = ' + valueEl.innerText;" & @CRLF & _
408-
" window.chrome.webview.postMessage(msg);" & @CRLF & _
409-
" }" & @CRLF & _
410-
" }" & @CRLF & _
411-
" });" & @CRLF & _
412-
" } else {" & @CRLF & _
413-
" throw new Error('jsonTree library not loaded');" & @CRLF & _
414-
" }" & @CRLF & _
415-
"} catch(e) {" & @CRLF & _
416-
" window.chrome.webview.postMessage('DEBUG:' + e.message);" & @CRLF & _
417-
" document.body.innerHTML = '<b style=""color:red"">JS Error:</b> ' + e.message;" & @CRLF & _
418-
"}" & @CRLF & _
419-
"</script></body></html>"
420-
421-
; 4. Navigate to the generated HTML
422-
$oWebV2M.NavigateToString($sHTML)
423-
__NetWebView2_Log(@ScriptLineNumber, "+ JSON Tree Rendered & Listeners Active")
424-
EndFunc ;==>_Web_jsonTree
425-
426-
; #FUNCTION# ====================================================================================================================
427-
; Name...........: _Web_jsonTreeFind
428-
; Description....: Searches for a string in labels and values and highlights matching nodes.
429-
; Parameters.....: $sSearch - The string to find
430-
; ===============================================================================================================================
431-
Func _Web_jsonTreeFind(ByRef $oWebV2M, $sSearch, $bNext = False)
432-
Local $sJS = _
433-
"var term = '" & $sSearch & "'.toLowerCase();" & _
434-
"if (!window.searchIndices || window.lastTerm !== term) {" & _
435-
" window.searchIndices = [];" & _
436-
" window.currentSearchIndex = -1;" & _
437-
" window.lastTerm = term;" & _
438-
"}" & _
439-
"" & _
440-
"/* 1. If it's a new search, find all targets */" & _
441-
"if (!" & StringLower($bNext) & " || window.searchIndices.length === 0) {" & _
442-
" document.querySelectorAll('.jsontree_node_marked').forEach(el => el.classList.remove('jsontree_node_marked', 'jsontree_node_active'));" & _
443-
" var targets = document.querySelectorAll('.jsontree_label, .jsontree_value');" & _
444-
" window.searchIndices = [];" & _
445-
" targets.forEach(function(el) {" & _
446-
" var text = el.innerText.toLowerCase();" & _
447-
" var isBracket = (text === '{' || text === '}' || text === '[' || text === ']' || text === '{ }' || text === '[ ]');" & _
448-
" if (!isBracket && (el.classList.contains('jsontree_label') || el.children.length === 0) && text.includes(term)) {" & _
449-
" el.classList.add('jsontree_node_marked');" & _
450-
" window.searchIndices.push(el);" & _
451-
" }" & _
452-
" });" & _
453-
"}" & _
454-
"" & _
455-
"/* 2. Move to next index */" & _
456-
"if (window.searchIndices.length > 0) {" & _
457-
" /* Remove active class from previous */" & _
458-
" if (window.currentSearchIndex >= 0) window.searchIndices[window.currentSearchIndex].classList.remove('jsontree_node_active');" & _
459-
" " & _
460-
" window.currentSearchIndex++;" & _
461-
" if (window.currentSearchIndex >= window.searchIndices.length) window.currentSearchIndex = 0;" & _
462-
" " & _
463-
" var activeEl = window.searchIndices[window.currentSearchIndex];" & _
464-
" activeEl.classList.add('jsontree_node_active');" & _
465-
" " & _
466-
" /* Expand parents of active element */" & _
467-
" var p = activeEl.closest('.jsontree_node');" & _
468-
" while (p && p.id !== 'tree-container') {" & _
469-
" if (p.classList.contains('jsontree_node_complex')) p.classList.add('jsontree_node_expanded');" & _
470-
" p = p.parentElement;" & _
471-
" }" & _
472-
" activeEl.scrollIntoView({behavior: 'smooth', block: 'center'});" & _
473-
"}"
474-
475-
; Replace the AutoIt variable $bNext with JS boolean
476-
;~ $sJS = StringReplace($sJS, "$bNext", ($bNext ? "true" : "false"))
477-
ConsoleWrite("$sJS=" & $sJS & @CRLF)
478-
$oWebV2M.ExecuteScript($sJS)
479-
EndFunc ;==>_Web_jsonTreeFind
480-
#EndRegion ; === UTILS ===
481-
#EndRegion ; UDF TESTING EXAMPLE

0 commit comments

Comments
 (0)