This repository has been archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Extensions.cs
151 lines (143 loc) · 5.03 KB
/
Extensions.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
using System;
using System.Net;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
namespace TrifleJS
{
public static class Extensions
{
/// <summary>
/// Returns a unix timestamp
/// </summary>
/// <returns></returns>
public static int ToUnixTimestamp(this DateTime date) {
if (date >= new DateTime(1970, 1, 1, 0, 0, 0, 0))
{
TimeSpan span = (date - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
return (int)span.TotalSeconds;
}
return -1;
}
/// <summary>
/// Gets an entry in a dictionary by specifying its type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dictionary"></param>
/// <param name="key"></param>
/// <returns></returns>
public static T Get<T>(this Dictionary<string, object> dictionary, string key)
{
try
{
if (dictionary.ContainsKey(key)) {
return (T)Convert.ChangeType(dictionary[key], typeof(T));
}
}
catch { }
return default(T);
}
/// <summary>
/// Gets an entry in a dictionary as an object
/// </summary>
/// <param name="dictionary"></param>
/// <param name="key"></param>
/// <returns></returns>
public static object Get(this Dictionary<string, object> dictionary, string key)
{
if (dictionary.ContainsKey(key))
{
return dictionary[key];
}
return null;
}
/// <summary>
/// Gets all ancestor frames in a HtmlWindow
/// </summary>
/// <param name="window"></param>
/// <returns></returns>
public static List<HtmlWindow> GetAllFrames(this HtmlWindow window) {
List<HtmlWindow> ancestors = new List<HtmlWindow> {window};
bool added; int count = 0;
do
{
// Keep recursing until we no longer
// have anything else to add.
added = false;
foreach (HtmlWindow ancestor in ancestors.ToArray())
{
foreach (HtmlWindow frame in ancestor.Frames)
{
if (!ancestors.Contains(frame))
{
ancestors.Add(frame);
added = true;
count++;
}
}
}
} while (added && count < 100); // Limit to 100 frames
return ancestors;
}
/// <summary>
/// Gets a DOM Element at specific X/Y coordinates.
/// Returns null if element not found.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public static HtmlElement GetElementFromPoint(this HtmlDocument document, object x, object y)
{
try
{
if (x != null && y != null)
{
Point point = new Point((int)x, (int)y);
return document.GetElementFromPoint(point);
}
}
catch { }
return null;
}
/// <summary>
/// Gets a list of DOM elements using a CSS query selector.
/// Only accepts simple selectors. (ie '.myclass' or '#myid' or 'mytag')
/// @see http://stackoverflow.com/questions/11271737/how-do-you-click-a-button-in-a-webbrowser-control
/// </summary>
/// <param name="document"></param>
/// <param name="selector"></param>
/// <returns></returns>
public static List<HtmlElement> GetElementFromSelector(this HtmlDocument document, string selector)
{
List<HtmlElement> output = new List<HtmlElement>();
if (!String.IsNullOrEmpty(selector))
{
selector = selector.Trim();
if (selector.StartsWith("#"))
{
HtmlElement result = document.GetElementById(selector.Remove(0, 1));
if (result != null) output.Add(result);
}
else
{
if (selector.StartsWith("."))
{
foreach (HtmlElement element in document.All)
{
if (element.GetAttribute("className") == selector.Remove(1))
output.Add(element);
}
}
else
{
foreach (HtmlElement element in document.GetElementsByTagName(selector))
output.Add(element);
}
}
}
return output;
}
}
}