Skip to content

Commit 8f75be8

Browse files
authored
Merge pull request #34 from dev-protocol/impr
Improvement of capture accuracy
2 parents 236ff0b + 7d458db commit 8f75be8

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/pages/api/generate.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { type APIRoute } from 'astro'
33
import chromium from '@sparticuz/chromium-min'
44
import { Chromium } from '../../libs/chromium.ts'
5+
import type { PuppeteerLifeCycleEvent } from 'puppeteer-core'
56

67
const exePath =
78
process.platform === 'win32'
@@ -18,18 +19,31 @@ const Localoptions = {
1819
executablePath: exePath,
1920
headless: true,
2021
}
22+
await chromium.font(
23+
'https://raw.githack.com/dev-protocol/stackroom/main/fonts/IBM_Plex_Sans_JP/IBMPlexSansJP-Medium.ttf',
24+
)
25+
await chromium.font(
26+
'https://raw.githack.com/googlei18n/noto-emoji/master/fonts/NotoColorEmoji.ttf',
27+
)
2128
const serverOptions = {
2229
args: chromium.args,
2330
executablePath: await chromium.executablePath(chromiumPack),
2431
headless: true,
2532
}
33+
const puppeteerLifeCycleEvents = [
34+
'load',
35+
'domcontentloaded',
36+
'networkidle0',
37+
'networkidle2',
38+
] satisfies PuppeteerLifeCycleEvent[]
2639

2740
export const GET: APIRoute = async ({ url }) => {
28-
const { isDev, height, width, cacheControl } = {
41+
const { isDev, height, width, cacheControl, gotoThenWaitUntil } = {
2942
isDev: url.searchParams.get('dev') === 'true',
3043
height: url.searchParams.get('h'),
3144
width: url.searchParams.get('w'),
3245
cacheControl: url.searchParams.get('cache-control'),
46+
gotoThenWaitUntil: url.searchParams.get('goto-then-waituntil'),
3347
}
3448
const options = isDev ? Localoptions : serverOptions
3549

@@ -56,8 +70,21 @@ export const GET: APIRoute = async ({ url }) => {
5670
deviceScaleFactor: 1,
5771
})
5872

73+
await page.setRequestInterception(true)
74+
75+
// eslint-disable-next-line functional/no-return-void
76+
page.on('request', (req) => {
77+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
78+
req.url().includes('capture.clubs.place') ? req.abort() : req.continue()
79+
})
80+
5981
// tell the page to visit the url
60-
await page.goto(targetUrl)
82+
const waitUntil: PuppeteerLifeCycleEvent =
83+
gotoThenWaitUntil &&
84+
puppeteerLifeCycleEvents.some((x) => x === gotoThenWaitUntil)
85+
? (gotoThenWaitUntil as PuppeteerLifeCycleEvent)
86+
: ('networkidle2' as PuppeteerLifeCycleEvent)
87+
await page.goto(targetUrl, { waitUntil, timeout: 0 })
6188

6289
// take a screenshot
6390
const file = await page.screenshot({

0 commit comments

Comments
 (0)