Skip to content

Commit 68d49e2

Browse files
authored
Update NetWebView2Lib.au3
1 parent c9de980 commit 68d49e2

1 file changed

Lines changed: 101 additions & 4 deletions

File tree

NetWebView2Lib.au3

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ Global Enum _ ; $NETWEBVIEW2_MESSAGE__* are set by __NetWebView2_WebViewEvents__
6767
; #FUNCTION# ====================================================================================================================
6868
; Name ..........: _NetWebView2_Initialize
6969
; Description ...:
70-
; Syntax ........: _NetWebView2_Initialize(ByRef $oWebV2M, $hGUI, $sProfileDirectory[, $i_Left = 0[, $i_Top = 0[, $i_Width = 0[,
71-
; $i_Height = 0[, $b_LoadWait = True[, $b_SetAutoResize = True[, $b_AreDevToolsEnabled = True[,
72-
; $i_ZoomFactor = 1.0[, $s_BackColor = "0x2B2B2B"]]]]]]]]])
70+
; Syntax ........: _NetWebView2_Initialize(ByRef $oWebV2M, $hGUI, $sProfileDirectory[, $i_Left = 0[, $i_Top = 0[, $i_Width = 0[, $i_Height = 0[, $b_LoadWait = True[, $b_SetAutoResize = True[, $b_AreDevToolsEnabled = True[, $i_ZoomFactor = 1.0[, $s_BackColor = "0x2B2B2B"[, $bInitializeConsoleBridge = False]]]]]]]]]])
7371
; Parameters ....: $oWebV2M - [in/out] an object.
7472
; $hGUI - a handle value.
7573
; $sProfileDirectory - a string value.
@@ -82,6 +80,7 @@ Global Enum _ ; $NETWEBVIEW2_MESSAGE__* are set by __NetWebView2_WebViewEvents__
8280
; $b_AreDevToolsEnabled- [optional] a boolean value. Default is True.
8381
; $i_ZoomFactor - [optional] an integer value. Default is 1.0.
8482
; $s_BackColor - [optional] a string value. Default is "0x2B2B2B".
83+
; $bInitializeConsoleBridge- [optional] a boolean value. Default is False.
8584
; Return values .: None
8685
; Author ........: mLipok, ioa747
8786
; Modified ......:
@@ -90,23 +89,30 @@ Global Enum _ ; $NETWEBVIEW2_MESSAGE__* are set by __NetWebView2_WebViewEvents__
9089
; Link ..........:
9190
; Example .......: No
9291
; ===============================================================================================================================
93-
Func _NetWebView2_Initialize(ByRef $oWebV2M, $hGUI, $sProfileDirectory, $i_Left = 0, $i_Top = 0, $i_Width = 0, $i_Height = 0, $b_LoadWait = True, $b_SetAutoResize = True, $b_AreDevToolsEnabled = True, $i_ZoomFactor = 1.0, $s_BackColor = "0x2B2B2B")
92+
Func _NetWebView2_Initialize(ByRef $oWebV2M, $hGUI, $sProfileDirectory, $i_Left = 0, $i_Top = 0, $i_Width = 0, $i_Height = 0, $b_LoadWait = True, $b_SetAutoResize = True, $b_AreDevToolsEnabled = True, $i_ZoomFactor = 1.0, $s_BackColor = "0x2B2B2B", $bInitializeConsoleBridge = False)
9493
Local Const $s_Prefix = "[_NetWebView2_Initialize]:" & " GUI:" & $hGUI & " ProfileDirectory:" & $sProfileDirectory & " LEFT:" & $i_Left & " TOP:" & $i_Top & " WIDTH" & $i_Width & " HEIGHT:" & $i_Height & " LOADWAIT:" & $b_LoadWait & " SETAUTORESIZE:" & $b_SetAutoResize & " SetAutoResize:" & $b_AreDevToolsEnabled & " ZoomFactor:" & $i_ZoomFactor & " BackColor:" & $s_BackColor
9594

9695
; ⚠️ Important: Enclose ($hGUI) in parentheses to force "Pass-by-Value".
9796
; This prevents the COM layer from changing the AutoIt variable type from Ptr to Int64.
9897
Local $iInit = $oWebV2M.Initialize(($hGUI), $sProfileDirectory, $i_Left, $i_Top, $i_Width, $i_Height)
9998
If @error Then Return SetError(@error, @extended, $iInit)
10099

100+
ConsoleWrite("#" & @ScriptLineNumber & " - " & @CRLF)
101101
Do ; Wait for the engine to be ready before navigating
102102
Sleep(50)
103103
Until $b_LoadWait And $oWebV2M.IsReady
104+
ConsoleWrite("#" & @ScriptLineNumber & " - " & @CRLF)
104105

105106
; WebView2 Configuration
106107
$oWebV2M.SetAutoResize($b_SetAutoResize) ; Using SetAutoResize(True) to skip WM_SIZE
107108
$oWebV2M.AreDevToolsEnabled = $b_AreDevToolsEnabled ; Allow F12
108109
$oWebV2M.ZoomFactor = $i_ZoomFactor
109110
$oWebV2M.BackColor = $s_BackColor
111+
112+
If $bInitializeConsoleBridge Then
113+
$oWebV2M.AddInitializationScript(__Get_Core_Bridge_JS())
114+
EndIf
115+
110116
If @error Then __NetWebView2_Log(@ScriptLineNumber, $s_Prefix & " Manager Creation ERROR", 1)
111117
Return SetError(@error, $oWebV2M.GetBrowserProcessId(), '')
112118
EndFunc ;==>_NetWebView2_Initialize
@@ -167,6 +173,7 @@ Func _NetWebView2_GetBridge(ByRef $oWebV2M, $s_fnEventPrefix = "")
167173

