public void ShowMessageDialog(string msg, string title = "", bool cancel = false)
 {
     if (title == "error")
     {
         this.Text = ResourceCulture.GetString("MSGBOX_TITLE_ERROR");
     }
     else if (title == "warning")
     {
         this.Text = ResourceCulture.GetString("MSGBOX_TITLE_WARNING");
     }
     else
     {
         this.Text = ResourceCulture.GetString("MSGBOX_TITLE_CONFIRM");
     }
     if (cancel)
     {
         this.btnCancel.Visible = true;
         this.btnCancel.Select();
     }
     else
     {
         this.btnCancel.Visible = false;
     }
     this.lblMsg.Text = msg;
     this.ShowDialog(parent);
 }
 private void MenuItemClearBrowsingRecord_Click(object sender, EventArgs e)
 {
     try {
         string txt = ResourceCulture.GetString("MSGBOX_TXT_CONFIRM_DELETE_BROWSING_RECORD");
         this.msgbox.ShowMessageDialog(txt, "confirm", true);
         if (this.msgbox.Confirm())
         {
             if (IsNodeIsExist("./config.xml", "favorites") && IsNodeIsExist("./config.xml", "url"))
             {
                 XmlNode favorites = this.root.SelectSingleNode("favorites");
                 favorites.RemoveAll();
                 this.xmldoc.Save("./config.xml");
                 this.cmbUrl.Items.Clear();
             }
         }
     }
     catch (System.Exception ex) {
         string txt = ResourceCulture.GetString("MSGBOX_TXT_CLEAR_BROWSING_DATA_FAIL");
         this.msgbox.ShowMessageDialog(txt, "error");
         if (thr != null)
         {
             thr.Append(ex.StackTrace);
         }
     }
 }
 public MessageForm(MainForm owner)
 {
     InitializeComponent();
     this.parent         = owner;
     this.btnCancel.Text = ResourceCulture.GetString("BUTTON_TEXT_CANCEL");
     this.btnOK.Text     = ResourceCulture.GetString("BUTTON_TEXT_OK");
 }
