public EmbeddedBrowserTabUserControl(Action <string, int?> openNewTab, string url, WebEntryInfo webEntryInfo, bool multiThreadedMessageLoopEnabled)
        {
            InitializeComponent();
            WebEntryInfo    = webEntryInfo;
            this.openNewTab = openNewTab;
            this.multiThreadedMessageLoopEnabled = multiThreadedMessageLoopEnabled;

            EmbeddedBrowserHelper.InitIfNeed();//we need this to correctly init first browser instance

            //https://stackoverflow.com/questions/34549565/separate-cache-per-browser
            ChromiumWebBrowser browser = null;

            if (WebEntryInfo.BrowserPrivateType == BrowserPrivateType.COMMON_CACHE ||
                WebEntryInfo.BrowserPrivateType == BrowserPrivateType.PERSONAL_OLD_DISK_CACHE ||
                WebEntryInfo.BrowserPrivateType == BrowserPrivateType.PERSONAL_NEW_DISK_CACHE)
            {
                if (WebEntryInfo.CachePath != null)
                {
                    if (WebEntryInfo.BrowserPrivateType == BrowserPrivateType.PERSONAL_NEW_DISK_CACHE)
                    {
                        FileUtils.CreateOrClearDirectory(WebEntryInfo.CachePath);
                    }
                    else if (WebEntryInfo.BrowserPrivateType == BrowserPrivateType.PERSONAL_OLD_DISK_CACHE ||
                             WebEntryInfo.BrowserPrivateType == BrowserPrivateType.COMMON_CACHE)
                    {
                        FileUtils.CreateDirectory(WebEntryInfo.CachePath);
                    }
                }
                else
                {
                    throw new NotSupportedException();
                }
                BrowserSettings        browserSettings        = new BrowserSettings();
                RequestContextSettings requestContextSettings = new RequestContextSettings {
                    CachePath = WebEntryInfo.CachePath
                };
                var requestContext = new RequestContext(requestContextSettings);
                browser = new ChromiumWebBrowser(url)
                {
                    BrowserSettings = browserSettings,
                    RequestContext  = requestContext,
                    Dock            = DockStyle.Fill
                };
            }
            else if (WebEntryInfo.BrowserPrivateType == BrowserPrivateType.PERSONAL_IN_MEMORY_CACHE)
            {
                var browserSettings        = new BrowserSettings();
                var requestContextSettings = new RequestContextSettings {
                    CachePath = null
                };
                var requestContext = new RequestContext(requestContextSettings);
                browser = new ChromiumWebBrowser(url)
                {
                    BrowserSettings = browserSettings,
                    RequestContext  = requestContext,
                    Dock            = DockStyle.Fill
                };
            }

            browserPanel.Controls.Add(browser);
            Browser = browser;

            MenuHandler handler = new MenuHandler();

            //handler.DownloadImage += Handler_DownloadImage;
            //handler.ViewPageSource += Handler_ViewPageSource;
            // handler.DownloadYouTubeVideo += Handler_DownloadYouTubeVideo;
            //handler.ViewImageExif += Handler_ViewImageExif;
            //handler.ViewFacebookIdNum += Handler_ViewFacebookIdNum;
            // handler.ViewTwitterIdNum += Handler_ViewTwitterIdNum;
            handler.CopyImageLocation       += Handler_CopyImageLocation;;
            handler.OpenInNewTabContextMenu += Handler_OpenInNewTabContextMenu;
            handler.ReverseImgSearch        += Handler_ReverseImgSearch;
            //handler.ExtractLinks += Handler_ExtractLinks;
            //handler.AddPageToBookmarks += Handler_AddPageToBookmarks;
            handler.SearchText       += Handler_SearchText;
            handler.SaveSelectedText += Handler_SaveSelectedText;

            Browser.MenuHandler = handler;


            //todo
            browser.RequestHandler              = new WinFormsRequestHandler(openNewTab, webEntryInfo);
            browser.LoadingStateChanged        += OnBrowserLoadingStateChanged;//todo some errors when press 'Go'
            browser.ConsoleMessage             += OnBrowserConsoleMessage;
            browser.TitleChanged               += OnBrowserTitleChanged;
            browser.AddressChanged             += OnBrowserAddressChanged;
            browser.StatusMessage              += OnBrowserStatusMessage;//to status label
            browser.LoadError                  += OnLoadError;
            browser.RenderProcessMessageHandler = new RenderProcessMessageHandler();
            browser.DisplayHandler              = new DisplayHandler();

            //var version = String.Format(EmbeddedBrowserControlRes.Chromium___0___CEF___1___CefSharp___2_, Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion);
            var version = String.Format(EmbeddedBrowserControlRes.Chromium___0, Cef.ChromiumVersion);

            DisplayOutput(version);
        }
