Skip to content

Commit 904af74

Browse files
authored
updating examples
016-CSV_Editor.au3
1 parent 9caee9a commit 904af74

1 file changed

Lines changed: 40 additions & 85 deletions

File tree

examples/016-CSV_Editor.au3

Lines changed: 40 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,15 @@
1313
#include <WindowsConstants.au3>
1414
#include "..\NetWebView2Lib.au3"
1515

16-
; Register exit function to ensure clean WebView2 shutdown
17-
OnAutoItExitRegister(_ExitApp)
18-
19-
; Global objects
20-
Global $oWeb, $oJS
2116
Global $oMyError = ObjEvent("AutoIt.Error", _ErrFunc) ; COM Error Handler
22-
Global $g_DebugInfo = True
23-
Global $sProfileDirectory = @ScriptDir & "\UserDataFolder"
24-
Global $hGUI
2517

2618
_Example()
2719

2820
Func _Example()
2921
ConsoleWrite("! MicrosoftEdgeWebview2 : version check: " & _NetWebView2_IsAlreadyInstalled() & ' ERR=' & @error & ' EXT=' & @extended & @CRLF)
3022

3123
; Create GUI with resizing support
32-
$hGUI = GUICreate("WebView2AutoIt CSV_editor", 1500, 650, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
24+
Local $hGUI = GUICreate("WebView2AutoIt CSV_editor", 1500, 650, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
3325
GUISetBkColor(0x2B2B2B, $hGUI)
3426

3527
; GUI Controls for CSV interaction
@@ -44,101 +36,98 @@ Func _Example()
4436
GUICtrlSetColor(-1, 0x00CCFF) ; Light Blue
4537

4638
; Initialize WebView2 Manager and register events
47-
$oWeb = ObjCreate("NetWebView2.Manager")
48-
ObjEvent($oWeb, "WebEvents_", "IWebViewEvents")
49-
50-
; Important: Pass $hGUI in parentheses to maintain Pointer type for COM
51-
$oWeb.Initialize(($hGUI), $sProfileDirectory, 0, 50, 1500, 600)
52-
53-
; Initialize JavaScript Bridge
54-
$oJS = $oWeb.GetBridge()
55-
ObjEvent($oJS, "JavaScript_", "IBridgeEvents")
39+
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", _
40+
"WebEvents_", "--mute-audio")
41+
If @error Then Return SetError(@error, @extended, $oWebV2M)
5642

57-
; Wait for WebView2 to be ready
58-
Do
59-
Sleep(50)
60-
Until $oWeb.IsReady
43+
; create JavaScript Bridge object
44+
Local $oJSBridge = _NetWebView2_GetBridge($oWebV2M, "JavaScript_")
45+
If @error Then Return SetError(@error, @extended, $oWebV2M)
6146

62-
; WebView2 Configuration
63-
$oWeb.SetAutoResize(True) ; Using SetAutoResize(True) to skip WM_SIZE
64-
$oWeb.BackColor = "0x2B2B2B"
65-
$oWeb.AreDevToolsEnabled = True ; Allow F12
66-
$oWeb.ZoomFactor = 1.2
47+
; initialize browser - put it on the GUI
48+
Local $sProfileDirectory = @ScriptDir & "\NetWebView2Lib-UserDataFolder"
49+
_NetWebView2_Initialize($oWebV2M, $hGUI, $sProfileDirectory, 0, 50, 1500, 600, True, True, 1.2, "0x2B2B2B")
6750

6851
; Initial CSV display
69-
_Web_CSVViewer($oWeb) ; 🏆 https://stackblitz.com/edit/web-platform-3kkvy2?file=index.html
52+
_Web_CSVViewer($oWebV2M) ; 🏆 https://stackblitz.com/edit/web-platform-3kkvy2?file=index.html
7053

7154
GUISetState(@SW_SHOW)
7255

7356
; Main Application Loop
7457
While 1
7558
Switch GUIGetMsg()
7659
Case $GUI_EVENT_CLOSE
77-
Exit
60+
ExitLoop
7861

7962
Case $idSaveFile
80-
$oWeb.ExecuteScript("sendDataToAutoIt();")
63+
_NetWebView2_ExecuteScript($oWebV2M, "sendDataToAutoIt();", $NETWEBVIEW2_EXECUTEJS_MODE0_FIREANDFORGET)
8164

8265
Case $idLoadFile
8366
Local $sFilePath = FileOpenDialog("Select CSV File", @ScriptDir, "CSV Files (*.csv;*.txt)", 1)
8467
If Not @error Then
8568
Local $sFileData = FileRead($sFilePath)
8669
If $sFileData <> "" Then
87-
_Web_CSVViewer($oWeb, $sFileData) ; Re-render CSV with new data
88-
__DW("+ Loaded CSV from: " & $sFilePath)
70+
_Web_CSVViewer($oWebV2M, $sFileData) ; Re-render CSV with new data
71+
ConsoleWrite(">>> Loaded CSV from: " & $sFilePath)
8972
EndIf
9073
EndIf
9174

9275
EndSwitch
9376
WEnd
77+
78+
_NetWebView2_CleanUp($oWebV2M, $oJSBridge)
79+
9480
EndFunc ;==>_Example
9581

9682
#Region ; === EVENT HANDLERS ===
9783

9884
; Handles native WebView2 events
99-
Func WebEvents_OnMessageReceived($sMsg)
100-
__DW("+++ [WebEvents]: " & (StringLen($sMsg) > 150 ? StringLeft($sMsg, 150) & "..." : $sMsg) & @CRLF, 0)
85+
Func WebEvents_OnMessageReceived($oWebV2M, $hGUI, $sMsg)
86+
#forceref $oWebV2M, $hGUI
87+
ConsoleWrite("> [WebEvents]: " & (StringLen($sMsg) > 150 ? StringLeft($sMsg, 150) & "..." : $sMsg) & @CRLF)
10188
Local $iSplitPos = StringInStr($sMsg, "|")
10289
Local $sCommand = $iSplitPos ? StringStripWS(StringLeft($sMsg, $iSplitPos - 1), 3) : $sMsg
10390
Local $sData = $iSplitPos ? StringTrimLeft($sMsg, $iSplitPos) : ""
10491
Local $aParts
10592

10693
Switch $sCommand
10794
Case "INIT_READY"
108-
$oWeb.ExecuteScript('window.chrome.webview.postMessage(JSON.stringify({ "type": "COM_TEST", "status": "OK" }));')
95+
Local $sJavaScript = 'window.chrome.webview.postMessage(JSON.stringify({ "type": "COM_TEST", "status": "OK" }));'
96+
_NetWebView2_ExecuteScript($oWebV2M, $sJavaScript, $NETWEBVIEW2_EXECUTEJS_MODE0_FIREANDFORGET)
10997

11098
Case "WINDOW_RESIZED"
11199
$aParts = StringSplit($sData, "|")
112100
If $aParts[0] >= 2 Then
113101
Local $iW = Int($aParts[1]), $iH = Int($aParts[2])
114102
; Filter minor resize glitches
115-
If $iW > 50 And $iH > 50 Then __DW("WINDOW_RESIZED : " & $iW & "x" & $iH & @CRLF)
103+
If $iW > 50 And $iH > 50 Then ConsoleWrite(">>WINDOW_RESIZED : " & $iW & "x" & $iH & @CRLF)
116104
EndIf
117105
EndSwitch
118106
EndFunc ;==>WebEvents_OnMessageReceived
119107

120108
; Handles custom messages from JavaScript (window.chrome.webview.postMessage)
121-
Func JavaScript_OnMessageReceived($sMsg)
122-
__DW(">>> [JavaScript]: " & (StringLen($sMsg) > 150 ? StringLeft($sMsg, 150) & "..." : $sMsg) & @CRLF, 0)
109+
Func JavaScript_OnMessageReceived($oWebV2M, $hGUI, $sMsg)
110+
#forceref $oWebV2M, $hGUI
111+
ConsoleWrite("> [JavaScript]: " & (StringLen($sMsg) > 150 ? StringLeft($sMsg, 150) & "..." : $sMsg) & @CRLF)
123112
Local $sFirstChar = StringLeft($sMsg, 1)
124113

125114
; 1. JSON Messaging
126115
If $sFirstChar = "{" Or $sFirstChar = "[" Then
127-
__DW("+> : Processing JSON Messaging..." & @CRLF)
128-
Local $oJson = ObjCreate("NetJson.Parser")
129-
If Not IsObj($oJson) Then Return ConsoleWrite("!> Error: Failed to create NetJson object." & @CRLF)
116+
ConsoleWrite(">> : Processing JSON Messaging..." & @CRLF)
117+
Local $oJSBridgeon = _NetJson_CreateParser()
118+
If @error Then Return ConsoleWrite("!> Error: Failed to create NetJson object." & @CRLF)
130119

131-
$oJson.Parse($sMsg)
132-
Local $sJobType = $oJson.GetTokenValue("type")
120+
$oJSBridgeon.Parse($sMsg)
121+
Local $sJobType = $oJSBridgeon.GetTokenValue("type")
133122

134123
Switch $sJobType
135124
Case "COM_TEST"
136-
__DW("- COM_TEST Confirmed: " & $oJson.GetTokenValue("status") & @CRLF)
125+
ConsoleWrite(">>> COM_TEST Confirmed: " & $oJSBridgeon.GetTokenValue("status") & @CRLF)
137126
EndSwitch
138127

139128
Else
140129
; 2. Legacy / Native Pipe-Delimited Messaging
141-
__DW("+> : Legacy / Native Pipe-Delimited Messaging..." & @CRLF, 0)
130+
ConsoleWrite(">> : Legacy / Native Pipe-Delimited Messaging..." & @CRLF)
142131
Local $sCommand, $sData, $iSplitPos
143132
$iSplitPos = StringInStr($sMsg, "|") - 1
144133

@@ -156,11 +145,11 @@ Func JavaScript_OnMessageReceived($sMsg)
156145
If UBound($aClickData) >= 2 Then
157146
Local $sKey = StringStripWS($aClickData[0], 3)
158147
Local $sVal = StringStripWS($aClickData[1], 3)
159-
__DW("+++ Property: " & $sKey & " | Value: " & $sVal & @CRLF)
148+
ConsoleWrite(">>> Property: " & $sKey & " | Value: " & $sVal & @CRLF)
160149
EndIf
161150

162151
Case "COM_TEST"
163-
__DW("- Status: Legacy COM_TEST: " & $sData & @CRLF)
152+
ConsoleWrite(">>> Status: Legacy COM_TEST: " & $sData & @CRLF)
164153

165154
Case "CSV_UPDATED"
166155
; Clean up literal \n sent by JS to real @CRLF for AutoIt
@@ -172,24 +161,20 @@ Func JavaScript_OnMessageReceived($sMsg)
172161
If $hFile <> -1 Then
173162
FileWrite($hFile, $sCleanData)
174163
FileClose($hFile)
175-
__DW("- CSV saved to file successfully!")
164+
ConsoleWrite(">>> CSV saved to file successfully!")
176165
EndIf
177166

178167
Case "ERROR"
179-
__DW("! Status: " & $sData & @CRLF)
168+
ConsoleWrite("! Status: " & $sData & @CRLF)
180169
EndSwitch
181170
EndIf
182171
EndFunc ;==>JavaScript_OnMessageReceived
183172

184-
Func WebEvents_OnContextMenuRequested($sLink, $iX, $iY, $sSelection)
185-
#forceref $sLink, $iX, $iY, $sSelection
186-
EndFunc ;==>WebEvents_OnContextMenuRequested
187-
188173
#EndRegion ; === EVENT HANDLERS ===
189174

190175
#Region ; === UTILS ===
191176

192-
Func _Web_CSVViewer(ByRef $oWeb, $sFileData = "")
177+
Func _Web_CSVViewer(ByRef $oWebV2M, $sFileData = "")
193178
Local $sSafeData = StringReplace($sFileData, "\", "\\")
194179
$sSafeData = StringReplace($sSafeData, "'", "\'")
195180
$sSafeData = StringReplace($sSafeData, @CRLF, "\n")
@@ -235,41 +220,11 @@ Func _Web_CSVViewer(ByRef $oWeb, $sFileData = "")
235220
"<div class='container'><table id='table'></table></div>" & _
236221
"<script>" & $sJS & "</script></body></html>"
237222

238-
_NetWebView2_NavigateToString($oWeb, $sHTML)
223+
_NetWebView2_NavigateToString($oWebV2M, $sHTML)
239224
EndFunc ;==>_Web_CSVViewer
240225

241226
Func _ErrFunc($oError) ; Global COM Error Handler
242227
ConsoleWrite('@@ Line(' & $oError.scriptline & ') : COM Error Number: (0x' & Hex($oError.number, 8) & ') ' & $oError.windescription & @CRLF)
243228
EndFunc ;==>_ErrFunc
244229

245-
; Debug Write utility
246-
Func __DW($sString, $iErrorNoLineNo = 1, $iLine = @ScriptLineNumber, $iError = @error, $iExtended = @extended)
247-
If Not $g_DebugInfo Then Return SetError($iError, $iExtended, 0)
248-
Local $iReturn
249-
If $iErrorNoLineNo = 1 Then
250-
If $iError Then
251-
$iReturn = ConsoleWrite("@@(" & $iLine & ") :: @error:" & $iError & ", @extended:" & $iExtended & ", " & $sString)
252-
Else
253-
$iReturn = ConsoleWrite("+>(" & $iLine & ") :: " & $sString)
254-
EndIf
255-
Else
256-
$iReturn = ConsoleWrite($sString)
257-
EndIf
258-
Return SetError($iError, $iExtended, $iReturn)
259-
EndFunc ;==>__DW
260-
261-
Func _NetJson_New($sInitialJson = "{}")
262-
Local $oParser = ObjCreate("NetJson.Parser")
263-
If Not IsObj($oParser) Then Return SetError(1, 0, 0)
264-
If $sInitialJson <> "" Then $oParser.Parse($sInitialJson)
265-
Return $oParser
266-
EndFunc ;==>_NetJson_New
267-
268-
Func _ExitApp()
269-
If IsObj($oWeb) Then $oWeb.Cleanup()
270-
$oWeb = 0
271-
$oJS = 0
272-
Exit
273-
EndFunc ;==>_ExitApp
274-
275230
#EndRegion ; === UTILS ===

0 commit comments

Comments
 (0)