chromelyapps / Chromely

Build Cross Platform HTML Desktop Apps on .NET using native GUI, HTML5, JavaScript, CSS, Owin, AspNetCore (MVC, RazorPages, Blazor)
MIT License
2.99k stars 279 forks source link

Howto: How to enable "View page source" menu item #351

Closed mattkol closed 2 years ago

mattkol commented 2 years ago

To enable "View page source", create a custom context menu using DefaultContextMenuHandler.

Make changes to the menu items - https://github.com/chromelyapps/Chromely/blob/1f95b7d1475cd56bd9a50b1d8df7140e42810e22/src/Chromely/Browser/Handlers/DefaultContextMenuHandler.cs#L38

CrossPlatDemo.zip

    internal class CustomContextMenuHandler : DefaultContextMenuHandler
    {
        public CustomContextMenuHandler(IChromelyConfiguration config) : base(config)
        {
        }

        protected override void OnBeforeContextMenu(CefBrowser browser, CefFrame frame, CefContextMenuParams state, CefMenuModel model)
        {
            // To disable the menu then call clear
            model.Clear();

            if (_config.Platform == ChromelyPlatform.Windows)
            {
                // Removing existing menu item
                // Remove "View Source" option

                // Allow to view source.
                // model.Remove((int)CefMenuId.ViewSource);

                if (debugging)
                {
                    // Add new custom menu items
                    model.AddItem((int)((CefMenuId)ShowDevTools), "Show DevTools");
                    model.AddItem((int)((CefMenuId)CloseDevTools), "Close DevTools");

                    // Allow to view source.
                    model.AddItem((int)(CefMenuId.ViewSource), "View page source");
                }
            }
        }
    }

Register the custom context menu handler:

    public class DemoApp : ChromelyBasicApp
    {
        public override void ConfigureServices(IServiceCollection services)
        {
            base.ConfigureServices(services);

            ----

            // Add custom context menu
            services.AddSingleton<CefContextMenuHandler, CustomContextMenuHandler>();
        }
    }

image