コード例 #1
0
ファイル: MauiWebView.cs プロジェクト: Glepooek/maui
        public async void LoadUrl(string?url)
        {
            Uri uri = new Uri(url ?? string.Empty, UriKind.RelativeOrAbsolute);

            if (!uri.IsAbsoluteUri)
            {
                await EnsureCoreWebView2Async();

                CoreWebView2.SetVirtualHostNameToFolderMapping(
                    LocalHostName,
                    ApplicationPath,
                    Web.WebView2.Core.CoreWebView2HostResourceAccessKind.Allow);

                uri = new Uri(LocalScheme + url, UriKind.RelativeOrAbsolute);
            }

            // TODO: Sync Cookies

            try
            {
                Source = uri;
            }
            catch (Exception exc)
            {
                Debug.WriteLine(nameof(MauiWebView), $"Failed to load: {uri} {exc}");
            }
        }
コード例 #2
0
ファイル: MauiWebView.cs プロジェクト: Glepooek/maui
        public async void LoadHtml(string?html, string?baseUrl)
        {
            var mapBaseDirectory = false;

            if (string.IsNullOrEmpty(baseUrl))
            {
                baseUrl          = LocalScheme;
                mapBaseDirectory = true;
            }

            // Generate a base tag for the document
            var baseTag = $"<base href=\"{baseUrl}\"></base>";

            string htmlWithBaseTag;

            // Set up an internal WebView we can use to load and parse the original HTML string
            // Make _internalWebView a field instead of local variable to avoid garbage collection
            _internalWebView = new WebView2();

            // TODO: For now, the CoreWebView2 won't be created without either setting Source or
            // calling EnsureCoreWebView2Async().
            await _internalWebView.EnsureCoreWebView2Async();

            // When the 'navigation' to the original HTML string is done, we can modify it to include our <base> tag
            _internalWebView.NavigationCompleted += async(sender, args) =>
            {
                // Generate a version of the <base> script with the correct <base> tag
                var script = BaseInsertionScript.Replace("baseTag", baseTag, StringComparison.Ordinal);

                // Run it and retrieve the updated HTML from our WebView
                await sender.ExecuteScriptAsync(script);

                htmlWithBaseTag = await sender.ExecuteScriptAsync("document.documentElement.outerHTML;");

                htmlWithBaseTag = Regex.Unescape(htmlWithBaseTag);
                htmlWithBaseTag = htmlWithBaseTag.Remove(0, 1);
                htmlWithBaseTag = htmlWithBaseTag.Remove(htmlWithBaseTag.Length - 1, 1);

                await EnsureCoreWebView2Async();

                if (mapBaseDirectory)
                {
                    CoreWebView2.SetVirtualHostNameToFolderMapping(
                        LocalHostName,
                        ApplicationPath,
                        Web.WebView2.Core.CoreWebView2HostResourceAccessKind.Allow);
                }

                // Set the HTML for the 'real' WebView to the updated HTML
                NavigateToString(!string.IsNullOrEmpty(htmlWithBaseTag) ? htmlWithBaseTag : html);

                // Free up memory after we're done with _internalWebView
                _internalWebView = null;
            };

            // Kick off the initial navigation
            _internalWebView.NavigateToString(html);
        }
コード例 #3
0
ファイル: MauiWebView.cs プロジェクト: Glepooek/maui
 public MauiWebView()
 {
     NavigationStarting += (sender, args) =>
     {
         // Auto map local virtual app dir host, e.g. if navigating back to local site from a link to an external site
         if (args?.Uri?.ToLowerInvariant().StartsWith(LocalScheme.TrimEnd('/').ToLowerInvariant()) == true)
         {
             CoreWebView2.SetVirtualHostNameToFolderMapping(
                 LocalHostName,
                 ApplicationPath,
                 Web.WebView2.Core.CoreWebView2HostResourceAccessKind.Allow);
         }
         // Auto unmap local virtual app dir host if navigating to any other potentially unsafe domain
         else
         {
             CoreWebView2.ClearVirtualHostNameToFolderMapping(LocalHostName);
         }
     };
 }