Ejemplo n.º 1
0
        public async Task <BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken token = default)
        //   public async Task<BrowserResult> InvokeAsync(BrowserOptions options)
        {
            using (var form = _formFactory.Invoke())
                using (var browser = new ExtendedWebBrowser()
                {
                    Dock = DockStyle.Fill
                })
                {
                    //       browser.IsWebBrowserContextMenuEnabled = false;
                    //  browser.WebBrowserShortcutsEnabled = false;
                    //  browser.ObjectForScripting = this;
                    //browser.ScriptErrorsSuppressed = true;
                    var signal = new SemaphoreSlim(0, 1);
                    var result = new BrowserResult
                    {
                        ResultType = BrowserResultType.UserCancel
                    };

                    browser.DocumentTitleChanged += (o, e) =>
                    {
                        String title = browser.DocumentTitle;
                        if (!String.IsNullOrWhiteSpace(options.SuccessTitle) && title.StartsWith(options.SuccessTitle))
                        {
                            result.ResultType = BrowserResultType.Success;
                            result.Response   = title;
                            signal.Release();
                        }
                    };

                    form.FormClosed += (o, e) =>
                    {
                        signal.Release();
                    };

                    browser.NavigateError += (o, e) =>
                    {
                        e.Cancel = true;

                        if (!String.IsNullOrWhiteSpace(options.EndUrl) && e.Url.StartsWith(options.EndUrl))
                        {
                            result.ResultType = BrowserResultType.Success;
                            result.Response   = e.Url;
                        }
                        else
                        {
                            result.ResultType = BrowserResultType.HttpError;
                            result.Error      = e.StatusCode.ToString();
                        }

                        signal.Release();
                    };

                    browser.BeforeNavigate2 += (o, e) =>
                    {
                        if (!String.IsNullOrWhiteSpace(options.EndUrl) && e.Url.StartsWith(options.EndUrl))
                        {
                            e.Cancel = true;

                            result.ResultType = BrowserResultType.Success;
                            result.Response   = e.Url;
                            signal.Release();
                        }
                    };

                    form.Controls.Add(browser);
                    browser.Show();

                    System.Threading.Timer timer = null;

                    form.Show();
                    browser.Navigate(options.StartUrl);

                    await signal.WaitAsync();

                    if (timer != null)
                    {
                        timer.Change(Timeout.Infinite, Timeout.Infinite);
                    }

                    form.Hide();
                    browser.Hide();

                    return(result);
                }
        }
Ejemplo n.º 2
0
 public ExtendedWebBrowserEventHelper(ExtendedWebBrowser parent)
 {
     this.parent = parent;
 }