168174
If $s_fnEventPrefix Then $_g_sNetWebView2_User_JSEvents = $s_fnEventPrefix
169175
ObjEvent($oWebJS, "__NetWebView2_JSEvents__", "IBridgeEvents")
176+
170177
Return SetError(@error, @extended, $oWebJS)
171178
EndFunc ;==>_NetWebView2_GetBridge
172179

@@ -616,6 +623,96 @@ Func __NetWebView2_ObjName_FlagsValue(ByRef $oObj)
616623

617624
ConsoleWrite($sInfo & @CRLF)
618625
EndFunc ;==>__NetWebView2_ObjName_FlagsValue
626+
627+
; #INTERNAL_USE_ONLY# ===========================================================================================================
628+
; Name ..........: __Get_Core_Bridge_JS
629+
; Description ...: Get JavaScript for Bridge
630+
; Syntax ........: __Get_Core_Bridge_JS()
631+
; Parameters ....: None
632+
; Return values .: JavaScript for Bridge
633+
; Author ........: ioa747
634+
; Modified ......: mLipok
635+
; Remarks .......:
636+
; Related .......:
637+
; Link ..........:
638+
; Example .......: No
639+
; ===============================================================================================================================
640+
Func __Get_Core_Bridge_JS()
641+
Local $sJS = _
642+
"/**" & @CRLF & _
643+
" * NetWebView2Lib Core Bridge" & @CRLF & _
644+
" * Handles Console Hijacking and Global Error Reporting" & @CRLF & _
645+
" */" & @CRLF & _
646+
"" & @CRLF & _
647+
"(function() {" & @CRLF & _
648+
" // 1. Configuration & State" & @CRLF & _
649+
" window.NET_BRIDGE_ENABLED = true;" & @CRLF & _
650+
"" & @CRLF & _
651+
" /**" & @CRLF & _
652+
" * Centralized message dispatcher to AutoIt" & @CRLF & _
653+
" */" & @CRLF & _
654+
" const dispatchToAutoIt = (data) => {" & @CRLF & _
655+
" try {" & @CRLF & _
656+
" if (window.chrome && window.chrome.webview) {" & @CRLF & _
657+
" window.chrome.webview.postMessage(JSON.stringify(data));" & @CRLF & _
658+
" }" & @CRLF & _
659+
" } catch (e) {" & @CRLF & _
660+
" // Silent fail if bridge is not fully ready" & @CRLF & _
661+
" }" & @CRLF & _
662+
" };" & @CRLF & _
663+
"" & @CRLF & _
664+
" /**" & @CRLF & _
665+
" * Console Hijacking Logic" & @CRLF & _
666+
" */" & @CRLF & _
667+
" const originalConsole = {" & @CRLF & _
668+
" log: console.log," & @CRLF & _
669+
" error: console.error," & @CRLF & _
670+
" warn: console.warn," & @CRLF & _
671+
" info: console.info" & @CRLF & _
672+
" };" & @CRLF & _
673+
"" & @CRLF & _
674+
" const createWrappedConsole = (type) => {" & @CRLF & _
675+
" return function() {" & @CRLF & _
676+
" // Send to AutoIt" & @CRLF & _
677+
" dispatchToAutoIt({" & @CRLF & _
678+
" type: ""CONSOLE_LOG""," & @CRLF & _
679+
" level: type.toUpperCase()," & @CRLF & _
680+
" message: Array.from(arguments).map(arg => " & @CRLF & _
681+
" (typeof arg === 'object') ? JSON.stringify(arg) : String(arg)" & @CRLF & _
682+
" ).join(' ')," & @CRLF & _
683+
" timestamp: new Date().toISOString()" & @CRLF & _
684+
" });" & @CRLF & _
685+
" // Keep original browser behavior" & @CRLF & _
686+
" originalConsole[type].apply(console, arguments);" & @CRLF & _
687+
" };" & @CRLF & _
688+
" };" & @CRLF & _
689+
"" & @CRLF & _
690+
" // Replace standard console methods" & @CRLF & _
691+
" console.log = createWrappedConsole('log');" & @CRLF & _
692+
" console.error = createWrappedConsole('error');" & @CRLF & _
693+
" console.warn = createWrappedConsole('warn');" & @CRLF & _
694+
" console.info = createWrappedConsole('info');" & @CRLF & _
695+
"" & @CRLF & _
696+
" /**" & @CRLF & _
697+
" * 2. Global Runtime Error Handler" & @CRLF & _
698+
" */" & @CRLF & _
699+
" window.onerror = function(message, source, lineno, colno, error) {" & @CRLF & _
700+
" dispatchToAutoIt({" & @CRLF & _
701+
" type: ""JS_ERROR""," & @CRLF & _
702+
" message: message," & @CRLF & _
703+
" source: source," & @CRLF & _
704+
" line: lineno," & @CRLF & _
705+
" column: colno," & @CRLF & _
706+
" stack: error ? error.stack : """"" & @CRLF & _
707+
" });" & @CRLF & _
708+
" return false; // Let browser handle it as well" & @CRLF & _
709+
" };" & @CRLF & _
710+
"" & @CRLF & _
711+
" // Signal that bridge is active" & @CRLF & _
712+
" dispatchToAutoIt({ type: ""SYSTEM"", message: ""Core Bridge Injected"" });" & @CRLF & _
713+
"})();"
714+
Return $sJS
715+
EndFunc ;==>__Get_Core_Bridge_JS
619716
#EndRegion ; NetWebView2Lib UDF - #INTERNAL_USE_ONLY#
620717

621718
#Region ; NetWebView2Lib UDF - === EVENT HANDLERS ===

0 commit comments

Comments
 (0)