-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathPageCollector.ts
More file actions
141 lines (129 loc) · 3.43 KB
/
PageCollector.ts
File metadata and controls
141 lines (129 loc) · 3.43 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
130
131
132
133
134
135
136
137
138
139
140
141
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type {Browser, HTTPRequest, Page} from 'puppeteer-core';
/**
* A generic class for collecting data from Puppeteer pages. It handles page
* creation and navigation to manage data collection lifecycle.
*
* @template T The type of data to collect.
* @public
*/
export class PageCollector<T> {
#browser: Browser;
#initializer: (page: Page, collector: (item: T) => void) => void;
/**
* The Array in this map should only be set once
* As we use the reference to it.
* Use methods that manipulate the array in place.
* @protected
*/
protected storage = new WeakMap<Page, T[]>();
/**
* @param browser - The Puppeteer browser instance.
* @param initializer - A function that sets up the data collection for a
* page.
*/
constructor(
browser: Browser,
initializer: (page: Page, collector: (item: T) => void) => void,
) {
this.#browser = browser;
this.#initializer = initializer;
}
/**
* Initializes the collector by setting up data collection for all existing
* pages and listening for new pages.
*/
async init() {
const pages = await this.#browser.pages();
for (const page of pages) {
this.#initializePage(page);
}
this.#browser.on('targetcreated', async target => {
const page = await target.page();
if (!page) {
return;
}
this.#initializePage(page);
});
}
/**
* Adds a new page to the collector and initializes it.
*
* @param page - The page to add.
*/
public addPage(page: Page) {
this.#initializePage(page);
}
#initializePage(page: Page) {
if (this.storage.has(page)) {
return;
}
const stored: T[] = [];
this.storage.set(page, stored);
page.on('framenavigated', frame => {
// Only reset the storage on main frame navigation
if (frame !== page.mainFrame()) {
return;
}
this.cleanup(page);
});
this.#initializer(page, value => {
stored.push(value);
});
}
/**
* Cleans up the stored data for a page. By default, it clears the entire
* collection.
*
* @param page - The page to clean up.
* @protected
*/
protected cleanup(page: Page) {
const collection = this.storage.get(page);
if (collection) {
// Keep the reference alive
collection.length = 0;
}
}
/**
* Gets the collected data for a specific page.
*
* @param page - The page to get data for.
* @returns The collected data, or an empty array if none.
*/
getData(page: Page): T[] {
return this.storage.get(page) ?? [];
}
}
/**
* A specific implementation of PageCollector for collecting network requests.
* @public
*/
export class NetworkCollector extends PageCollector<HTTPRequest> {
/**
* Cleans up network requests by removing all requests before the last
* navigation.
*
* @param page - The page to clean up.
* @override
*/
override cleanup(page: Page) {
const requests = this.storage.get(page) ?? [];
if (!requests) {
return;
}
const lastRequestIdx = requests.findLastIndex(request => {
return request.frame() === page.mainFrame()
? request.isNavigationRequest()
: false;
});
// Keep all requests since the last navigation request including that
// navigation request itself.
// Keep the reference
requests.splice(0, Math.max(lastRequestIdx, 0));
}
}