Skip to content

Commit 9caee9a

Browse files
authored
updating examples
updating examples with the current version of NetWebView2Lib
1 parent 43d67a5 commit 9caee9a

1 file changed

Lines changed: 47 additions & 98 deletions

File tree

examples/012-GetCookies.au3

Lines changed: 47 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88
#include <Misc.au3>
99
#include "..\NetWebView2Lib.au3"
1010

11-
_VersionChecker("1.2.0.0") ; DLL Version Check
11+
; 012-GetCookies.au3
1212

13-
; Register the exit function
14-
OnAutoItExitRegister(_CleanExit)
13+
_VersionChecker("1.2.0.0") ; DLL Version Check
1514

1615
; Global objects handler for COM objects
17-
Global $oManager, $oBridge
18-
Global $oMyError = ObjEvent("AutoIt.Error", "_ErrFunc")
19-
Global $sProfileDirectory = @ScriptDir & "\NetWebView2Lib-UserDataFolder"
16+
Global $oWebV2M, $oJSBridge
17+
Global $oMyError = ObjEvent("AutoIt.Error", _ErrFunc)
2018

2119
; Global variables for data management
2220
Global $hGUI, $idURL, $idStatusLabel
@@ -69,7 +67,6 @@ Func _Example()
6967
GUICtrlSetResizing(-1, $GUI_DOCKALL)
7068
GUICtrlSetTip(-1, "SetZoom")
7169

72-
7370
; Button GoBack
7471
Local $idBtnGoBack = GUICtrlCreateButton(ChrW(59179), 185, 10, 25, 25)
7572
GUICtrlSetFont(-1, 10, 400, 0, "Segoe Fluent Icons")
@@ -96,10 +93,18 @@ Func _Example()
9693
GUICtrlSetColor(-1, 0xFFFFFF)
9794
GUICtrlSetResizing(-1, $GUI_DOCKSIZE + $GUI_DOCKBOTTOM)
9895

99-
_WebView2(0, 45, 1285, 800) ; Get the WebView2 Manager object
96+
; Initialize WebView2 Manager and register events
97+
$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", _
98+
"WebView_", "--mute-audio")
99+
If @error Then Return SetError(@error, @extended, $oWebV2M)
100100

101-
; Register the WM_SIZE message to handle window resizing
102-
GUIRegisterMsg($WM_SIZE, "WM_SIZE")
101+
; create JavaScript Bridge object
102+
$oJSBridge = _NetWebView2_GetBridge($oWebV2M, "Bridge_")
103+
If @error Then Return SetError(@error, @extended, $oWebV2M)
104+
105+
; initialize browser - put it on the GUI
106+
Local $sProfileDirectory = @ScriptDir & "\NetWebView2Lib-UserDataFolder"
107+
_NetWebView2_Initialize($oWebV2M, $hGUI, $sProfileDirectory, 0, 45, 1285, 800, True, True, 1, "0x2B2B2B")
103108

104109
; Register the WM_COMMAND message to handle ...
105110
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
@@ -115,42 +120,42 @@ Func _Example()
115120

116121
Case $idBtnClearBrowserData
117122
If MsgBox(36, "Confirm", "Do you want to clear your browsing data?") = 6 Then
118-
$oManager.ClearBrowserData()
123+
$oWebV2M.ClearBrowserData()
119124
ShowWebNotification("Browser history & cookies cleared!", "#f44336")
120125
EndIf
121126

122127
Case $idBtnSaveSession
123-
$oManager.GetCookies(GUICtrlRead($idURL))
128+
$oWebV2M.GetCookies(GUICtrlRead($idURL))
124129

125130
Case $idBtnRestoreSession
126131
$sURL = GUICtrlRead($idURL)
127132
Local $sDomainOnly = StringRegExpReplace($sURL, "https?://([^/]+).*", "$1")
128133
_RestoreSession($sDomainOnly)
129134

130135
Case $idBtnSetZoom
131-
$oManager.SetZoom(1.5) ; Zoom to 150%
136+
$oWebV2M.SetZoom(1.5) ; Zoom to 150%
132137
ShowWebNotification("Zoom: 150%", "#2196F3")
133138

