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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
import puppeteer from 'puppeteer'; import { htmlGenWaterMark } from './watermark';
class PuppeteerHelper { constructor() { this.instance = null; this.MAX_WSE = 4; this.WSE_LIST = []; this.PAGE_COUNT = 1000; this.PAGE_NUM = []; this.replaceTimer = []; this.p_config = { headless: true, args: [ '--disable-gpu', '--disable-dev-shm-usage', '--disable-setuid-sandbox', '--no-first-run', '--no-sandbox', '--no-zygote', '--single-process', ], };
this._init(); }
static getInstance() { if (!this.instance) { this.instance = new PuppeteerHelper(); } return this.instance; }
_init() { (async () => { console.log('【PuppeteerHelper】puppeteer config:', this.p_config); for (let i = 0; i < this.MAX_WSE; i++) { await this._generateBrowser(i); } console.log('【PuppeteerHelper】WSE_LIST:', this.WSE_LIST); })(); }
async _generateBrowser(num) { const browser = await puppeteer.launch(this.p_config); this.WSE_LIST[num] = await browser.wsEndpoint(); this.PAGE_NUM[num] = 1;
return browser; }
async _replaceBrowserInstance(browser, num, retries = 2) { clearTimeout(this.replaceTimer[num]);
const pageNum = this.PAGE_NUM[num];
const openPages = await browser.pages(); const oneMinute = 60 * 1000; if (openPages && openPages.length > 1 && retries > 0) { const nextRetries = retries - 1; console.log( '【PuppeteerHelper】当前使用浏览器编号:%s,browser.pages:%s,retries', num, openPages.length, retries ); this.replaceTimer[num] = setTimeout(() => this._replaceBrowserInstance(browser, num, nextRetries), oneMinute); return browser; }
browser.close();
const newBrowser = await this._generateBrowser(num); console.log( '【PuppeteerHelper】当前使用浏览器编号:%s 已打开页面总次数(%s)超过上限,创建新实例,新的wsEndpoint:%s', num, pageNum, this.WSE_LIST[num] );
return newBrowser; }
async _currentBrowser() { const tmp = Math.floor(Math.random() * this.MAX_WSE); const browserWSEndpoint = this.WSE_LIST[tmp]; const pageNum = this.PAGE_NUM[tmp]; console.log( '【PuppeteerHelper】当前使用浏览器编号:%s ,wsEndpoint:%s,过去已打开页面总次数 %s', tmp, browserWSEndpoint, pageNum );
let browser; try { browser = await puppeteer.connect({ browserWSEndpoint });
if (this.PAGE_NUM[tmp] > this.PAGE_COUNT) { browser = this._replaceBrowserInstance(browser, tmp); } } catch (err) { browser = await this._generateBrowser(tmp); console.log( '【PuppeteerHelper】当前使用浏览器编号:%s 连接失败,创建新实例,新的wsEndpoint:%s', tmp, this.WSE_LIST[tmp] ); console.log('【PuppeteerHelper】WSE_LIST:', this.WSE_LIST); }
this.PAGE_NUM[tmp]++;
return browser; }
async _waitRender(page, timeout) { console.log('【_waitRender】开启自定义等待,自定义等待时长:%s ms,(默认30s)', timeout); const renderDoneHandle = await page.waitForFunction('window._renderDone', { polling: 120, timeout: timeout, });
const renderDone = await renderDoneHandle.jsonValue(); if (typeof renderDone === 'object') { console.log(`【_waitRender】加载页面失败: -- ${renderDone.msg}`); await page.close();
throw new Error(`客户端请求重试: -- ${renderDone.msg}`); } else { console.log('【_waitRender】页面加载成功'); } }
async _watermark(page, text) { await page.addScriptTag({ content: htmlGenWaterMark.toString() }); await page.evaluate( (options) => { window.htmlGenWaterMark(options); }, { text: text } ); }
async screenshot({ url, filePath = './example.png', width = 800, height = 600, screenshotType = 'default', selector, altitudeCompensation = 0, headers, openWait, waitTimeout, openWatermark, watermarkText = '水印', }) { console.log('【PuppeteerHelper】开始截图'); const browser = await this._currentBrowser(); const page = await browser.newPage(); try { await page.setViewport({ width, height }); headers && (await page.setExtraHTTPHeaders(headers)); await page.goto(url, { waitUtil: 'networkidle0', }); const documentSize = await page.evaluate(() => { return { width: document.documentElement.clientWidth, height: document.body.clientHeight, }; }); openWait && (await this._waitRender(page, waitTimeout)); openWatermark && (await this._watermark(page, watermarkText));
const picture = await this._capture(page, { screenshotType, filePath, selector, altitudeCompensation });
return picture; } finally { console.log('【PuppeteerHelper】结束截图,关闭当前页面'); await page.close(); } }
async _capture(page, { screenshotType, filePath, selector, altitudeCompensation = 0 }) { switch (screenshotType) { case 'selector': { const element = await page.$(selector); const boundingBox = await element.boundingBox(); const picture = await element.screenshot({ path: filePath, clip: boundingBox }); return picture; } case 'scrollBody': { const { scrollHeight, isBody, width } = await page.evaluate(() => { const clientHeight = document.documentElement.clientHeight; const clientWidth = document.documentElement.clientWidth; const divs = [...document.querySelectorAll('div')]; const len = divs.length; let isBody = false; let boxEl = null; let i = 0; for (; i < len; i++) { const div = divs[i]; if (div.scrollHeight > clientHeight) { boxEl = div; break; } } if (!boxEl && i === len) { boxEl = document.querySelector('body'); isBody = true; } return { scrollHeight: boxEl.scrollHeight, isBody: isBody, width: clientWidth }; }); !isBody && (await page.setViewport({ height: scrollHeight + altitudeCompensation, width: width })); const picture = await page.screenshot({ path: filePath, fullPage: true, }); return picture; } case 'default': default: { const picture = await page.screenshot({ path: filePath, fullPage: true, }); return picture; } } } }
export default PuppeteerHelper.getInstance();
|