Skip to content

Commit 7c82618

Browse files
committed
Feat: New viewport option to set the width and height of the page
1 parent 4704d4b commit 7c82618

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/api.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ function loaderPageConfig(
206206
rawCrawlPageDetails.push(...transformToCrawlDetails(crawlPages))
207207
} else {
208208
// string | CrawlPageDetailConfig | (string | CrawlPageDetailConfig)[] 处理
209-
const transformRes = transformToCrawlObjects(
209+
const transformRes = transformToCrawlDetails(
210210
rawConfig as
211211
| string
212212
| CrawlPageDetailConfig
@@ -224,18 +224,23 @@ function loaderPageConfig(
224224
crawlPageConfig.crawlPageDetails
225225
)
226226

227-
// 装载单独的配置
228-
if (!isUndefined(crawlPageConfig.cookies)) {
229-
crawlPageConfig.crawlPageDetails.forEach((pageConfig) => {
230-
const { cookies } = pageConfig
227+
// 装载单独配置
228+
const APIHaveCookies = !isUndefined(crawlPageConfig.cookies)
229+
const APIHaveViewport = !isUndefined(crawlPageConfig.viewport)
230+
crawlPageConfig.crawlPageDetails.forEach((detail) => {
231+
// detail > crawlConfig > xCrawl
232+
const { cookies, viewport } = detail
233+
234+
// 1.cookies
235+
if (isUndefined(cookies) && APIHaveCookies) {
236+
detail.cookies = crawlPageConfig.cookies
237+
}
231238

232-
// cookies
233-
if (isUndefined(cookies)) {
234-
// 装载 API Config
235-
pageConfig.cookies = crawlPageConfig.cookies
236-
}
237-
})
238-
}
239+
// 2.viewport
240+
if (isUndefined(viewport) && APIHaveViewport) {
241+
detail.viewport = crawlPageConfig.viewport
242+
}
243+
})
239244

240245
return crawlPageConfig
241246
}
@@ -478,8 +483,8 @@ export function createCrawlPage(xCrawlConfig: LoaderXCrawlConfig) {
478483
const { id, crawlDetailConfig } = controllerConfig
479484
const page = await browser!.newPage()
480485
await page.setViewport({
481-
width: 1280,
482-
height: 1024
486+
width: crawlDetailConfig.viewport?.width ?? 1280,
487+
height: crawlDetailConfig.viewport?.width ?? 1024
483488
})
484489

485490
let response: HTTPResponse | null = null

src/types/api.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IncomingHttpHeaders } from 'node:http'
22
import { Browser, HTTPResponse, Page, Protocol } from 'puppeteer'
33

4-
import { AnyObject, MapTypeObject } from './common'
4+
import { AnyObject } from './common'
55

66
/* Loader Config */
77
type LoaderHasConfig = {
@@ -94,6 +94,10 @@ export interface CrawlPageDetailConfig extends CrawlCommonConfig {
9494
headers?: AnyObject
9595
cookies?: PageCookies
9696
priority?: number
97+
viewport?: {
98+
width?: number
99+
height?: number
100+
}
97101
}
98102

99103
export interface CrawlPageEnhanceConfig extends CrawlCommonConfig {
@@ -102,6 +106,10 @@ export interface CrawlPageEnhanceConfig extends CrawlCommonConfig {
102106

103107
headers?: AnyObject
104108
cookies?: PageCookies
109+
viewport?: {
110+
width?: number
111+
height?: number
112+
}
105113
}
106114

107115
// 2.Crawl data config
@@ -145,7 +153,6 @@ export interface CrawlFileEnhanceConfig extends CrawlCommonConfig {
145153
filePath: string
146154
data: Buffer
147155
}) => Promise<Buffer>
148-
}
149156
}
150157

151158
// 4.Polling config

0 commit comments

Comments
 (0)