/
WebPageService.cs
150 lines (125 loc) · 4.53 KB
/
WebPageService.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
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.XPath;
using ExCSS;
using PuppeteerSharp;
namespace WebPageUpdated
{
public enum SelectorType
{
Unspecified,
Css,
Xpath
}
public class WebPageService
{
private Page _page;
public string PathToScreenshot { get; private set; }
private WebPageService() { }
private async Task<WebPageService> InitializeAsync(string webpageUrl)
{
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true
});
_page = await browser.NewPageAsync();
await _page.SetViewportAsync(new ViewPortOptions
{
Width = 1280,
Height = 720
});
await GoToPage(webpageUrl);
return this;
}
public static Task<WebPageService> LoadPage(string webpageUrl)
{
if (string.IsNullOrWhiteSpace(webpageUrl))
{
throw new ArgumentNullException("webpageUrl", "Please specify a web page URL.");
}
var wp = new WebPageService();
return wp.InitializeAsync(webpageUrl);
}
public async Task<WebPageService> GoToPage(string webPageUrl)
{
await _page.GoToAsync(webPageUrl);
return this;
}
public async Task<string> GetMd5ValueOfElement(string pathOfElement)
{
ElementHandle selector;
string val = null;
switch (GetSelectorType(pathOfElement))
{
case SelectorType.Unspecified:
case SelectorType.Css:
pathOfElement = !string.IsNullOrWhiteSpace(pathOfElement) ? pathOfElement : "body";
selector = await _page.QuerySelectorAsync(pathOfElement);
val = await _page.EvaluateFunctionAsync<string>("e => { e.style.border = '5px solid yellow'; return e.textContent }", selector);
break;
case SelectorType.Xpath:
selector = (await _page.XPathAsync(pathOfElement))[0];
val = await _page.EvaluateFunctionAsync<string>("e => { e.style.border = '5px solid yellow'; return e.textContent }", selector);
break;
}
return GetMD5(val);
}
public async Task<string> TakeScreenshot()
{
var screenshotPath = GetTempFilePathWithExtension(".jpg");
await _page.ScreenshotAsync(screenshotPath, new ScreenshotOptions()
{
Quality = 95,
// FullPage = true,
Type = ScreenshotType.Jpeg
});
// Base64PageScreenshot = Convert.ToBase64String(File.ReadAllBytes(screenshotPath));
PathToScreenshot = screenshotPath;
return PathToScreenshot;
}
public static SelectorType GetSelectorType(string selector)
{
SelectorType sel = SelectorType.Unspecified;
try
{
XPathExpression.Compile(selector);
sel = SelectorType.Xpath;
}
catch (XPathException) { }
try
{
var parser = new StylesheetParser();
var stylesheet = parser.ParseSelector(selector);
if (stylesheet == null)
{
throw new ArgumentException();
}
sel = SelectorType.Css;
}
catch (ArgumentException) { }
return sel;
}
private static string GetTempFilePathWithExtension(string extension)
{
var path = Path.GetTempPath();
var fileName = Guid.NewGuid().ToString() + extension;
return Path.Combine(path, fileName);
}
private static string GetMD5(string str)
{
// Use input string to calculate MD5 hash
StringBuilder hash = new StringBuilder();
MD5CryptoServiceProvider md5provider = new MD5CryptoServiceProvider();
byte[] bytes = md5provider.ComputeHash(new UTF8Encoding().GetBytes(str));
for (int i = 0; i < bytes.Length; i++)
{
hash.Append(bytes[i].ToString("x2"));
}
return hash.ToString();
}
}
}