Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 72 additions & 49 deletions NetWebView2Lib.au3
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#Tidy_Parameters=/tcb=-1

; NetWebView2Lib.au3 - Script Version: 2.2.1-alpha (2026.03.20.09) ๐Ÿšฉ
; NetWebView2Lib.au3 - Script Version: 2.2.1-alpha1 (2026.04.14.10) ๐Ÿšฉ

#include <Array.au3>
#include <GUIConstantsEx.au3>
Expand Down Expand Up @@ -112,6 +112,7 @@ Global Enum _ ; $NETWEBVIEW2_MESSAGE__* are set by mainly by __NetWebView2_Event
$NETWEBVIEW2_MESSAGE__COOKIE_ADD_ERROR, _
$NETWEBVIEW2_MESSAGE__BLOCKED_AD, _
$NETWEBVIEW2_MESSAGE__DOWNLOAD_STARTING, _
$NETWEBVIEW2_MESSAGE__DOWNLOAD_CANCELLED, _ ; <<--( NEW )--<<
$NETWEBVIEW2_MESSAGE__DOWNLOAD_IN_PROGRESS, _
$NETWEBVIEW2_MESSAGE__DOWNLOAD_INTERRUPTED, _
$NETWEBVIEW2_MESSAGE__DOWNLOAD_COMPLETED, _
Expand Down Expand Up @@ -152,12 +153,13 @@ Global Enum _ ; Indicates the reason for the process failure.
; $bVerbose = False]]]])
; Parameters ....: $sUserAgent - [optional] a string value. Default is "".
; $s_fnEventPrefix - [optional] a string value. Default is "".
; $s_AddBrowserArgs - [optional] a string value. Default is "". Allows passing command-line switches (e.g., --disable-gpu, --mute-audio, --proxy-server="...") to the Chromium engine.
; $s_AddBrowserArgs - [optional] a string value. Default is "". Allows passing command-line switches (e.g., --disable-gpu --mute-audio --proxy-server="...") to the Chromium engine.
; $bVerbose - [optional] True/False - Enable/Disable diagnostic logging. Default is False = Disabled.
; Return values .: None
; Author ........: mLipok, ioa747
; Modified ......:
; Remarks .......: $s_AddBrowserArgs must be set before calling Initialize().
; Remarks .......: $s_AddBrowserArgs must be set before calling _NetWebView2_Initialize().
; Multiple arguments must be separated by a SPACE, not a comma (e.g., "--mute-audio --disable-gpu").
; Related .......:
; Link ..........: https://www.chromium.org/developers/how-tos/run-chromium-with-flags
; Link ..........: https://chromium.googlesource.com/chromium/src/+/main/docs/configuration.md#switches
Expand Down Expand Up @@ -2102,6 +2104,10 @@ Volatile Func __NetWebView2_Events__OnMessageReceived($oWebV2M, $hGUI, $sMsg)
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & " COMMAND:" & $sCommand, 1)
__NetWebView2_LastMessage_KEEPER($oWebV2M, $NETWEBVIEW2_MESSAGE__DOWNLOAD_STARTING)

Case "DOWNLOAD_CANCELLED"
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & " COMMAND:" & $sCommand, 1)
__NetWebView2_LastMessage_KEEPER($oWebV2M, $NETWEBVIEW2_MESSAGE__DOWNLOAD_CANCELLED) ; <<--( NEW )--<<

