How to detect when a link is clicked or make links open in the default browser app?

Developers sometimes wish to detect when a link is clicked in a webview so that the application can open the link in the device's default browser app instead of loading the page in the webview. 3D WebView doesn't have an API specifically for intercepting link clicks, but an application can use IWebView.PageLoadScripts or ExecuteJavaScript() to add JavaScript to the page that detects when a link is clicked and sends a message to the application's C# so that it can open the URL in the default browser. Here's an example:

using UnityEngine;
using Vuplex.WebView;

class OpenLinksInDefaultBrowser : MonoBehaviour {

    // Set this field to your WebViewPrefab or CanvasWebViewPrefab via the Editor's inspector.
    public BaseWebViewPrefab webViewPrefab;

    async void Start() {

        // Wait for the WebViewPrefab.WebView property to be ready.
        // https://developer.vuplex.com/webview/WebViewPrefab#WaitUntilInitialized
        await webViewPrefab.WaitUntilInitialized();

        // Add JavaScript to PageLoadScripts.
        // https://developer.vuplex.com/webview/IWebView#PageLoadScripts
        webViewPrefab.WebView.PageLoadScripts.Add(@"
            // Use setInterval() to run this code every 250 ms.
            setInterval(() => {
                // Detect <a> tags that don't have our custom 'overridden' attribute
                // and update them to add the 'overridden' attribute and send the link
                // URL to the app's C# via window.vuplex.postMessage() when the link is clicked.
                const newLinks = document.querySelectorAll('a[href]:not([overridden])');
                for (const link of newLinks) {
                    link.setAttribute('overridden', true);
                    const linkUrl = link.href;
                    link.addEventListener('click', event => {
                        // Send the link URL to the C# script.
                        window.vuplex.postMessage('link:' + linkUrl);
                        // Call preventDefault() to prevent the webview from loading the URL.
                        event.preventDefault();
                    });
                }
            }, 250);
        ");

        // Listen for messages from JavaScript.
        // https://support.vuplex.com/articles/how-to-send-messages-from-javascript-to-c-sharp
        webViewPrefab.WebView.MessageEmitted += (sender, eventArgs) => {
            var message = eventArgs.Value;
            var prefix = "link:";
            if (message.StartsWith(prefix)) {
                var linkUrl = message.Substring(prefix.Length);
                // Open the link in the default browser app.
                // https://docs.unity3d.com/ScriptReference/Application.OpenURL.html
                Application.OpenURL(linkUrl);
            }
        };
    }
}