Ejemplo n.º 2
0
        void IContextMenuHandler.OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
        {
            //To disable the menu then call clear
            // model.Clear();

            //Removing existing menu item
            //bool removed = model.Remove(CefMenuCommand.ViewSource); // Remove "View Source" option

            //Add new custom menu items
            model.AddItem((CefMenuCommand)ShowDevTools, EmbeddedBrowserControlRes.Show_DevTools);
            model.AddItem((CefMenuCommand)CloseDevTools, EmbeddedBrowserControlRes.Close_DevTools);

            //BufferLink = null;
            if (string.IsNullOrEmpty(parameters.UnfilteredLinkUrl) == false)
            {
                //BufferLink = parameters.LinkUrl;
                model.AddItem((CefMenuCommand)OpenLinkInNewTab, EmbeddedBrowserControlRes.Open_Link_In_New_Tab);
            }


            if (parameters.TypeFlags.HasFlag(ContextMenuType.Selection))
            {
                model.AddItem((CefMenuCommand)SearchSelectedText, EmbeddedBrowserControlRes.Search_selected_text_using_Google);
                model.AddItem((CefMenuCommand)SaveText, EmbeddedBrowserControlRes.Save_selected_text);
                model.AddSeparator();
            }


            if (parameters.TypeFlags.HasFlag(ContextMenuType.Media) && parameters.HasImageContents)
            {
                if (EmbeddedBrowserHelper.HasJpegExtension(parameters.SourceUrl))
                {
                    //model.AddItem((CefMenuCommand)ViewImageExifData, "View image EXIF data");
                }
                //model.AddItem((CefMenuCommand)MenuSaveImage, "Save image");
                model.AddItem((CefMenuCommand)CopyImgLocation, EmbeddedBrowserControlRes.Copy_image_location_to_clipboard);

                var sub = model.AddSubMenu((CefMenuCommand)ReverseImgSearchSubMenu, EmbeddedBrowserControlRes.Reverse_image_search_tools);
                sub.AddItem((CefMenuCommand)ReverseImageSearchTineye, EmbeddedBrowserControlRes.Reverse_image_search_using_TinEye);
                //sub.AddItem((CefMenuCommand)ReverseImageSearchYandex, EmbeddedBrowserControlRes.Reverse_image_search_using_Yandex);
                sub.AddItem((CefMenuCommand)ReverseImageSearchGoogle, EmbeddedBrowserControlRes.Reverse_image_search_using_Google);
                model.AddSeparator();
                //
            }
            if (EmbeddedBrowserHelper.IsOnYouTube(browserControl.Address))
            {
                //model.AddItem((CefMenuCommand)SaveYouTubeVideo, "Extract YouTube video");
            }
            if (EmbeddedBrowserHelper.IsOnFacebook(browserControl.Address))
            {
                //model.AddItem((CefMenuCommand)ViewFacebookId, "Show Facebook profile ID");
            }
            if (EmbeddedBrowserHelper.IsOnTwitter(browserControl.Address))
            {
                //model.AddItem((CefMenuCommand)ViewTwitterId, "Show Twitter profile ID");
            }

            //model.AddItem((CefMenuCommand)ViewSource, "*View page source");
            //model.AddItem((CefMenuCommand)ExtractAllLinks, "Extract all links on page");
            //model.AddItem((CefMenuCommand)Bookmark, "Add page to bookmarks");
            string s  = EmbeddedBrowserControlRes.View_source;
            string s2 = EmbeddedBrowserControlRes.Forward;

            model.SetLabel(CefMenuCommand.ViewSource, EmbeddedBrowserControlRes.View_source);
            model.SetLabel(CefMenuCommand.Print, EmbeddedBrowserControlRes.Print);
            model.SetLabel(CefMenuCommand.Undo, EmbeddedBrowserControlRes.Undo);
            model.SetLabel(CefMenuCommand.Redo, EmbeddedBrowserControlRes.Redo);
            model.SetLabel(CefMenuCommand.Forward, EmbeddedBrowserControlRes.Forward);
            model.SetLabel(CefMenuCommand.Back, EmbeddedBrowserControlRes.Back);

            /*
             * model.Remove(CefMenuCommand.ViewSource);
             * model.Remove(CefMenuCommand.Print);
             * model.Remove(CefMenuCommand.Undo);
             * model.Remove(CefMenuCommand.Redo);
             * model.Remove(CefMenuCommand.Forward);
             * model.Remove(CefMenuCommand.Back);
             */
        }