-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageResolver.js
255 lines (222 loc) · 7.7 KB
/
PageResolver.js
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
const urlMatcher = require("./lib/UrlMatcher");
class PageResolver {
page;
_browserFactory;
_pageFactory;
_waitTimeout = 1000 * 30;
constructor(options) {
this._browserFactory = options.buildBrowserFactory();
this._cacheStatics = options.buildCacheComponent();
this._pageFactory = this._browserFactory.getPageFactory();
}
async getBrowser() {
return await this._browserFactory.getBrowser();
}
getBrowserFactory() {
return this._browserFactory;
}
async getPage() {
if (this.page) {
return this.page;
}
this.page = await this._pageFactory.getPage();
return this.page;
}
async newPage() {
this.page = await this._pageFactory.newPage();
await this.page.bringToFront();
return this.page;
}
setPage(page) {
this.page = page;
}
async waitType(selector, _data) {
const page = await this.getPage();
await page.waitForSelector(selector, {timeout: this._waitTimeout});
await page.type(selector, _data);
}
async waitClick(selector) {
const page = await this.getPage();
await page.waitForSelector(selector);
await page.click(selector, {timeout: this._waitTimeout});
}
/**
* 点击触发新tab
* @param selector
* @returns {Promise<Page>} 新的page
*/
async waitClickNewPage(selector) {
const page = await this.getPage();
const browser = await this.getBrowser();
await page.waitForSelector(selector); // 等待并获取点击跳转的goto元素
const link = await page.$(selector);
const newPagePromise = new Promise(x => browser.once('targetcreated', target => x(target.page())));
await link.click(); // 点击跳转
return await newPagePromise; // newPage就是a链接打开窗口的Page对象
}
/**
* 点击触发刷新当前页
* @param selector
* @returns {Promise<void>}
*/
async waitClick2Page(selector) {
const page = await this.getPage();
await page.waitForSelector(selector);
await Promise.all([
page.waitForNavigation({timeout: this._waitTimeout}),
page.click(selector, {timeout: this._waitTimeout})
]);
}
async waitForSelector(selector) {
const _page = await this.getPage();
await _page.waitForSelector(selector, {timeout: this._waitTimeout});
}
async goto(url) {
const page = await this.getPage();
await page.goto(url);
}
config() {
return new ConfigBuilder(this);
}
async $(selector) {
const page = await this.getPage();
return await page.$(selector);
}
async evaluate(pageFunction, ...args) {
const page = await this.getPage();
return page.evaluate(pageFunction, args);
}
async _removeRequestResponseListener() {
const page = await this.getPage();
if (page.lastRequestCallback) {
await page.removeListener('request', page.lastRequestCallback);
}
if (page.lastResponseCallback) {
await page.removeListener('response', page.lastResponseCallback);
}
page.lastRequestCallback = null;
page.lastResponseCallback = null;
}
async clear() {
const _chromePage = await this.getPage();
this._removeRequestResponseListener();
await _chromePage.on('request', request => {
request.continue();
});
}
}
class ConfigBuilder {
constructor(pageResolver) {
this.pageResolver = pageResolver;
}
useStaticCache(cacheSource, _except) {
const statics = ['**.js', '**.js?*', '**.ttf?*', '**.ttf', '**.css?*', '**.woff?*', '**.woff2?*', '**.png', '**.jpg', '**.jpge', '**.woff', '**.woff2', '**.css', '**.svg', '**.ico', '**.gif'];
if (cacheSource) {
statics.push(cacheSource);
}
this.useCache(statics, _except);
return this;
}
useCache(_matches, _except) {
this._cacheMatches = _matches;
this._cacheExcept = _except;
return this;
}
/**
* 默认超时时间,单位秒
* @param waitTimeout
*/
waitTimeout(waitTimeout) {
this.pageResolver._waitTimeout = waitTimeout * 1000;
return this;
}
/**
*
* @param cacheTimeout 缓存时间,单位秒
*/
cacheTimeout(cacheTimeout) {
this._cacheTimeout = cacheTimeout;
return this;
}
/**
* 用来设置缓存到磁盘的响应头的
* @param cacheHeaderFilter
* @returns {ConfigBuilder}
*/
cacheHeaderFilter(cacheHeaderFilter) {
this._cacheHeaderFilter = cacheHeaderFilter;
return this;
}
/**
* 用来修改从缓存中获取的响应头,只有被缓存的数据才有效
* @param cacheHeaderFilter
* @returns {ConfigBuilder}
*/
responseHeaderFilter(responseHeaderFilter) {
this._responseHeaderFilter = responseHeaderFilter;
return this;
}
/**
* 响应过滤器,开/关缓存都有效,开启缓存时,response是缓存中的数据
* @param responseFilter
* @returns {ConfigBuilder}
*/
responseFilter(responseFilter) {
this._responseFilter = responseFilter;
return this;
}
abortStaticSource(abortSource, _except) {
const statics = ['**.css?*', '**.ttf?*', '**.woff?*', '**.woff2?*', '**.gif?*', '**.png', '**.jpg', '**.jpge', '**.woff', '**.woff2', '**.css', '**.svg', '**.ico', '**.gif', '**.ttf'];
if (abortSource) {
statics.push(abortSource);
}
this.abortSource(statics, _except);
return this;
}
/**
* @param {Array} abortMatches
*/
abortSource(abortMatches, _except) {
this._abortMatches = abortMatches;
this._abortExcept = _except;
return this;
}
async build() {
const _chromePage = await this.pageResolver.getPage();
await this.pageResolver._removeRequestResponseListener();
if (this._cacheMatches || this._abortMatches) {
this.pageResolver._cacheStatics.setCacheTimeout(this._cacheTimeout);
await _chromePage.setRequestInterception(true);
_chromePage.lastRequestCallback = async request => {
if (urlMatcher.match(request.url(), this._abortMatches, this._abortExcept)) {
await request.abort();
} else if (await this.pageResolver._cacheStatics.checkLoad(request.url(), this._cacheMatches, this._cacheExcept)) {
await this.pageResolver._cacheStatics.loadCache(request, this._responseHeaderFilter);
} else {
await request.continue();
}
};
_chromePage.on('request', _chromePage.lastRequestCallback);
}
if (this._cacheMatches) {
_chromePage.lastResponseCallback = async response => {
const url = response.url();
if (await this.pageResolver._cacheStatics.checkStore(url, this._cacheMatches, this._cacheExcept)) {
await this.pageResolver._cacheStatics.storeSource(response, this._cacheHeaderFilter);
}
if (this._responseFilter) {
this._responseFilter(url, response);
}
};
}else{
_chromePage.lastResponseCallback = async response => {
const url = response.url();
if (this._responseFilter) {
this._responseFilter(url, response);
}
};
}
_chromePage.on('response', _chromePage.lastResponseCallback);
}
}
module.exports = PageResolver;