예제 #4
0
 public AboutForm(MainForm owner)
 {
     InitializeComponent();
     this.parent           = owner;
     this.Text             = ResourceCulture.GetString("ABOUT_DIALOG_TITLE");
     this.txbAboutApp.Text = ResourceCulture.GetString("ABOUT_DIALOG_TXT");
 }
        public MainForm()
        {
            InitializeComponent();
            try {
                this.xmldoc = new XmlDocument();
                ParseConfigXml();

                if (this.language.Equals("en"))
                {
                    ResourceCulture.SetCurrentCulture("en-US");
                }
                else
                {
                    ResourceCulture.SetCurrentCulture("zh-CN");
                }

                this.Text = ResourceCulture.GetString("MAINFORM_TITLE");
                this.MenuItemTool.Text                = ResourceCulture.GetString("MENU_TOOL");
                this.MenuItemImportFromCsv.Text       = ResourceCulture.GetString("MENU_TOOL_IMPORT_FROM_CSV");
                this.MenuItemExportToCsv.Text         = ResourceCulture.GetString("MENU_TOOL_EXPORT_TO_CSV");
                this.MenuItemClearBrowsingRecord.Text = ResourceCulture.GetString("MENU_TOOL_CLEAR_BROWSING_RECORD");
                this.MenuItemAbout.Text               = ResourceCulture.GetString("MENU_TOOL_ABOUT");
                this.btnPrevious.Text   = ResourceCulture.GetString("BUTTON_TEXT_BACK");
                this.btnNext.Text       = ResourceCulture.GetString("BUTTON_TEXT_FRONT");
                this.btnClearTable.Text = ResourceCulture.GetString("BUTTON_TEXT_CLEAR");
                this.msgbox             = new MessageForm(this);
                this.aboutform          = new AboutForm(this);

                if (this.log != 0)
                {
                    thr = new WriteLogThread();
                    WriteLogThread.EmitMessage += new EmitCustomizedMessage(ReceiveMessage);
                    thr.Start();
                }
            }
            catch (Exception ex) {
                if (thr != null)
                {
                    thr.Append(ex.StackTrace);
                }
                string txt = ResourceCulture.GetString("MSGBOX_TXT_FAILS_TO_START_APP");
                msgbox.ShowMessageDialog(txt, "warning");
            }
        }
        private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            try {
                //将所有的链接的目标,指向本窗体
                foreach (HtmlElement archor in this.webBrowser.Document.Links)
                {
                    archor.SetAttribute("target", "_self");
                }

                //将所有的FORM的提交目标,指向本窗体
                foreach (HtmlElement form in this.webBrowser.Document.Forms)
                {
                    form.SetAttribute("target", "_self");
                }
            }
            catch (System.Exception ex) {
                string txt = ResourceCulture.GetString("MSGBOX_TXT_FAILS_TO_LOAD_WEB_PAGE");
                this.msgbox.ShowMessageDialog(txt, "error");
                if (thr != null)
                {
                    thr.Append(ex.StackTrace);
                }
            }
        }
        private void MenuItemExportToCsv_Click(object sender, EventArgs e)
        {
            try {
                HtmlElement table = null;
                try {
                    table = this.webBrowser.Document.GetElementById(this.tablename);
                    if (table == null)
                    {
                        throw new Exception();
                    }
                }
                catch (System.Exception ex) {
                    string fmt = ResourceCulture.GetString("MSGBOX_TXT_TABLE_NOT_FOUND");
                    string txt = string.Format(fmt, this.tablename);
                    this.msgbox.ShowMessageDialog(txt, "error");
                    if (thr != null)
                    {
                        thr.Append(ex.StackTrace);
                    }
                    return;
                }

                SaveFileDialog dlg = new SaveFileDialog();
                dlg.InitialDirectory = this.initdir;
                dlg.Filter           = "csv file|*.csv";
                dlg.RestoreDirectory = false;
                dlg.FilterIndex      = 1;
                dlg.Title            = ResourceCulture.GetString("MENU_TOOL_EXPORT_TO_CSV");
                if (dlg.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                string filename = initdir = dlg.FileName;
                if (!Regex.IsMatch(dlg.FileName.ToLower(), @".csv$"))
                {
                    filename = initdir += ".csv";
                }

                HtmlElement script = this.webBrowser.Document.GetElementById("_#@#_embedded_stript_id_check_if_element_visible");
                if (script == null)
                {
                    script = this.webBrowser.Document.CreateElement("script");
                    script.SetAttribute("type", "text/javascript");
                    script.SetAttribute("id", "_#@#_embedded_stript_id_check_if_element_visible");
                    script.SetAttribute("text", this.embededScript);
                    this.webBrowser.Document.Body.AppendChild(script);
                }

                HtmlElementCollection trs = table.GetElementsByTagName("tr");

                CsvStreamReader csv = new CsvStreamReader();
                csv.OpenCsvFile(filename);

                object[] args     = new object[1];
                string[] tagnames = { "a", "b", "p", "font", "h1", "h2", "h3", "h4", "h5", "h6", "input", "select", "span", "textarea" };

                foreach (HtmlElement tr in trs)
                {
                    if (!tr.Parent.Parent.Equals(table))
                    {
                        continue;
                    }
                    HtmlElementCollection tds = tr.GetElementsByTagName("td");
                    LinkedList <string>   lls = new LinkedList <string>();
                    foreach (HtmlElement td in tds)
                    {
                        if (!td.Parent.Equals(tr))
                        {
                            continue;
                        }

                        string id    = td.GetAttribute("id");
                        string newId = "#@#_temp_id_123456";
                        if (id != null)
                        {
                            newId = id + "_#@#_temp_id_123456";
                        }
                        td.SetAttribute("id", newId);
                        args[0] = newId;
                        object obj = this.webBrowser.Document.InvokeScript("__getInnerText", args);
                        td.SetAttribute("id", id);

                        string   innerText = (obj == null ? "" : obj.ToString());
                        string[] texts     = innerText.Split('\t');
                        for (int j = 0; j < texts.Length; j++)
                        {
                            lls.AddLast(texts[j]);
                        }
                    }
                    csv.WriteCsvFile(lls);
                }
                csv.CloseCsvFile();
            }
            catch (System.Exception ex) {
                string txt = ResourceCulture.GetString("MSGBOX_TXT_EXPORT_FAIL");
                this.msgbox.ShowMessageDialog(txt, "error");
                if (thr != null)
                {
                    thr.Append(ex.StackTrace);
                }
            }
        }
        private void MenuItemImportFromCsv_Click(object sender, EventArgs e)
        {
            try {
                HtmlElement table = null;
                try {
                    table = this.webBrowser.Document.GetElementById(this.tablename);
                    if (table == null)
                    {
                        throw new Exception();
                    }
                }
                catch (System.Exception ex) {
                    string fmt = ResourceCulture.GetString("MSGBOX_TXT_TABLE_NOT_FOUND");
                    string txt = string.Format(fmt, this.tablename);
                    this.msgbox.ShowMessageDialog(txt, "error");
                    if (thr != null)
                    {
                        thr.Append(ex.StackTrace);
                    }
                    return;
                }

                OpenFileDialog ofdlg = new OpenFileDialog();
                ofdlg.InitialDirectory = this.initdir;
                ofdlg.Filter           = "csv file|*.csv";
                ofdlg.RestoreDirectory = false;
                ofdlg.FilterIndex      = 1;
                ofdlg.Title            = ResourceCulture.GetString("MENU_TOOL_IMPORT_FROM_CSV");
                if (ofdlg.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                string filename = ofdlg.FileName;
                initdir = filename;

                HtmlElement script = this.webBrowser.Document.GetElementById("_#@#_embedded_stript_id_check_if_element_visible");
                if (script == null)
                {
                    script = this.webBrowser.Document.CreateElement("script");
                    script.SetAttribute("type", "text/javascript");
                    script.SetAttribute("id", "_#@#_embedded_stript_id_check_if_element_visible");
                    script.SetAttribute("text", this.embededScript);
                    this.webBrowser.Document.Body.AppendChild(script);
                }

                object[] args = new object[1];

                HtmlElementCollection trs = table.GetElementsByTagName("tr");
                CsvStreamReader       csr = new CsvStreamReader(filename);

                for (int i = 0, m = 0; i < trs.Count && m < csr.RowCount; i++)
                {
                    if (!trs[i].Parent.Parent.Equals(table))   // trs[i].Parent.TagName == "TBODY";
                    {
                        continue;
                    }
                    bool validRow             = false;
                    HtmlElementCollection tds = trs[i].GetElementsByTagName("td");
                    for (int j = 0, n = 0; j < tds.Count && n < csr.ColCount; j++)
                    {
                        HtmlElementCollection hec = tds[j].Children;
                        if (hec == null)
                        {
                            continue;
                        }
                        for (int k = 0; k < hec.Count; k++)
                        {
                            // Only input, select and textarea can be filled with value.
                            string tagname = hec[k].TagName.ToLower();
                            if (tagname != "input" && tagname != "select" && tagname != "textarea")
                            {
                                continue;
                            }
                            // If the element is a input, then the type of it must be text.
                            if (tagname == "input" && hec[k].GetAttribute("type").ToLower() != "text")
                            {
                                continue;
                            }
                            // Check if the object is visible.
                            string id    = hec[k].GetAttribute("id");
                            string newId = "#@#_temp_id_123456";
                            if (id != null)
                            {
                                newId = id + "_#@#_temp_id_123456";
                            }
                            args[0] = newId;
                            hec[k].SetAttribute("id", newId);
                            object obj = this.webBrowser.Document.InvokeScript("__isVisible", args);
                            hec[k].SetAttribute("id", id);

                            if (obj == null || obj.ToString() == "none")
                            {
                                continue;
                            }

                            // Elements must be editable.
                            if (!hec[k].Enabled)
                            {
                                ++n;
                                continue;
                            }
                            // Copy text to elements' inner area.
                            if (tagname == "input" || tagname == "textarea")
                            {
                                hec[k].InnerText = csr[m + 1, n + 1].ToString();
                                validRow         = true;
                            }
                            else  // select
                            {
                                foreach (HtmlElement opt in hec[k].GetElementsByTagName("option"))
                                {
                                    string text = csr[m + 1, n + 1].ToString();
                                    validRow = true;
                                    if (opt.InnerText == text)
                                    {
                                        opt.SetAttribute("selected", "selected");
                                        break;
                                    }
                                }
                            }
                            ++n;
                        }
                    }
                    if (validRow)
                    {
                        ++m;
                    }
                }
            }
            catch (System.Exception ex) {
                string txt = ResourceCulture.GetString("MSGBOX_TXT_IMPORT_FAIL");
                this.msgbox.ShowMessageDialog(txt, "error");
                if (thr != null)
                {
                    thr.Append(ex.StackTrace);
                }
            }
        }