-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathWebPage.cs
More file actions
129 lines (109 loc) · 4.38 KB
/
WebPage.cs
File metadata and controls
129 lines (109 loc) · 4.38 KB
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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using PuppeteerSharp;
using PuppeteerSharp.Input;
using UniversalDownloaderPlatform.PuppeteerEngine.Interfaces.Wrappers.Browser;
namespace UniversalDownloaderPlatform.PuppeteerEngine.Wrappers.Browser
{
/// <summary>
/// This class is a wrapper around a Puppeteer Sharp's page object used to implement proper dependency injection mechanism
/// It should copy any used puppeteer sharp's method definitions for ease of code maintenance
/// </summary>
public sealed class WebPage : IWebPage
{
private readonly IPage _page;
private bool _configured;
public WebPage(IPage page)
{
_page = page ?? throw new ArgumentNullException(nameof(page));
_configured = false;
}
public bool IsClosed
{
get { return _page.IsClosed; }
}
public async Task<IWebResponse> GoToAsync(string url, int? timeout = null, WaitUntilNavigation[] waitUntil = null)
{
await ConfigurePage();
if (!timeout.HasValue)
timeout = 60000;
IResponse response = await _page.GoToAsync(url, timeout, waitUntil);
IWebResponse webResponse = new WebResponse(response);
return webResponse;
}
public async Task SetUserAgentAsync(string userAgent)
{
await _page.SetUserAgentAsync(userAgent);
}
public async Task<string> GetContentAsync()
{
return await _page.GetContentAsync();
}
public async Task<IWebRequest> WaitForRequestAsync(Func<IRequest, bool> predicate, WaitForOptions options = null)
{
await ConfigurePage();
IRequest request = await _page.WaitForRequestAsync(predicate, options);
IWebRequest webRequest = new WebRequest(request);
return webRequest;
}
public async Task<IWebResponse> WaitForResponseAsync(Func<IResponse, bool> predicate, WaitForOptions options = null)
{
await ConfigurePage();
IResponse response = await _page.WaitForResponseAsync(predicate, options);
IWebResponse webResponse = new WebResponse(response);
return webResponse;
}
public async Task WaitForNetworkIdleAsync(WaitForNetworkIdleOptions options = null)
{
await ConfigurePage();
await _page.WaitForNetworkIdleAsync(options);
}
public async Task WaitForSelectorAsync(string selector, WaitForSelectorOptions options = null)
{
await ConfigurePage();
await _page.WaitForSelectorAsync(selector, options);
}
public async Task TypeAsync(string selector, string text, TypeOptions options = null)
{
await _page.TypeAsync(selector, text, options);
}
public async Task ClickAsync(string selector, ClickOptions options = null)
{
await _page.ClickAsync(selector, options);
}
public async Task<CookieParam[]> GetCookiesAsync(params string[] urls)
{
return await _page.GetCookiesAsync(urls);
}
public async Task CloseAsync(PageCloseOptions options = null)
{
await _page.CloseAsync(options);
}
/// <summary>
/// Perform required configuration for a page. (avoid cloudflare triggering, do not load images, etc)
/// </summary>
/// <returns></returns>
private async Task ConfigurePage()
{
if (!_configured)
{
Dictionary<string, string> headerDictionary = new Dictionary<string, string>();
headerDictionary.Add("Accept-Language", "en-GB,en-US;q=0.9,en;q=0.8");
await _page.SetExtraHttpHeadersAsync(headerDictionary);
//await _page.SetRequestInterceptionAsync(true);
_page.DefaultTimeout = 0;
_page.DefaultNavigationTimeout = 0;
// disable images to download
/*_page.Request += (sender, e) =>
{
if (e.Request.ResourceType == ResourceType.Image)
e.Request.AbortAsync();
else
e.Request.ContinueAsync();
};*/
_configured = true;
}
}
}
}