134139
Case $idBtnResetZoom
135-
$oManager.ResetZoom() ; Reset to 100%
140+
$oWebV2M.ResetZoom() ; Reset to 100%
136141
ShowWebNotification("Zoom: 100%", "#4CAF50")
137142

138143
Case $idBtnGoBack
139-
$oManager.GoBack()
144+
$oWebV2M.GoBack()
140145

141146
Case $idBtnGoForward
142-
$oManager.GoForward()
147+
$oWebV2M.GoForward()
143148

144149
Case $idBtnStop
145-
$oManager.Stop()
150+
$oWebV2M.Stop()
146151
GUICtrlSetData($idStatusLabel, "Stop")
147152

148153
Case $idReload
149-
$oManager.Reload()
154+
$oWebV2M.Reload()
150155
GUICtrlSetData($idStatusLabel, "Reload")
151156

152157
Case $idURL
153-
_NetWebView2_Navigate($oManager, GUICtrlRead($idURL))
158+
_NetWebView2_Navigate($oWebV2M, GUICtrlRead($idURL))
154159
GUICtrlSetData($idStatusLabel, "Navigate: " & GUICtrlRead($idURL))
155160

156161
EndSwitch
@@ -162,46 +167,20 @@ Func _Example()
162167

163168
WEnd
164169

165-
EndFunc ;==>_Example
166-
;---------------------------------------------------------------------------------------
167-
Func _CleanExit() ; CleanExit
168-
; Check if the object exists before calling methods to avoid COM errors during crash
169-
If IsObj($oManager) Then
170-
$oManager.Cleanup()
171-
EndIf
172-
173-
; Release the event sinks
174-
$oManager = 0
175-
$oBridge = 0
176-
;~ $oEvtManager = 0
177-
;~ $oEvtBridge = 0
178-
$oMyError = 0
179-
170+
_NetWebView2_CleanUp($oWebV2M, $oJSBridge)
180171
ConsoleWrite("--> Application exited cleanly." & @CRLF)
181-
EndFunc ;==>_CleanExit
182-
;---------------------------------------------------------------------------------------
183-
Func _WebView2($iLeft = 0, $iTop = 0, $iWidth = 0, $iHeight = 0)
184-
185-
; Get the WebView2 Manager object and register events
186-
$oManager = ObjCreate("NetWebView2.Manager")
187-
ObjEvent($oManager, "WebView_", "IWebViewEvents")
188172

189-
; Get the bridge object and register events
190-
$oBridge = $oManager.GetBridge()
191-
ObjEvent($oBridge, "Bridge_", "IBridgeEvents")
192-
193-
; ⚠️ Important: Enclose ($hGUI) in parentheses to force "Pass-by-Value".
194-
; This prevents the COM layer from changing the AutoIt variable type from Ptr to Int64.
195-
$oManager.Initialize(($hGUI), $sProfileDirectory, $iLeft, $iTop, $iWidth, $iHeight)
196-
EndFunc ;==>_WebView2
173+
EndFunc ;==>_Example
197174
;---------------------------------------------------------------------------------------
198-
Func Bridge_OnMessageReceived($sMessage)
175+
Func Bridge_OnMessageReceived($oWebV2M, $hGUI, $sMessage)
176+
#forceref $oWebV2M, $hGUI
199177
; Handles data received from the JavaScript 'postMessage'
200-
ConsoleWrite("+> [JS MESSAGE]: " & $sMessage & @CRLF)
178+
ConsoleWrite("> [JS MESSAGE]: " & $sMessage & @CRLF)
201179
EndFunc ;==>Bridge_OnMessageReceived
202180
;---------------------------------------------------------------------------------------
203-
Func WebView_OnMessageReceived($sMessage)
204-
ConsoleWrite("+> [CORE EVENT]: " & $sMessage & @CRLF)
181+
Func WebView_OnMessageReceived($oWebV2M, $hGUI, $sMessage)
182+
#forceref $oWebV2M, $hGUI
183+
ConsoleWrite("> [CORE EVENT]: " & $sMessage & @CRLF)
205184
Local Static $sCurentURL = "", $sLastRestoredDomain = ""
206185
Local $sDomain
207186

