Пример #1
0
        public static void InitBrowser(ChromiumWebBrowser browser)
        {
            while (!browser.IsBrowserInitialized)
            {
                Application.DoEvents();
            }
            browser.FrameLoadEnd += (sender, args) =>
            {
                //Wait for the MainFrame to finish loading
                if (args.Frame.IsMain)
                {
                    foreach (var LinkingFunc in queueLinking)
                    {
                        LinkingFunc.Invoke();
                    }
                }

                ThreadPool.QueueUserWorkItem((c) =>
                {
                    var address     = System.Web.HttpUtility.UrlDecode(browser.Address.Replace("file:///", ""));
                    var addressDir  = Path.GetDirectoryName(address);
                    bool okToChange = false;
                    try
                    {
                        while (true)
                        {
                            var elementLength = browser.GetBrowser().MainFrame.EvaluateScriptAsync(
                                @"document.getElementsByClassName(""loadfile"").length");
                            elementLength.Wait();
                            var res = elementLength.Result;

                            if (int.Parse(res.Result.ToString()) > 0)
                            {
                                try
                                {
                                    okToChange      = true;
                                    var filenameObj = browser.GetBrowser().MainFrame.EvaluateScriptAsync(
                                        @"document.getElementsByClassName(""loadfile"")[0].getAttribute(""data-filename"")");
                                    filenameObj.Wait();
                                    string filename = filenameObj.Result.Result.ToString();

                                    string html = File.ReadAllText(Path.Combine(addressDir, filename), Encoding.UTF8);

                                    var FrameObj = browser.GetBrowser().MainFrame.EvaluateScriptAsync(
                                        @"document.getElementsByClassName(""loadfile"")[0].outerHTML =""" + StringEditing.StringToJs(html) + @"""");
                                    FrameObj.Wait();
                                }
                                catch (FileNotFoundException)
                                {
                                    var FrameObj = browser.GetBrowser().MainFrame.EvaluateScriptAsync(
                                        @"document.getElementsByClassName(""loadfile"")[0].setAttribute(""class"", """")");
                                    FrameObj.Wait();
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        MessageBox.Show(ex.StackTrace);
                    }
                    finally
                    {
                        if (okToChange)
                        {
                            string dic      = Path.GetDirectoryName(address);
                            string filep    = Path.GetFileNameWithoutExtension(address);
                            string filepExt = Path.GetExtension(address);
                            string dic_file = Path.Combine(dic, filep + filepExt);
                            if (!filep.Contains("temp"))
                            {
                                dic_file = Path.Combine(dic, filep + "_temp" + filepExt);
                            }

                            var SourceTask = browser.GetBrowser().MainFrame.EvaluateScriptAsync(@"document.getElementsByTagName ('html')[0].innerHTML");
                            SourceTask.Wait();
                            string Source = SourceTask.Result.Result.ToString();

                            File.WriteAllText(dic_file, Source, Encoding.UTF8);
                            browser.Load(dic_file);
                        }
                    }
                });
            };
        }
Пример #2
0
        public static void ChangeText(
            ChromiumWebBrowser browser,
            string elementID, string Text)
        {
            ChromiumWebBrowser _browser = browser;

            _browser.GetBrowser().MainFrame.ExecuteJavaScriptAsync(@"document.getElementById(""" + elementID + @""").innerHTML = """ + StringEditing.StringToJs(Text) + "\"");
        }