예제 #1
0
        private void InitializeBrowser()
        {
            EngineOptions engineOptions = new EngineOptions.Builder
            {
                ChromiumSwitches =
                {
                    "--enable-automation"
                },
                WebSecurityDisabled = true,
                RemoteDebuggingPort = RemoteDebuggingPort
            }
            .Build();

            engine  = EngineFactory.Create(engineOptions);
            browser = engine.CreateBrowser();

            byte[] htmlBytes = Encoding.UTF8.GetBytes("<h1>Waiting for Selenium...</h1>");
            browser.Navigation.LoadUrl("data:text/html;base64," + Convert.ToBase64String(htmlBytes));

            browserView = new BrowserView {
                Dock = DockStyle.Fill
            };
            browserView.InitializeFrom(browser);
            Controls.Add(browserView);
        }
예제 #2
0
        public Form1()
        {
            // Create the Windows Forms BrowserView control.
            BrowserView browserView = new BrowserView
            {
                Dock = DockStyle.Fill
            };

            // Create and initialize the IEngine instance.
            EngineOptions engineOptions = new EngineOptions.Builder
            {
                RenderingMode = RenderingMode.HardwareAccelerated,
                // Set the license key programmatically.
                LicenseKey = "your_license_key_goes_here"
            }.Build();

            engine = EngineFactory.Create(engineOptions);

            // Create the IBrowser instance.
            browser = engine.CreateBrowser();

            InitializeComponent();

            // Add the BrowserView control to the Form.
            Controls.Add(browserView);
            FormClosed += Form1_FormClosed;

            // Initialize the Windows Forms BrowserView control.
            browserView.InitializeFrom(browser);
            browser.Navigation.LoadUrl(Url);
        }
예제 #3
0
        private void InitializeBrowser()
        {
            EngineOptions engineOptions = new EngineOptions.Builder()
            {
                ChromiumSwitches =
                {
                    "--enable-automation"
                },
                WebSecurityDisabled = true,
                RemoteDebuggingPort = RemoteDebuggingPort
            }
            .Build();

            engine  = EngineFactory.Create(engineOptions);
            browser = engine.CreateBrowser();

            browser.MainFrame.LoadHtml("<h1>Waiting for Selenium...</h1>");

            browserView = new BrowserView()
            {
                Dock = DockStyle.Fill
            };
            browserView.InitializeFrom(browser);
            Controls.Add(browserView);
        }
예제 #4
0
        public Form1()
        {
            // Create and initialize the IEngine instance.
            EngineOptions engineOptions = new EngineOptions.Builder
            {
                RenderingMode = RenderingMode.OffScreen,
                // Set the license key programmatically.
                LicenseKey = "your_license_key_goes_here"
            }.Build();

            engine = EngineFactory.Create(engineOptions);

            // Create the IBrowser instance.
            browser = engine.CreateBrowser();
            // Create the WPF BrowserView control.
            BrowserView browserView = new BrowserView();

            InitializeComponent();
            FormClosed += Form1_FormClosed;

            // Create and initialize the ElementHost control.
            host = new ElementHost
            {
                Dock  = DockStyle.Fill,
                Child = browserView
            };
            Controls.Add(host);

            // Initialize the WPF BrowserView control.
            browserView.InitializeFrom(browser);
            browser.Navigation.LoadUrl(Url);
        }
예제 #5
0
        public static void Main()
        {
            // #docfragment "CustomRequestHandling"
            Handler <InterceptRequestParameters, InterceptRequestResponse> handler =
                new Handler <InterceptRequestParameters, InterceptRequestResponse>(p =>
            {
                UrlRequestJobOptions options = new UrlRequestJobOptions
                {
                    Headers = new List <HttpHeader>
                    {
                        new HttpHeader("Content-Type", "text/html", "charset=utf-8")
                    }
                };

                UrlRequestJob job = p.Network.CreateUrlRequestJob(p.UrlRequest, options);

                Task.Run(() =>
                {
                    // The request processing is performed in a worker thread
                    // in order to avoid freezing the web page.
                    job.Write(Encoding.UTF8.GetBytes("Hello world!"));
                    job.Complete();
                });

                return(InterceptRequestResponse.Intercept(job));
            });

            EngineOptions engineOptions = new EngineOptions.Builder
            {
                Schemes = { { Scheme.Create("myscheme"), handler } }
            }.Build();

            using (IEngine engine = EngineFactory.Create(engineOptions))
            {
                using (IBrowser browser = engine.CreateBrowser())
                {
                    LoadResult loadResult =
                        browser.Navigation.LoadUrl("myscheme://test1").Result;

                    // If the scheme handler was not set, the LoadResult would be
                    // LoadResult.Stopped.
                    // However, with the scheme handler, the web page is loaded and
                    // the result is LoadResult.Completed.
                    Console.WriteLine($"Load result: {loadResult}");
                    Console.WriteLine($"HTML: {browser.MainFrame.Html}");
                }
            }
            // #enddocfragment "CustomRequestHandling"

            Console.WriteLine("Press any key to terminate...");
            Console.ReadKey();
        }
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;

            EngineOptions engineOptions = new EngineOptions.Builder
            {
                RenderingMode = RenderingMode.HardwareAccelerated
            }
            .Build();

            engine = EngineFactory.Create(engineOptions);

            MyBrowser = new MyBrowserViewModel(engine.CreateBrowser())
            {
                Url = "www.teamdev.com/dotnetbrowser"
            };
        }