@@ -211,15 +190,15 @@ Func WebView_OnMessageReceived($sMessage)
211190

212191
Switch $sCommand
213192
Case "INIT_READY"
214-
_NetWebView2_Navigate($oManager, GUICtrlRead($idURL))
193+
_NetWebView2_Navigate($oWebV2M, GUICtrlRead($idURL))
215194
GUISetState(@SW_SHOW, $hGUI)
216195

217196
Case "NAV_STARTING"
218197
$sCurentURL = GUICtrlRead($idURL)
219198
$sDomain = StringRegExpReplace($sCurentURL, "https?://([^/]+).*", "$1")
220199

221200
If $g_bAutoRestoreSession And $sDomain <> $sLastRestoredDomain Then
222-
ConsoleWrite("> Auto-Restore: Initializing for " & $sDomain & @CRLF)
201+
ConsoleWrite(">>> Auto-Restore: Initializing for " & $sDomain & @CRLF)
223202
_RestoreSession($sDomain)
224203
$sLastRestoredDomain = $sDomain ; Remember that we already restored this domain
225204
EndIf
@@ -237,7 +216,7 @@ Func WebView_OnMessageReceived($sMessage)
237216
$sCurentURL = $aParts[2]
238217
GUICtrlSetData($idURL, $sCurentURL)
239218
GUICtrlSendMsg($idURL, $EM_SETSEL, 0, 0)
240-
$oManager.WebViewSetFocus() ; We give focus to the browser
219+
$oWebV2M.WebViewSetFocus() ; We give focus to the browser
241220
EndIf
242221

243222
Case "COOKIES_B64"
@@ -259,11 +238,11 @@ Func WebView_OnMessageReceived($sMessage)
259238
EndFunc ;==>WebView_OnMessageReceived
260239
;---------------------------------------------------------------------------------------
261240
Func _ProcessCookies($sURL, $sBase64)
262-
Local $oJson = ObjCreate("NetJson.Parser")
263-
If Not IsObj($oJson) Then Return
241+
Local $oJson = _NetJson_CreateParser()
242+
If @error Then Return ConsoleWrite("!!! Erorr CreateParser" & @CRLF)
264243

265244
Local $sDomainOnly = StringRegExpReplace($sURL, "https?://([^/]+).*", "$1")
266-
Local $sDecodedJson = $oManager.DecodeB64($sBase64) ; _Base64Decode($sBase64)
245+
Local $sDecodedJson = $oWebV2M.DecodeB64($sBase64) ; _Base64Decode($sBase64)
267246
$oJson.Parse($sDecodedJson)
268247

269248
Local $iTotal = $oJson.GetArrayLength("")
@@ -307,7 +286,7 @@ Func _ProcessCookies($sURL, $sBase64)
307286
If Not FileExists(@ScriptDir & "\Session") Then DirCreate(@ScriptDir & "\Session")
308287
Local $sLogFile = @ScriptDir & "\Session\cookies_" & $sDomainOnly & ".json"
309288
$oJson.SaveToFile($sLogFile)
310-
ConsoleWrite("> Clean session saved (" & $iFoundCount & " cookies) to: " & $sLogFile & @CRLF)
289+
ConsoleWrite(">>> Clean session saved (" & $iFoundCount & " cookies) to: " & $sLogFile & @CRLF)
311290
EndIf
312291
EndFunc ;==>_ProcessCookies
313292
;---------------------------------------------------------------------------------------
@@ -321,7 +300,7 @@ Func _RestoreSession($sDomain)
321300
EndIf
322301

323302
Local $iTotal = $oJson.GetArrayLength("")
324-
ConsoleWrite("> Restoring " & $iTotal & " cookies for " & $sDomain & "..." & @CRLF)
303+
ConsoleWrite(">>> Restoring " & $iTotal & " cookies for " & $sDomain & "..." & @CRLF)
325304

326305
Local $iInjected = 0
327306

@@ -337,7 +316,7 @@ Func _RestoreSession($sDomain)
337316
; Check if the cookie domain matches the target session domain
338317
If StringInStr($sRawDom, $sDomain) Or StringInStr($sDomain, StringTrimLeft($sRawDom, 1)) Then
339318
; Inject the cookie into the WebView2 manager
340-
$oManager.AddCookie($sRawName, $sRawValue, $sRawDom, $sRawPath)
319+
$oWebV2M.AddCookie($sRawName, $sRawValue, $sRawDom, $sRawPath)
341320