Case "BROWSER_GOT_FOCUS"
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & " COMMAND:" & $sCommand, 1)
__NetWebView2_LastMessage_KEEPER($oWebV2M, $NETWEBVIEW2_MESSAGE__BROWSER_GOT_FOCUS)
Expand Down Expand Up @@ -2405,46 +2411,55 @@ EndFunc ;==>__NetWebView2_Events__OnContextMenu

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __NetWebView2_Events__OnWebResourceResponseReceived
; Description ...:
; Syntax ........: __NetWebView2_Events__OnWebResourceResponseReceived($oWebV2M, $hGUI, $iStatusCode, $sReasonPhrase,
; $sRequestUrl)
; Description ...: Internal handler for the WebResourceResponseReceived event.
; Syntax ........: __NetWebView2_Events__OnWebResourceResponseReceived($oWebV2M, $hGUI, $oArgs)
; Parameters ....: $oWebV2M - WebView2 object that fired the event
; $hGUI - a handle to Window that fired the event
; $iStatusCode - HTTP StatusCode
; $sReasonPhrase - StatusCode rephrased to human resonable string
; $sRequestUrl - the URL that fired the event
; $hGUI - A handle to Window that fired the event
; $oArgs - An Event Arguments Object containing:
; | .StatusCode (int) [RO]: The HTTP status code (e.g., 200, 404).
; | .ReasonPhrase (string) [RO]: The HTTP reason phrase (e.g., "OK").
; | .RequestUri (string) [RO]: The URL of the request.
; | .IsDocument (bool) [RO]: True if the resource is the main document.
; Return values .: None
; Author ........: mLipok
; Modified ......:
; Author ........: ioa747, mLipok
; Modified ......: 2026-04-14 (Refactored to Object-Oriented Event Model)
; Remarks .......:
; Related .......:
; Link ..........: https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.webresourceresponsereceived
; Example .......: No
; Example .......: 007-HTTP_StatusCodeTracking.au3
; ===============================================================================================================================
Volatile Func __NetWebView2_Events__OnWebResourceResponseReceived($oWebV2M, $hGUI, $iStatusCode, $sReasonPhrase, $sRequestUrl)
Local Const $s_Prefix = ">>>[EVENT: OnWebResourceResponseReceived]: GUI:" & $hGUI & " HTTPStatusCode:" & $iStatusCode & " (" & $sReasonPhrase & ") URL:" & $sRequestUrl
__NetWebView2_Log(@ScriptLineNumber, (StringLen($s_Prefix) > 150 ? StringLeft($s_Prefix, 150) & "..." : $s_Prefix), 1)
Volatile Func __NetWebView2_Events__OnWebResourceResponseReceived($oWebV2M, $hGUI, $oArgs) ; <<--( NEW )--<<
Local Const $s_Prefix = ">>>[EVENT: OnWebResourceResponseReceived]: GUI:" & $hGUI & _
" HTTPStatusCode:" & $oArgs.StatusCode & " (" & $oArgs.ReasonPhrase & ") URL:" & $oArgs.RequestUri & " URL:" & $oArgs.IsDocument

__NetWebView2_Log(@ScriptLineNumber, $s_Prefix, 1)
__NetWebView2_LastMessage_KEEPER($oWebV2M, $NETWEBVIEW2_MESSAGE__RESPONSE_RECEIVED)
EndFunc ;==>__NetWebView2_Events__OnWebResourceResponseReceived

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __NetWebView2_Events__OnDownloadStarting
; Description ...:
; Syntax ........: __NetWebView2_Events__OnDownloadStarting($oWebV2M, $hGUI, $sURL, $sDefaultPath)
; Parameters ....: $oWebV2M - an object.
; $hGUI - a handle value.
; $sURL - a string value.
; $sDefaultPath - a string value.
; Description ...: Internal handler for the DownloadStarting event. Dispatches the event to the user's callback with an Args Object.
; Syntax ........: __NetWebView2_Events__OnDownloadStarting($oWebV2M, $hGUI, $oArgs)
; Parameters ....: $oWebV2M - The WebView2 Manager object.
; $hGUI - The handle to the GUI window.
; $oArgs - An Event Arguments Object containing:
; | .Uri (String) [RO]: The source URL of the download.
; | .MimeType (String) [RO]: The MIME type (e.g., "application/pdf").
; | .ContentDisposition (String) [RO]: The HTTP Content-Disposition header.
; | .TotalBytesToReceive (Int64) [RO]: Total size in bytes (-1 if unknown).
; | .ResultFilePath (String) [RW]: Get/Set the full target save path.
; | .Cancel (Boolean) [RW]: Set to True to abort the download.
; | .Handled (Boolean) [RW]: Set to True to exit the C# wait loop immediately.
; Return values .: None
; Author ........: ioa747, mLipok
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; Modified ......: 2026-04-14 (Refactored to Object-Oriented Event Model)
; Remarks .......: This event uses a 5000ms synchronization loop in C# to allow for AutoIt interaction (e.g., MsgBox).
; Related .......: _NetWebView2_CreateManager
; Link ..........: https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2downloadstartingeventargs
; Example .......: 021 - Handle Unviewable Content (MIME).au3
; ===============================================================================================================================
Volatile Func __NetWebView2_Events__OnDownloadStarting($oWebV2M, $hGUI, $sURL, $sDefaultPath)
Local Const $s_Prefix = ">>>[EVENT: OnDownloadStarting]: GUI:" & $hGUI & " URL:" & $sURL & " DEFAULT_PATH:" & $sDefaultPath
Volatile Func __NetWebView2_Events__OnDownloadStarting($oWebV2M, $hGUI, $oArgs) ; <<--( NEW )--<<
Local Const $s_Prefix = "[>>>EVENT: OnDownloadStarting]: GUI:" & $hGUI & " URL: " & $oArgs.Uri & " PATH: " & $oArgs.ResultFilePath & " MIME: " & $oArgs.MimeType
__NetWebView2_Log(@ScriptLineNumber, (StringLen($s_Prefix) > 150 ? StringLeft($s_Prefix, 150) & "..." : $s_Prefix), 1)
__NetWebView2_LastMessage_KEEPER($oWebV2M, $NETWEBVIEW2_MESSAGE__DOWNLOAD_STARTING)
EndFunc ;==>__NetWebView2_Events__OnDownloadStarting
Expand All @@ -2456,48 +2471,56 @@ EndFunc ;==>__NetWebView2_Events__OnDownloadStarting
; $iReceived_Bytes)
; Parameters ....: $oWebV2M - an object.
; $hGUI - a handle value.
; $sState - a string value.
; $sURL - a string value.
; $iTotal_Bytes - an integer value.
; $iReceived_Bytes - an integer value.
; $oArgs - An Event Arguments Object containing:
; | .Uri (String) [RO]: The source URL of the download.
; | .State (String) [RO]: The current state ("InProgress", "Completed", "Interrupted").
; | .TotalBytesToReceive (int) [RO]: Estimated total size.
; | .BytesReceived (int) [RO]: Number of bytes received so far.
; | .PercentComplete (int) [RO]: Calculation (0-100) or -1 if unknown.
; Return values .: None
; Author ........: ioa747, mLipok
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; Example .......: 006-DownloadDemo.au3
; ===============================================================================================================================
Volatile Func __NetWebView2_Events__OnDownloadStateChanged($oWebV2M, $hGUI, $sState, $sURL, $iTotal_Bytes, $iReceived_Bytes)
Local Const $s_Prefix = ">>>[EVENT: OnDownloadStateChanged]: GUI:" & $hGUI & " State:" & $sState & " URL:" & $sURL & " Total_Bytes:" & $iTotal_Bytes & " Received_Bytes:" & $iReceived_Bytes
Local $iPercent = 0
If $iTotal_Bytes > 0 Then $iPercent = Round(($iReceived_Bytes / $iTotal_Bytes), 5) * 100
Volatile Func __NetWebView2_Events__OnDownloadStateChanged($oWebV2M, $hGUI, $oArgs) ; <<--( NEW )--<<
Local $iReceived_MB = Round($oArgs.BytesReceived / 1048576, 2) ; 1024*1024
Local $iTotal_MB = Round($oArgs.TotalBytesToReceive / 1048576, 2)

