Skip to content

Commit fa1bbf3

Browse files
committed
fix: prefer using system chrome install of playwright install
Fixes #7
1 parent 559a469 commit fa1bbf3

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

packages/crawl/src/cli.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { dirname, join, resolve } from 'pathe'
77
import { withHttps } from 'ufo'
88
import { crawlAndGenerate } from './crawl.ts'
99
import { parseUrlPattern, validateGlobPattern } from './glob-utils.ts'
10-
import { ensurePlaywrightInstalled } from './playwright-utils.ts'
10+
import { ensurePlaywrightInstalled, isUseChromeSupported } from './playwright-utils.ts'
1111

1212
// Read version from package.json
1313
const __dirname = dirname(fileURLToPath(import.meta.url))
@@ -464,10 +464,19 @@ async function main() {
464464

465465
// Check playwright installation if needed
466466
if (options.driver === 'playwright') {
467-
const playwrightInstalled = await ensurePlaywrightInstalled()
468-
if (!playwrightInstalled) {
469-
p.log.error('Cannot proceed without Playwright. Please install it manually or use the HTTP driver instead.')
470-
process.exit(1)
467+
// Check Chrome support and configure if available
468+
const chromeSupported = await isUseChromeSupported()
469+
if (chromeSupported) {
470+
options.useChrome = true
471+
p.log.info('System Chrome detected and enabled.')
472+
}
473+
else {
474+
const playwrightInstalled = await ensurePlaywrightInstalled()
475+
if (!playwrightInstalled) {
476+
p.log.error('Cannot proceed without Playwright. Please install it manually or use the HTTP driver instead.')
477+
process.exit(1)
478+
}
479+
p.log.info('Using global playwright instance.')
471480
}
472481
}
473482

packages/crawl/src/crawl.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export async function crawlAndGenerate(options: CrawlOptions, onProgress?: (prog
6363
generateIndividualMd = true,
6464
origin,
6565
driver = 'http',
66+
useChrome,
6667
followLinks = false,
6768
maxDepth = 1,
6869
globPatterns = [],
@@ -469,7 +470,17 @@ export async function crawlAndGenerate(options: CrawlOptions, onProgress?: (prog
469470

470471
if (driver === 'playwright') {
471472
// PlaywrightCrawler - installation check should happen in CLI layer
472-
crawler = new PlaywrightCrawler(crawlerOptions as PlaywrightCrawlerOptions)
473+
const playwrightOptions = crawlerOptions as PlaywrightCrawlerOptions
474+
475+
// Add useChrome option if specified
476+
if (useChrome) {
477+
playwrightOptions.launchContext = {
478+
...playwrightOptions.launchContext,
479+
useChrome,
480+
}
481+
}
482+
483+
crawler = new PlaywrightCrawler(playwrightOptions)
473484
}
474485
else {
475486
crawler = new HttpCrawler(crawlerOptions as HttpCrawlerOptions)

packages/crawl/src/playwright-utils.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as p from '@clack/prompts'
2+
import { PlaywrightCrawler } from 'crawlee'
23
import { addDependency } from 'nypm'
34

45
export async function checkPlaywrightInstallation(): Promise<boolean> {
@@ -56,3 +57,30 @@ export async function ensurePlaywrightInstalled(): Promise<boolean> {
5657

5758
return true
5859
}
60+
61+
export async function isUseChromeSupported(): Promise<boolean> {
62+
try {
63+
// Create a minimal crawler instance with useChrome option
64+
const crawler = new PlaywrightCrawler({
65+
launchContext: {
66+
useChrome: true,
67+
},
68+
requestHandler: async () => {
69+
// Minimal no-op handler
70+
},
71+
maxRequestsPerCrawl: 1,
72+
})
73+
const page = await crawler.browserPool.newPage()
74+
await page.evaluate(() => {
75+
// Simple check to ensure page is functional
76+
return window.navigator.userAgent
77+
})
78+
await page.close()
79+
await crawler.browserPool.closeAllBrowsers()
80+
crawler.stop()
81+
return true
82+
}
83+
catch {
84+
}
85+
return false
86+
}

packages/crawl/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface CrawlOptions {
88
origin?: string
99
chunkSize?: number
1010
driver?: 'http' | 'playwright'
11+
useChrome?: boolean
1112
followLinks?: boolean
1213
maxDepth?: number
1314
globPatterns?: ParsedUrlPattern[]

0 commit comments

Comments
 (0)