This repository was archived by the owner on Dec 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathForm1.cs
543 lines (504 loc) · 23.7 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Runtime.InteropServices;
using System.Web;
using Microsoft.Win32;
namespace FacebookWidget
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override CreateParams CreateParams
{
get
{
var Params = base.CreateParams;
Params.ExStyle |= 0x80;
return Params;
}
}
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private const UInt32 SWP_NOSIZE = 0x0001;
private const UInt32 SWP_NOMOVE = 0x0002;
private const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern Int32 GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT Rect);
string id, cookie, position, xOffset, yOffset, response, msgNum = "0";
int phase = 1, boxWidth = 0, boxHeight = 0;
bool retrievalError = false;
void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{
int xOs = Convert.ToInt32(xOffset), yOs = Convert.ToInt32(yOffset);
int resWidth = Screen.PrimaryScreen.Bounds.Width, resHeight = Screen.PrimaryScreen.Bounds.Height;
switch (position)
{
case "UpperLeft":
this.Left = xOs;
this.Top = yOs;
break;
case "UpperRight":
this.Left = resWidth - this.Size.Width - xOs;
this.Top = yOs;
break;
case "LowerLeft":
this.Left = xOs;
this.Top = resHeight - this.Size.Height - yOs;
break;
case "LowerRight":
this.Left = resWidth - this.Size.Width - xOs;
this.Top = resHeight - this.Size.Height - yOs;
break;
default:
MessageBox.Show("Unknown position type '" + position + "', the supported positions are 'UpperLeft', 'UpperRight', 'LowerLeft' or 'LowerRight'", "FacebookWidget", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
break;
}
}
void GetInformation()
{
try
{
IniFile Config = new IniFile(Application.StartupPath + "\\Config.ini");
WebClient client = new WebClient();
client.Headers.Add("accept-language", "en;q=0.9,vi;q=0.8,fr-FR;q=0.7,fr;q=0.6,en-US;q=0.5");
client.Headers.Add("cache-control", "max-age=0");
client.Headers.Add("cookie", cookie);
client.Headers.Add("sec-ch-ua", "\"Chromium\";v=\"104\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"104\"");
client.Headers.Add("sec-ch-ua-mobile", "?0");
client.Headers.Add("sec-ch-ua-platform", "\"Windows\"");
client.Headers.Add("sec-fetch-dest", "document");
client.Headers.Add("sec-fetch-mode", "navigate");
client.Headers.Add("sec-fetch-site", "none");
client.Headers.Add("sec-fetch-user", "?1");
client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36");
if (phase == 1)
{
Stream data = client.OpenRead(String.IsNullOrEmpty(cookie) ? ("https://mbasic.facebook.com/" + id) : ("https://mbasic.facebook.com/" + id + "?v=timeline"));
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
response = s;
var html = new HtmlAgilityPack.HtmlDocument();
html.LoadHtml(s);
if (!String.IsNullOrEmpty(cookie))
{
if (String.IsNullOrEmpty(Config.Read("CustomImagePath", "Config")))
{
var avatar = html.DocumentNode.SelectSingleNode("//div[contains(@class, 'acw')]").SelectSingleNode("//img[contains(@alt, 'profile picture')]").Attributes["src"].Value.Replace("&", "&");
pictureBox1.Load(avatar);
}
else pictureBox1.Load(Config.Read("CustomImagePath", "Config"));
var name = html.DocumentNode.SelectSingleNode("//span/div/span/strong").InnerText;
if (name.Contains("(")) name = name.Substring(0, name.IndexOf('(') - 1);
if (name.IndexOf('(') - 1 > 0) label1.Text = name.Substring(0, name.IndexOf('(') - 1);
else label1.Text = name;
try
{
var x = html.DocumentNode.SelectSingleNode("//a[contains(@href, 'photos/change/profile_picture')]").InnerText;
label1.Text = name + " (You)";
}
catch { }
var status = "Online";
try
{
if (System.Web.HttpUtility.HtmlDecode(html.DocumentNode.SelectSingleNode("//span/div/span/img").Attributes["aria-label"].Value).Contains(name)) status = "Offline";
label2.Text = status;
}
catch
{
label2.Text = "Offline";
}
if (label2.Text == "Online")
{
try
{
if (!String.IsNullOrEmpty(Config.Read("OnlineStatusColor", "Config"))) label2.ForeColor = Color.FromName(Config.Read("OnlineStatusColor", "Config"));
}
catch
{
MessageBox.Show("Unsupported color name '" + Config.Read("OnlineStatusColor", "Config") + "' in OnlineStatusColor", "FacebookWidget", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else label2.ForeColor = Color.FromName(Config.Read("StatusColor", "Config"));
}
else
{
if (String.IsNullOrEmpty(Config.Read("CustomImagePath", "Config")))
{
var avatar = html.DocumentNode.SelectSingleNode("//img[contains(@alt, 'profile')]").Attributes["src"].Value.Replace("&", "&");
pictureBox1.Load(avatar);
}
else pictureBox1.Load(Config.Read("CustomImagePath", "Config"));
var name = html.DocumentNode.SelectSingleNode("//div[@id='cover-name-root']").InnerText;
label1.Text = name;
label2.Text = "Active status unavailable";
}
data.Close();
reader.Close();
if (!String.IsNullOrEmpty(cookie)) phase = 2;
}
else if (phase == 2)
{
Stream data = client.OpenRead("https://mbasic.facebook.com/home.php");
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
var html = new HtmlAgilityPack.HtmlDocument();
response = s;
html.LoadHtml(s);
var text = "";
try
{
text = html.DocumentNode.SelectSingleNode("//nav/a/strong[contains(text(), 'Messages')]//span").InnerText;
if (text.Contains("(") && text.Contains(")"))
{
text = html.DocumentNode.SelectSingleNode("//nav/a[contains(@href, '" + id + "') and contains(@aria-label, 'new message')]").InnerText;
if (text.Contains("(") && text.Contains(")"))
{
try
{
var p = text.Substring(text.IndexOf("(") + 1);
label2.Text = p.Substring(0, p.IndexOf(")")) + " unread message" + (p.Substring(0, p.IndexOf(")")) != "1" ? "s" : "");
if (p.Substring(0, p.IndexOf(")")) != "0" && p.Substring(0, p.IndexOf(")")) != msgNum)
{
notifyIcon1.BalloonTipText = (label1.Text.Contains(" (You)") ? "You have " : (label1.Text + " send you ")) + p.Substring(0, p.IndexOf(")")) + " new message" + (p.Substring(0, p.IndexOf(")")) != "1" ? "s" : "") + ".";
notifyIcon1.ShowBalloonTip(15000);
}
msgNum = p.Substring(0, p.IndexOf(")"));
}
catch
{
label2.Text = "0 unread messages";
}
}
else label2.Text = "0 unread messages";
}
else label2.Text = "0 unread messages";
}
catch
{
label2.Text = "0 unread messages";
}
data.Close();
reader.Close();
phase = 1;
}
if (!String.IsNullOrEmpty(Config.Read("CustomName", "Config")))
{
label1.Text = Config.Read("CustomName", "Config").ToString();
}
}
catch (Exception ex)
{
File.WriteAllText(Application.StartupPath + "\\WidgetLog.log", "Response:\n" + response + "\nErrorInformation:\n" + ex.ToString());
if (retrievalError == false) MessageBox.Show("Cannot retrieve the user data!\nPlease check again the Facebook User ID" + (String.IsNullOrEmpty(cookie) ? ", or provide a Facebook cookie for a better retrieving" : ", or provide another cookie and try again.") + ".\nIf you have checked it but the error still occurs, you can contact the developer at GitHub.com/NozakiYuu.\n\nError information:\n" + ex.ToString() + "\n\nTo view more information, open the WidgetLog.log file in the same folder as the executable.", "FacebookWidget", MessageBoxButtons.OK, MessageBoxIcon.Error);
retrievalError = true;
label2.Text = "Cannot retrieve data!";
}
}
private void Form1_Load(object sender, EventArgs e)
{
if (System.Diagnostics.Process.GetProcessesByName("FacebookWidget").Length > 1)
{
timer1.Enabled = false;
timer2.Enabled = false;
MessageBox.Show("Only 1 instance of FacebookWidget is allowed. End the currently running process first and try again.", "FacebookWidget", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
else
{
SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged);
if (File.Exists(Application.StartupPath + "\\Config.ini"))
{
var Config = new IniFile(Application.StartupPath + "\\Config.ini");
id = Config.Read("ID", "Config");
cookie = Config.Read("Cookie", "Config");
this.Size = new Size(Convert.ToInt32(Config.Read("Width", "Config")), Convert.ToInt32(Config.Read("Height", "Config")));
position = Config.Read("Position", "Config");
xOffset = Config.Read("XOffset", "Config");
yOffset = Config.Read("YOffset", "Config");
int xOs = Convert.ToInt32(xOffset), yOs = Convert.ToInt32(yOffset);
int resWidth = Screen.PrimaryScreen.Bounds.Width, resHeight = Screen.PrimaryScreen.Bounds.Height;
switch (position)
{
case "UpperLeft":
this.Left = xOs;
this.Top = yOs;
break;
case "UpperRight":
this.Left = resWidth - this.Size.Width - xOs;
this.Top = yOs;
break;
case "LowerLeft":
this.Left = xOs;
this.Top = resHeight - this.Size.Height - yOs;
break;
case "LowerRight":
this.Left = resWidth - this.Size.Width - xOs;
this.Top = resHeight - this.Size.Height - yOs;
break;
default:
MessageBox.Show("Unknown position type '" + position + "', the supported positions are 'UpperLeft', 'UpperRight', 'LowerLeft' or 'LowerRight'", "FacebookWidget", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
break;
}
try
{
this.Font = new System.Drawing.Font(Config.Read("FontName", "Config"), Convert.ToSingle(Convert.ToInt32(Config.Read("FontSize", "Config"))));
}
catch
{
MessageBox.Show("Cannot load the given font with the font name '" + Config.Read("FontName", "Config") + "' and size " + Config.Read("FontSize", "Config"), "FacebookWidget", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
try
{
label1.ForeColor = Color.FromName(Config.Read("NameColor", "Config"));
}
catch
{
MessageBox.Show("Unsupported color name '" + Config.Read("NameColor", "Config") + "' in NameColor", "FacebookWidget", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
try
{
label2.ForeColor = Color.FromName(Config.Read("StatusColor", "Config"));
}
catch
{
MessageBox.Show("Unsupported color name '" + Config.Read("StatusColor", "Config") + "' in StatusColor", "FacebookWidget", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
try
{
this.BackColor = Color.FromName(Config.Read("TransparencyKey", "Config"));
this.TransparencyKey = Color.FromName(Config.Read("TransparencyKey", "Config"));
}
catch
{
MessageBox.Show("Unsupported color name '" + Config.Read("TransparencyKey", "Config") + "' in TransparencyKey", "FacebookWidget", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
if (!String.IsNullOrEmpty(Config.Read("AdditionalStyle", "Config")))
{
switch (Config.Read("AdditionalStyle", "Config"))
{
case "Bold":
label1.Font = new Font(label1.Font.Name, label1.Font.Size, FontStyle.Bold);
label2.Font = new Font(label2.Font.Name, label2.Font.Size, FontStyle.Bold);
break;
case "Italic":
label1.Font = new Font(label1.Font.Name, label1.Font.Size, FontStyle.Italic);
label2.Font = new Font(label2.Font.Name, label2.Font.Size, FontStyle.Italic);
break;
default:
MessageBox.Show("Unknown style '" + Config.Read("AdditionalStyle", "Config") + "', the supported styles are 'Bold' or 'Italic'", "FacebookWidget", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
}
}
else
{
MessageBox.Show("Cannot find the config file!", "FacebookWidget", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)(768 | 3072);
GetInformation();
SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
tableLayoutPanel1.ColumnStyles[0].Width = pictureBox1.Height + 8;
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
notifyIcon1.ShowBalloonTip(5000);
}
boxWidth = this.Size.Width;
boxHeight = this.Size.Height;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
GetInformation();
}
private void timer2_Tick(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
this.Focus();
this.BringToFront();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
Click();
}
void Hover()
{
this.BackColor = Color.Gray;
}
void Leave()
{
var Config = new IniFile(Application.StartupPath + "\\Config.ini");
this.BackColor = Color.FromName(Config.Read("TransparencyKey", "Config"));
}
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
Hover();
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
Leave();
}
private void tableLayoutPanel1_MouseHover(object sender, EventArgs e)
{
Hover();
}
private void tableLayoutPanel1_MouseLeave(object sender, EventArgs e)
{
Leave();
}
private void tableLayoutPanel2_MouseHover(object sender, EventArgs e)
{
Hover();
}
private void tableLayoutPanel2_MouseLeave(object sender, EventArgs e)
{
Leave();
}
private void label1_MouseHover(object sender, EventArgs e)
{
Hover();
}
private void label1_MouseLeave(object sender, EventArgs e)
{
Leave();
}
private void label2_MouseHover(object sender, EventArgs e)
{
Hover();
}
private void label2_MouseLeave(object sender, EventArgs e)
{
Leave();
}
void Click()
{
IniFile Config = new IniFile(Application.StartupPath + "\\Config.ini");
if (Config.Read("CustomClickLink") == null)
{
System.Diagnostics.Process.Start("https://www.facebook.com/" + id);
}
else
{
System.Diagnostics.Process.Start(Config.Read("CustomClickLink").Replace("$id", id));
}
}
private void label1_Click(object sender, EventArgs e)
{
Click();
}
private void label2_Click(object sender, EventArgs e)
{
Click();
}
private void tableLayoutPanel1_Click(object sender, EventArgs e)
{
Click();
}
private void tableLayoutPanel2_Click(object sender, EventArgs e)
{
Click();
}
private void timer3_Tick(object sender, EventArgs e)
{
IntPtr handle = GetForegroundWindow();
if (handle != null)
{
RECT Rect = new RECT();
if (GetWindowRect(handle, ref Rect))
{
int width = Rect.right - Rect.left, height = Rect.bottom - Rect.top;
if (width == Screen.PrimaryScreen.Bounds.Width && height >= Screen.PrimaryScreen.Bounds.Height - boxHeight)
{
uint pid;
GetWindowThreadProcessId(handle, out pid);
System.Diagnostics.Process proc = System.Diagnostics.Process.GetProcessById(Convert.ToInt32(pid));
if (proc.ProcessName != "explorer")
{
timer2.Enabled = false;
this.Size = new Size(0, 0);
}
else
{
this.Size = new Size(boxWidth, boxHeight);
timer2.Enabled = true;
}
}
else
{
this.Size = new Size(boxWidth, boxHeight);
timer2.Enabled = true;
}
}
}
}
void HandleMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
HandleMouseDown(e);
}
private void label1_MouseDown(object sender, MouseEventArgs e)
{
HandleMouseDown(e);
}
private void label2_MouseDown(object sender, MouseEventArgs e)
{
HandleMouseDown(e);
}
private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
{
HandleMouseDown(e);
}
}
}