342321
ConsoleWrite(StringFormat(" [%d] %-15s | Domain: %-15s | Val: %s...\n", _
343322
$i, $sRawName, $sDomain, StringLeft($sRawValue, 10)))
@@ -347,7 +326,7 @@ Func _RestoreSession($sDomain)
347326

348327
; Apply changes if cookies were injected
349328
If $iInjected > 0 Then
350-
$oManager.Reload()
329+
$oWebV2M.Reload()
351330
ShowWebNotification("Session Restored for " & $sDomain, "#2196F3")
352331
EndIf
353332
EndFunc ;==>_RestoreSession
@@ -368,28 +347,8 @@ Func ShowWebNotification($sMessage, $sBgColor = "#4CAF50", $iDuration = 3000) ;
368347
" if(target) { target.style.opacity = '0'; setTimeout(() => target.remove(), 500); }" & _
369348
"}, " & $iDuration & ");"
370349

371-
$oManager.ExecuteScript($sJS)
350+
$oWebV2M.ExecuteScript($sJS)
372351
EndFunc ;==>ShowWebNotification
373-
374-
;---------------------------------------------------------------------------------------
375-
Func WM_SIZE($hWnd, $iMsg, $wParam, $lParam) ; Synchronizes WebView size with the GUI window
376-
#forceref $hWnd, $iMsg
377-
378-
If $wParam = 1 Then Return $GUI_RUNDEFMSG ; 1 = SIZE_MINIMIZED
379-
380-
Local $iNewWidth = BitAND($lParam, 0xFFFF)
381-
Local $iNewHeight = BitShift($lParam, 16)
382-
383-
If IsObj($oManager) Then
384-
; Make sure the dimensions are positive
385-
Local $iW = $iNewWidth - 0
386-
Local $iH = $iNewHeight - 65
387-
If $iW < 10 Then $iW = 10
388-
If $iH < 10 Then $iH = 10
389-
$oManager.Resize($iW, $iH)
390-
EndIf
391-
Return $GUI_RUNDEFMSG
392-
EndFunc ;==>WM_SIZE
393352
;---------------------------------------------------------------------------------------
394353
Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
395354
#forceref $hWnd, $iMsg
@@ -407,17 +366,7 @@ Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
407366
EndFunc ;==>WM_COMMAND
408367
;---------------------------------------------------------------------------------------
409368
Func _ErrFunc($oError) ; User's COM error function. Will be called if COM error occurs
410-
; Do anything here.
411-
ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
412-
@TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
413-
@TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
414-
@TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
415-
@TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
416-
@TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
417-
@TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
418-
@TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
419-
@TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
420-
@TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
369+
ConsoleWrite('@@ Line(' & $oError.scriptline & ') : COM Error Number: (0x' & Hex($oError.number, 8) & ') ' & $oError.windescription & @CRLF)
421370
EndFunc ;==>_ErrFunc
422371
;---------------------------------------------------------------------------------------
423372
Func _VersionChecker($sRequired = "1.0.0.0")
@@ -439,9 +388,9 @@ Func _VersionChecker($sRequired = "1.0.0.0")
439388
; Get the version of the actual DLL
440389
Local $sCurrent = FileGetVersion($sDllPath)
441390

442-
ConsoleWrite("+> Found TLB: " & $sTlbPath & @CRLF)
443-
ConsoleWrite("+> Checking DLL: " & $sDllPath & @CRLF)
444-
ConsoleWrite("+> Current Version: " & $sCurrent & @CRLF)
391+
ConsoleWrite("++> Found TLB: " & $sTlbPath & @CRLF)
392+
ConsoleWrite("++> Checking DLL: " & $sDllPath & @CRLF)
393+
ConsoleWrite("++> Current Version: " & $sCurrent & @CRLF)
445394

446395
; Compare
447396
If _VersionCompare($sCurrent, $sRequired) = -1 Then

0 commit comments

Comments
 (0)