|
| 1 | +#include "..\NetWebView2Lib.au3" |
| 2 | + |
| 3 | +Global $oWebV2M, $oBridge |
| 4 | + |
| 5 | +_Example_Console_Redirect() |
| 6 | + |
| 7 | +Func _Example_Console_Redirect() |
| 8 | + Local $hGUI = GUICreate("Console Redirect Test", 400, 300) |
| 9 | + |
| 10 | + ; 1. Initialize WebView2 |
| 11 | + ; Initialize WebView2 Manager and register events |
| 12 | + 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", "__MyEVENTS_Manager_", "--disable-gpu, --mute-audio") |
| 13 | + $_g_oWeb = $oWebV2M |
| 14 | + If @error Then Return SetError(@error, @extended, $oWebV2M) |
| 15 | + |
| 16 | + ; create JavaScript Bridge object |
| 17 | + Local $oJSBridge = _NetWebView2_GetBridge($oWebV2M, "BridgeEvents_") |
| 18 | + #forceref $oJSBridge |
| 19 | + If @error Then Return SetError(@error, @extended, $oWebV2M) |
| 20 | + |
| 21 | + ; initialize browser - put it on the GUI |
| 22 | + Local $sProfileDirectory = @TempDir & "\NetWebView2Lib-UserDataFolder" |
| 23 | + _NetWebView2_Initialize($oWebV2M, $hGUI, $sProfileDirectory, 0, 0, 0, 0, True, True, True, 1.2, "0x2B2B2B", True) |
| 24 | + GUISetState(@SW_SHOW) |
| 25 | + |
| 26 | + ; navigate to the page |
| 27 | + _NetWebView2_Navigate($oWebV2M, "about:blank") |
| 28 | + |
| 29 | + ; 6. TEST: Execute a console.log |
| 30 | + ; Note: We don't use ExecuteScriptWithResult here because |
| 31 | + ; the data will come back through the Bridge Event! |
| 32 | + $oWebV2M.ExecuteScript("console.log('Hello from JavaScript to AutoIt Console!');") |
| 33 | + $oWebV2M.ExecuteScript("console.error('This is a test error message');") |
| 34 | + |
| 35 | + While GUIGetMsg() <> -3 |
| 36 | + WEnd |
| 37 | + |
| 38 | + _NetWebView2_CleanUp($oWebV2M, $oBridge) |
| 39 | +EndFunc |
| 40 | + |
| 41 | +; This function handles the incoming messages from the JS Bridge |
| 42 | +Func BridgeEvents_OnMessageReceived($sMsg) |
| 43 | + ; Check if it's a JSON message from our Bridge |
| 44 | + If StringLeft($sMsg, 1) = "{" Then |
| 45 | + ; For simplicity in this example, we just print the raw message |
| 46 | + ; In a real app, you would parse the JSON to get .message and .level |
| 47 | + ConsoleWrite(">>> BRIDGE MESSAGE: " & $sMsg & @CRLF) |
| 48 | + EndIf |
| 49 | +EndFunc |
| 50 | + |
0 commit comments