-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathForm1.cs
119 lines (97 loc) · 3.62 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using Microsoft.Web.WebView2.WinForms;
using static System.Net.WebRequestMethods;
namespace DiscordWV
{
public partial class Discord : Form
{
public Discord()
{
InitializeComponent();
textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
}
private void webView21_Click(object sender, EventArgs e)
{
}
private void toolStripComboBox1_Click(object sender, EventArgs e)
{
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
this.Opacity = ((double)(trackBar1.Value) / 100.0);
this.Text = (trackBar1.Value.ToString());
}
private void button1_Click(object sender, EventArgs e)
{
// this.webView21.Source = new Uri(textBox1.Text);
string userInput = textBox1.Text;
// Check if the input is empty
if (string.IsNullOrWhiteSpace(userInput))
{
MessageBox.Show("Please enter a URL.");
return;
}
try
{
// Ensure the URL is properly formatted
UriBuilder uriBuilder = new UriBuilder(userInput);
// Default to "http" if no scheme is provided
if (string.IsNullOrWhiteSpace(uriBuilder.Scheme))
{
uriBuilder.Scheme = Uri.UriSchemeHttp;
}
// Set the Source of the WebView to the cleaned URL
this.webView21.Source = uriBuilder.Uri;
}
catch (UriFormatException ex)
{
// If the input is not a valid URL, treat it as a search term and use Google search
string googleSearchUrl = $"https://www.google.com/search?q={Uri.EscapeDataString(userInput)}";
this.webView21.Source = new Uri(googleSearchUrl);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// Prevent the beep sound on Enter key press
e.SuppressKeyPress = true;
// Call the method to handle the URL or search term
HandleUserInput();
}
}
private void HandleUserInput()
{
string userInput = textBox1.Text;
// Check if the input is empty
if (string.IsNullOrWhiteSpace(userInput))
{
MessageBox.Show("Please enter a URL or search term.");
return;
}
try
{
// Ensure the URL is properly formatted
UriBuilder uriBuilder = new UriBuilder(userInput);
// Default to "http" if no scheme is provided
if (string.IsNullOrWhiteSpace(uriBuilder.Scheme))
{
uriBuilder.Scheme = Uri.UriSchemeHttp;
}
// Set the Source of the WebView to the cleaned URL
this.webView21.Source = uriBuilder.Uri;
}
catch (UriFormatException)
{
// If the input is not a valid URL, treat it as a search term and use Google search
string googleSearchUrl = $"https://www.google.com/search?q={Uri.EscapeDataString(userInput)}";
this.webView21.Source = new Uri(googleSearchUrl);
}
}
}
}