예제 #7
0
        public MainWindow()
        {
            chromiumDirectory = Path.GetFullPath("chromium");

            //Delete the Chromium directory it it exists - this will force downloading the binaries over network.
            if (Directory.Exists(chromiumDirectory))
            {
                Directory.Delete(chromiumDirectory, true);
            }

            Directory.CreateDirectory(chromiumDirectory);

            //Create and initialize the BinariesResolver
            BinariesResolver = new BinariesResolver();
            //Subscribe to the StatusUpdated event to update the UI accordingly.
            BinariesResolver.StatusUpdated += (sender, e) => InitializationStatus = e.Message;

            DataContext = this;

            Task.Run(() =>
            {
                IsInitializationInProgress  = true;
                EngineOptions engineOptions = new EngineOptions.Builder
                {
                    RenderingMode     = RenderingMode.HardwareAccelerated,
                    ChromiumDirectory = chromiumDirectory
                }
                .Build();
                InitializationStatus = "Creating DotNetBrowser engine";
                engine = EngineFactory.Create(engineOptions);
                InitializationStatus = "DotNetBrowser engine created";
                browser = engine.CreateBrowser();
            })
            .ContinueWith(t =>
            {
                BrowserView.InitializeFrom(browser);
                IsInitializationInProgress = false;
                browser.Navigation.LoadUrl("https://www.teamdev.com/");
            }, TaskScheduler.FromCurrentSynchronizationContext());

            InitializeComponent();
        }
예제 #8
0
 public Form1()
 {
     Task.Run(() =>
     {
         EngineOptions engineOptions = new EngineOptions.Builder
         {
             RenderingMode = RenderingMode.HardwareAccelerated
         }
         .Build();
         engine  = EngineFactory.Create(engineOptions);
         browser = engine.CreateBrowser();
         browser.Navigation.StartNavigationHandler =
             new Handler <StartNavigationParameters, StartNavigationResponse>(OnStartNavigation);
     })
     .ContinueWith(t =>
     {
         browserView1.InitializeFrom(browser);
         browser.Navigation.LoadUrl("https://www.teamdev.com/contact");
     }, TaskScheduler.FromCurrentSynchronizationContext());
     InitializeComponent();
 }
예제 #9
0
        public MainWindow()
        {
            // Create and initialize the IEngine instance.
            EngineOptions engineOptions = new EngineOptions.Builder
            {
                RenderingMode = RenderingMode.HardwareAccelerated,
                // Set the license key programmatically.
                LicenseKey = "your_license_key_goes_here"
            }.Build();

            engine = EngineFactory.Create(engineOptions);

            // Create the IBrowser instance.
            browser = engine.CreateBrowser();

            InitializeComponent();

            // Initialize the WPF BrowserView control.
            browserView.InitializeFrom(browser);
            browser.Navigation.LoadUrl(Url);
        }
예제 #10
0
        public static void Main()
        {
            string url =
                "https://dotnetbrowser-support.teamdev.com/docs/guides/gs/printing.html";
            string pdfFilePath = Path.GetFullPath("result.pdf");

            EngineOptions engineOptions = new EngineOptions.Builder
            {
                RenderingMode = RenderingMode.OffScreen,
                GpuDisabled   = true
            }.Build();

            using (IEngine engine = EngineFactory.Create(engineOptions))
            {
                using (IBrowser browser = engine.CreateBrowser())
                {
                    // #docfragment "PrintToPdf"
                    // Resize browser to the required dimension.
                    browser.Size = new Size(1024, 768);

                    // Load the required web page and wait until it is loaded completely.
                    Console.WriteLine($"Loading {url}");
                    browser.Navigation.LoadUrl(url).Wait();


                    // Configure print handlers.
                    browser.RequestPrintHandler =
                        new Handler <RequestPrintParameters, RequestPrintResponse
                                     >(p => RequestPrintResponse.Print());

                    TaskCompletionSource <string> printCompletedTcs =
                        new TaskCompletionSource <string>();
                    browser.PrintHtmlContentHandler
                        = new Handler <PrintHtmlContentParameters, PrintHtmlContentResponse
                                       >(p =>
                    {
                        try
                        {
                            // Get the print job for the built-in PDF printer.
                            PdfPrinter <PdfPrinter.IHtmlSettings> pdfPrinter =
                                p.Printers.Pdf;
                            IPrintJob <PdfPrinter.IHtmlSettings> printJob =
                                pdfPrinter.PrintJob;

                            // Apply the necessary print settings.
                            printJob.Settings.Apply(s =>
                            {
                                IReadOnlyCollection <PaperSize> paperSizes =
                                    pdfPrinter.Capabilities.PaperSizes;
                                s.PaperSize =
                                    paperSizes.First(size => size.Name.Contains("A4"));

                                s.PrintingHeaderFooterEnabled = true;
                                // Specify the path to save the result.
                                s.PdfFilePath = pdfFilePath;
                            });

                            string browserUrl        = p.Browser.Url;
                            printJob.PrintCompleted += (sender, args) =>
                            {
                                // Set the task result when the printing is completed.
                                printCompletedTcs.TrySetResult(browserUrl);
                            };

                            // Tell Chromium to use the built-in PDF printer
                            // for printing.
                            return(PrintHtmlContentResponse.Print(pdfPrinter));
                        }
                        catch (Exception e)
                        {
                            printCompletedTcs.TrySetException(e);
                            throw;
                        }
                    });

                    // Initiate printing and wait until it is completed.
                    Console.WriteLine("URL loaded. Initiate printing");
                    browser.MainFrame.Print();
                    string printedUrl = printCompletedTcs.Task.Result;
                    Console.WriteLine($"Printing completed for the URL: {printedUrl}");
                    // #enddocfragment "PrintToPdf"
                }
            }

            Console.WriteLine("Press any key to terminate...");
            Console.ReadKey();
        }