Tuesday, November 11, 2025

questions for improvement

using ExamTopics; using Newtonsoft.Json; using Microsoft.Web.WebView2.WinForms; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Windows.Forms; using iTextSharp.tool.xml.css; public partial class DiscussionViewerForm : Form { private List _allData = new(); private List _filteredData = new(); private int _currentIndex = 0; private string _jsonPath = "ExamTopicsData.json"; string searchStoreFile = Path.Combine(Application.StartupPath, "last_search.txt"); private string simpleStoreFile = Path.Combine(Application.StartupPath, "simple_list.txt"); private HashSet simpleMarked = new HashSet(); public DiscussionViewerForm() { InitializeComponent(); this.Load += DiscussionViewerForm_Load; // Add Form Load event } private async void DiscussionViewerForm_Load(object sender, EventArgs e) { await webView.EnsureCoreWebView2Async(null); // Listen when a page finishes loading webView.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted; LoadData(); if (File.Exists(simpleStoreFile)) { simpleMarked = new HashSet(File.ReadAllLines(simpleStoreFile)); } if (File.Exists(searchStoreFile)) { try { string lastSearch = File.ReadAllText(searchStoreFile); txtSearch.Text = lastSearch; // ✅ Auto trigger search if exists if (!string.IsNullOrWhiteSpace(lastSearch)) btnSearch.PerformClick(); } catch { } } } private void btnSimple_Click(object sender, EventArgs e) { if (_filteredData.Count == 0) return; var current = _filteredData[_currentIndex]; string key = current.Id?.ToString() ?? current.Url; // choose any unique field if (!string.IsNullOrWhiteSpace(key)) { simpleMarked.Add(key); File.WriteAllLines(simpleStoreFile, simpleMarked); btnNext.PerformClick(); // auto move to next } } private async void CoreWebView2_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e) { string css = @" .show { display: none !important; } .full-width-header { display: none !important; } .action-row-container { display: none !important; } .contrib__ulimited.contrib__block { display: none !important; } .question-discussion-header { display: none !important; } "; string script = $@" let style = document.createElement('style'); style.innerHTML = `{css}`; document.head.appendChild(style); "; try { await webView.ExecuteScriptAsync(script); } catch (Exception ex) { MessageBox.Show("CSS Injection Error: " + ex.Message); } } private void LoadData() { if (!File.Exists(_jsonPath)) { MessageBox.Show("JSON file not found!"); return; } var json = File.ReadAllText(_jsonPath); _allData = JsonConvert.DeserializeObject>(json) ?? new(); _filteredData = _allData.ToList(); } private void DisplayRecord() { if (_filteredData.Count == 0) { webView.NavigateToString("

No records found

"); lblCounter.Text = "0 / 0"; return; } var rec = _filteredData[_currentIndex]; // ✅ Navigate using Chrome Engine (WebView2) webView.CoreWebView2.Navigate(rec.Link); lblCounter.Text = $"{_currentIndex + 1} / {_filteredData.Count}"; } private void btnNext_Click(object sender, EventArgs e) { do { if (_currentIndex < _filteredData.Count - 1) _currentIndex++; else break; } while (IsSimple(_filteredData[_currentIndex])); DisplayRecord(); } private bool IsSimple(dynamic record) { string key = record.Id?.ToString() ?? record.Url; return !string.IsNullOrWhiteSpace(key) && simpleMarked.Contains(key); } private void btnPrev_Click(object sender, EventArgs e) { if (_currentIndex > 0) { _currentIndex--; DisplayRecord(); } } private void btnSearch_Click(object sender, EventArgs e) { string term = txtSearch.Text.Trim(); // ✅ Save search text to file try { File.WriteAllText(searchStoreFile, term); } catch { } var terms = term.ToLower() .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(x => x.Trim()) .ToList(); if (terms.Count == 0) _filteredData = _allData.ToList(); else { _filteredData = _allData .Where(x => terms.Any(t => (x.Title != null && x.Title.ToLower().Contains(t)) || (x.Content != null && x.Content.ToLower().Contains(t)) ) ).ToList(); } _currentIndex = 0; lblFoundCount.Text = $"{_filteredData.Count} records found"; DisplayRecord(); } public class DiscussionRecord { public string Url { get; set; } public string Title { get; set; } public string Content { get; set; } public DateTime ScrapedOn { get; set; } public string Link { get; set; } public string Id { get; set; } } } using Microsoft.Web.WebView2.WinForms; partial class DiscussionViewerForm { private System.ComponentModel.IContainer components = null; private WebView2 webView; private System.Windows.Forms.TextBox txtSearch; private System.Windows.Forms.Button btnSearch; private System.Windows.Forms.Button btnPrev; private System.Windows.Forms.Button btnNext; private System.Windows.Forms.Label lblCounter; private System.Windows.Forms.Label lblFoundCount; // ✅ new label private System.Windows.Forms.Button btnSimple; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } private void InitializeComponent() { webView = new WebView2(); txtSearch = new TextBox(); btnSearch = new Button(); btnPrev = new Button(); btnNext = new Button(); lblCounter = new Label(); lblFoundCount = new Label(); btnSimple = new Button(); ((System.ComponentModel.ISupportInitialize)webView).BeginInit(); SuspendLayout(); // // webView // webView.AllowExternalDrop = true; webView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; webView.CreationProperties = null; webView.DefaultBackgroundColor = Color.White; webView.Location = new Point(12, 45); webView.Name = "webView"; webView.Size = new Size(760, 420); webView.TabIndex = 0; webView.ZoomFactor = 1D; // // txtSearch // txtSearch.Font = new Font("Segoe UI", 10F); txtSearch.Location = new Point(12, 12); txtSearch.Name = "txtSearch"; txtSearch.Size = new Size(300, 25); txtSearch.TabIndex = 1; // // btnSearch // btnSearch.Font = new Font("Segoe UI", 9F); btnSearch.Location = new Point(318, 12); btnSearch.Name = "btnSearch"; btnSearch.Size = new Size(70, 25); btnSearch.TabIndex = 2; btnSearch.Text = "Search"; btnSearch.UseVisualStyleBackColor = true; btnSearch.Click += btnSearch_Click; // // btnPrev // btnPrev.Font = new Font("Segoe UI", 9F); btnPrev.Location = new Point(395, 12); btnPrev.Name = "btnPrev"; btnPrev.Size = new Size(60, 25); btnPrev.TabIndex = 3; btnPrev.Text = "<< Prev"; btnPrev.UseVisualStyleBackColor = true; btnPrev.Click += btnPrev_Click; // // btnNext // btnNext.Font = new Font("Segoe UI", 9F); btnNext.Location = new Point(460, 12); btnNext.Name = "btnNext"; btnNext.Size = new Size(60, 25); btnNext.TabIndex = 4; btnNext.Text = "Next >>"; btnNext.UseVisualStyleBackColor = true; btnNext.Click += btnNext_Click; // // lblCounter // lblCounter.AutoSize = true; lblCounter.Font = new Font("Segoe UI", 10F); lblCounter.Location = new Point(360, 477); lblCounter.Name = "lblCounter"; lblCounter.Size = new Size(38, 19); lblCounter.TabIndex = 5; lblCounter.Text = "0 / 0"; // // lblFoundCount // lblFoundCount.AutoSize = true; lblFoundCount.Font = new Font("Segoe UI", 10F, FontStyle.Bold); lblFoundCount.Location = new Point(602, 13); lblFoundCount.Name = "lblFoundCount"; lblFoundCount.Size = new Size(115, 19); lblFoundCount.TabIndex = 6; lblFoundCount.Text = "0 records found"; // // btnSimple // btnSimple.Font = new Font("Segoe UI", 9F); btnSimple.Location = new Point(526, 12); btnSimple.Name = "btnSimple"; btnSimple.Size = new Size(70, 25); btnSimple.TabIndex = 7; btnSimple.Text = "Simple ✓"; btnSimple.UseVisualStyleBackColor = true; btnSimple.Click += btnSimple_Click; // // DiscussionViewerForm // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(784, 511); Controls.Add(webView); Controls.Add(txtSearch); Controls.Add(btnSearch); Controls.Add(btnPrev); Controls.Add(btnNext); Controls.Add(lblCounter); Controls.Add(lblFoundCount); Controls.Add(btnSimple); Name = "DiscussionViewerForm"; StartPosition = FormStartPosition.CenterScreen; Text = "ExamTopics Discussion Viewer"; ((System.ComponentModel.ISupportInitialize)webView).EndInit(); ResumeLayout(false); PerformLayout(); } }

No comments:

Post a Comment

Featured Post

questions for improvement

using ExamTopics; using Newtonsoft.Json; using Microsoft.Web.WebView2.WinForms; using System; using System.Collections.Generic; using System...