|
| 1 | +#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 |
| 2 | +#include <GUIConstantsEx.au3> |
| 3 | +#include <WindowsConstants.au3> |
| 4 | +#include "..\NetWebView2Lib.au3" |
| 5 | + |
| 6 | +;~ $_g_bNetWebView2_DebugInfo = False |
| 7 | + |
| 8 | +ConsoleWrite("! MicrosoftEdgeWebview2 : version check: " & _NetWebView2_IsAlreadyInstalled() & ' ERR=' & @error & ' EXT=' & @extended & @CRLF) |
| 9 | + |
| 10 | +; 020-NavigationInterception.au3 |
| 11 | +; ## Navigation Interception & Cancellation |
| 12 | +; Description: Shows how to use the new NavigationStarting event object to |
| 13 | +; intercept specific links (e.g., https://) and cancel them, |
| 14 | +; redirecting them to the default system browser instead. |
| 15 | + |
| 16 | +_MainGUI() |
| 17 | + |
| 18 | +Func _MainGUI() |
| 19 | + Local $hGUI = GUICreate("WebView2 Navigation Interception", 640, 400, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN)) |
| 20 | + |
| 21 | + ; Create and Initialize WebView2 |
| 22 | + Local $oWebV2M = _NetWebView2_CreateManager("", "Web_") |
| 23 | + If @error Then |
| 24 | + ConsoleWrite("!> Error: Failed to create $oWebV2M object , error: " & @error & ", extended:" & @extended & @CRLF) |
| 25 | + MsgBox(16, "Error", "Failed to create $oWebV2M object!") |
| 26 | + Exit |
| 27 | + EndIf |
| 28 | + |
| 29 | + _NetWebView2_Initialize($oWebV2M, $hGUI, @ScriptDir & "\NetWebView2Lib-UserDataFolder", 0, 0, 0, 0, True, True, 1, "0x2B2B2B", False) |
| 30 | + |
| 31 | + Local $sHtml = '<html><head><style>' & _ |
| 32 | + 'body { background-color: #1e1e1e; color: white; font-family: Arial; }' & _ |
| 33 | + 'a { color: #4db8ff; text-decoration: none; }' & _ |
| 34 | + 'a:hover { color: #ffcc00; text-decoration: underline; }' & _ |
| 35 | + '</style></head><body>' & _ |
| 36 | + '<h1>Navigation Interception Demo</h1>' & _ |
| 37 | + '<p>In this demo, all external links (starting with http) are intercepted.</p>' & _ |
| 38 | + '<ul>' & _ |
| 39 | + '<li><a href="https://www.google.com">Google (Will open in Default Browser)</a></li>' & _ |
| 40 | + '<li><a href="https://www.bing.com">Bing (Will open in Default Browser)</a></li>' & _ |
| 41 | + '<li><a href="local_page.html">Local Link (Will stay in WebView)</a></li>' & _ |
| 42 | + '</ul>' & _ |
| 43 | + '</body></html>' |
| 44 | + |
| 45 | + _NetWebView2_NavigateToString($oWebV2M, $sHtml) |
| 46 | + GUISetState(@SW_SHOW) |
| 47 | + |
| 48 | + While 1 |
| 49 | + Switch GUIGetMsg() |
| 50 | + Case -3 ;$GUI_EVENT_CLOSE |
| 51 | + ExitLoop |
| 52 | + EndSwitch |
| 53 | + WEnd |
| 54 | + |
| 55 | + GUIDelete($hGUI) |
| 56 | + _NetWebView2_CleanUp($oWebV2M, $hGUI) |
| 57 | + Exit |
| 58 | +EndFunc ;==>_MainGUI |
| 59 | + |
| 60 | +#Region ; === EVENT HANDLERS === |
| 61 | +Volatile Func Web_OnNavigationStarting($oSender, $hParent, $oArgs) |
| 62 | + #forceref $oSender, $hParent |
| 63 | + |
| 64 | + Local $sURL = $oArgs.Uri |
| 65 | + ConsoleWrite("-> Navigation Starting to: " & (StringLen($sURL) > 100 ? StringLeft($sURL, 100) & "..." : $sURL) & @CRLF) |
| 66 | + |
| 67 | + ; If URL starts with "http", cancel internal navigation and ShellExecute |
| 68 | + If StringLeft($sURL, 4) = "http" Then |
| 69 | + ConsoleWrite("!!! Intercepting External Link: " & $sURL & @CRLF) |
| 70 | + $oArgs.Cancel = True ; Cancel navigation in WebView2 |
| 71 | + ShellExecute($sURL) ; Open in default browser |
| 72 | + EndIf |
| 73 | +EndFunc ;==>Web_OnNavigationStarting |
| 74 | + |
| 75 | +Volatile Func Web_OnNavigationCompleted($oSender, $hParent, $bSuccess, $iError) |
| 76 | + #forceref $oSender, $hParent, $bSuccess, $iError |
| 77 | + ConsoleWrite(">> Navigation Completed. Success: " & $bSuccess & @CRLF) |
| 78 | +EndFunc ;==>Web_OnNavigationCompleted |
| 79 | +#EndRegion ; === EVENT HANDLERS === |
0 commit comments