//private BrowserDialog window;

        /// <summary>
        ///
        /// </summary>
        /// <param name="searchTerm"></param>
        /// <param name="searchEngineType"></param>
        /// <returns></returns>
        public async Task <List <SearchItem> > GetSearchLinks(string searchTerm,
                                                              SearchEngineTypes searchEngineType = SearchEngineTypes.DuckDuckGo)
        {
            var list = new List <SearchItem>();
            var url  = "https://{0}.com?q={1}";

            url = string.Format(url, searchEngineType, searchTerm);

            var window = new BrowserDialog();

            window.Width      = 0;
            window.Height     = 0;
            window.Top        = -50000; // offsreen but visible
            window.Visibility = Visibility.Visible;

            if (!window.NavigateAndWaitForCompletion(url))
            {
                return(null);
            }



            await window.Dispatcher.DelayAsync(1000, (p) =>
            {
                dynamic document = window.Browser.Document;
                var html         = document.Body.InnerHtml as string;
                window?.Close();

                if (string.IsNullOrEmpty((html)))
                {
                    return;
                }

                var doc = new HtmlDocument();
                doc.LoadHtml(html);
                var links = doc.DocumentNode.SelectNodes("//*/h2/a[1]");

                if (links == null)
                {
                    return;
                }

                foreach (var link in links)
                {
                    var searchItem = new SearchItem();
                    url            = link.Attributes["href"]?.Value;
                    if (url.Contains("?uddg="))
                    {
                        url = StringUtils.ExtractString(url, "?uddg=", "x",
                                                        allowMissingEndDelimiter: true);
                    }
                    searchItem.Url   = StringUtils.UrlDecode(url);
                    searchItem.Title = link.InnerText;
                    list.Add(searchItem);
                }
            }, DispatcherPriority.ApplicationIdle);


            return(list);
        }
 public void CloseBrowser()
 {
     Application.Current.Dispatcher.BeginInvoke(new Action(() =>
     {
         browserDialog.Close();
     }));
 }