Local Const $s_Prefix = "[>>>EVENT: OnDownloadStateChanged]: GUI:" & $hGUI & " State: " & $oArgs.State & " | " & _
$oArgs.PercentComplete & "% (" & $iReceived_MB & "/" & $iTotal_MB & " MB) URL: " & $oArgs.Uri

; Convert to MB for easy-to-read log
Local $iReceived_MegaBytes = Round($iReceived_Bytes / 1024 / 1024)
Local $iTotal_MegaBytes = Round($iTotal_Bytes / 1024 / 1024)
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix, 1)

Local Const $s_Message = " " & $iPercent & "% (" & $iReceived_MegaBytes & " / " & $iTotal_MegaBytes & " Mega Bytes)"
Switch $sState
Switch $oArgs.State
Case "InProgress"
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & $s_Message, 1)
__NetWebView2_LastMessage_KEEPER($oWebV2M, $NETWEBVIEW2_MESSAGE__DOWNLOAD_IN_PROGRESS)
Case "Interrupted"
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & $s_Message, 1)
__NetWebView2_LastMessage_KEEPER($oWebV2M, $NETWEBVIEW2_MESSAGE__DOWNLOAD_INTERRUPTED)
Case "Completed"
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix & $s_Message, 1)
__NetWebView2_LastMessage_KEEPER($oWebV2M, $NETWEBVIEW2_MESSAGE__DOWNLOAD_COMPLETED)
EndSwitch
EndFunc ;==>__NetWebView2_Events__OnDownloadStateChanged
EndFunc

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __NetWebView2_Events__OnAcceleratorKeyPressed
; Description ...:
; Description ...: It allows the application to intercept and handle system keys and keyboard shortcuts (Accelerators) before the WebView2 engine processes them.
; Syntax ........: __NetWebView2_Events__OnAcceleratorKeyPressed($oWebV2M, $hGUI, $oArgs)
; Parameters ....: $oWebV2M - an object.
; $hGUI - a handle value.
; $oArgs - an object.
; $oArgs - An Event Arguments Object containing:
; | .VirtualKey (uint) [RO]: The VK code of the key.
; | .KeyEventKind (int) [RO]: Type of key event (0:KeyDown, 1:KeyUp, etc.).
; | .RepeatCount (uint) [RO]: The number of times the key has repeated.
; | .ScanCode (uint) [RO]: Hardware scan code.
; | .IsExtendedKey (bool) [RO]: True if it's an extended key (e.g., right Alt).
; | .IsMenuKeyDown (bool) [RO]: True if Alt is pressed.
; | .WasKeyDown (bool) [RO]: True if the key was already down.
; | .IsKeyReleased (bool) [RO]: True if the event is a key up.
; | .KeyEventLParam (int) [RO]: The native LPARAM window message value.
; | .Handled (bool) [RW]: Set to True to prevent the browser from executing the default action.
; Return values .: None
; Author ........: ioa747, mLipok
; Modified ......:
Expand Down
Loading