示例#3
0
        /// <summary>
        /// Converts an HTML string to Markdown.
        /// </summary>
        /// <remarks>
        /// This routine relies on a browser window
        /// and an HTML file that handles the actual
        /// parsing: Editor\HtmlToMarkdown.htm
        /// </remarks>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string HtmlToMarkdown(string html, bool noErrorUI = false)
        {
            if (string.IsNullOrEmpty(html))
            {
                return("");
            }
#if false
            var config = new ReverseMarkdown.Config {
                GithubFlavored    = true,
                UnknownTags       = ReverseMarkdown.Config.UnknownTagsOption.PassThrough, // Include the unknown tag completely in the result (default as well)
                SmartHrefHandling = true                                                  // remove markdown output for links where appropriate
            };
            var    converter = new ReverseMarkdown.Converter(config);
            string markdown  = converter.Convert(html);

//            if (!string.IsNullOrEmpty(markdown))
//                markdown = markdown.Replace("\n\n", "\r\n").Replace("\r\n\r\n", "\r\n");
            return(markdown ?? html);
#else
            // Old code that uses JavaScript in a WebBrowser Control
            string markdown = null;
            string htmlFile = Path.Combine(App.InitialStartDirectory, "Editor\\htmltomarkdown.htm");

            var form = new BrowserDialog();
            form.ShowInTaskbar = false;
            form.Width         = 1;
            form.Height        = 1;
            form.Left          = -10000;
            form.Show();

            bool exists = File.Exists(htmlFile);
            form.NavigateAndWaitForCompletion(htmlFile);

            WindowUtilities.DoEvents();

            try
            {
                dynamic doc = form.Browser.Document;
                markdown = doc.ParentWindow.htmltomarkdown(html);
            }
            catch (Exception ex)
            {
                mmApp.Log("Failed to convert Html to Markdown", ex);
                if (!noErrorUI)
                {
                    MessageBox.Show("Unable to convert Html to Markdown. Returning Html.", "Html to Markdown Conversion Failed.", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }

            form.Close();
            form = null;

            return(markdown ?? html);
#endif
        }
示例#4
0
        /// <summary>
        /// Converts an HTML string to Markdown.
        /// </summary>
        /// <remarks>
        /// This routine relies on a browser window
        /// and an HTML file that handles the actual
        /// parsing: Editor\HtmlToMarkdown.htm
        /// </remarks>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string HtmlToMarkdown(string html)
        {
            if (string.IsNullOrEmpty(html))
            {
                return("");
            }
#if false
            var    config    = new ReverseMarkdown.Config(githubFlavored: true);
            var    converter = new ReverseMarkdown.Converter(config);
            string markdown  = converter.Convert(html);
            return(markdown ?? html);
#else
            // Old code that uses JavaScript in a WebBrowser Control
            string markdown = null;
            string htmlFile = Path.Combine(Environment.CurrentDirectory, "Editor\\htmltomarkdown.htm");

            var form = new BrowserDialog();
            form.ShowInTaskbar = false;
            form.Width         = 1;
            form.Height        = 1;
            form.Left          = -10000;
            form.Show();

            bool exists = File.Exists(htmlFile);
            form.Navigate(htmlFile);

            WindowUtilities.DoEvents();

            for (int i = 0; i < 200; i++)
            {
                dynamic doc = form.Browser.Document;

                if (!form.IsLoaded)
                {
                    Task.Delay(10);
                    WindowUtilities.DoEvents();
                }
                else
                {
                    markdown = doc.ParentWindow.htmltomarkdown(html);
                    break;
                }
            }

            form.Close();
            form = null;

            return(markdown ?? html);
#endif
        }
示例#5
0
        /// <summary>
        /// Converts an HTML string to Markdown.
        /// </summary>
        /// <remarks>
        /// This routine relies on a browser window
        /// and an HTML file that handles the actual
        /// parsing: Editor\HtmlToMarkdown.htm
        /// </remarks>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string HtmlToMarkdown(string html, bool noErrorUI = false)
        {
            if (string.IsNullOrEmpty(html))
            {
                return("");
            }
#if false
            var    config    = new ReverseMarkdown.Config(githubFlavored: true);
            var    converter = new ReverseMarkdown.Converter(config);
            string markdown  = converter.Convert(html);
            return(markdown ?? html);
#else
            // Old code that uses JavaScript in a WebBrowser Control
            string markdown = null;
            string htmlFile = Path.Combine(Environment.CurrentDirectory, "Editor\\htmltomarkdown.htm");

            var form = new BrowserDialog();
            form.ShowInTaskbar = false;
            form.Width         = 1;
            form.Height        = 1;
            form.Left          = -10000;
            form.Show();

            bool exists = File.Exists(htmlFile);
            form.NavigateAndWaitForCompletion(htmlFile);

            WindowUtilities.DoEvents();

            try
            {
                dynamic doc = form.Browser.Document;
                markdown = doc.ParentWindow.htmltomarkdown(html);
            }
            catch (Exception ex)
            {
                mmApp.Log("Failed to convert Html to Markdown", ex);
                if (!noErrorUI)
                {
                    MessageBox.Show("Unable to convert Html to Markdown. Returning Html.", "Html to Markdown Conversion Failed.", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }

            form.Close();
            form = null;

            return(markdown ?? html);
#endif
        }
示例#6
0
        /// <summary>
        /// Converts an HTML string to Markdown.
        /// </summary>
        /// <remarks>
        /// This routine relies on a browser window
        /// and an HTML file that handles the actual
        /// parsing: Editor\HtmlToMarkdown.htm
        /// </remarks>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string HtmlToMarkdown(string html)
        {
            string markdown = null;
            string htmlFile = Path.Combine(Environment.CurrentDirectory, "Editor\\htmltomarkdown.htm");

            var form = new BrowserDialog();

            form.ShowInTaskbar = false;
            form.Width         = 1;
            form.Height        = 1;
            form.Left          = -10000;
            form.Show();

            bool exists = File.Exists(htmlFile);

            form.Navigate(htmlFile);

            WindowUtilities.DoEvents();

            for (int i = 0; i < 200; i++)
            {
                dynamic doc = form.Browser.Document;

                if (!form.IsLoaded)
                {
                    Task.Delay(10);
                    WindowUtilities.DoEvents();
                }
                else
                {
                    markdown = doc.ParentWindow.htmltomarkdown(html);
                    break;
                }
            }

            form.Close();
            form = null;

            return(markdown ?? html);
        }