Skip to content

Commit

Permalink
细节优化
Browse files Browse the repository at this point in the history
  • Loading branch information
TimeRainStarSky committed Jun 2, 2024
1 parent cb2b121 commit ed4d36b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
18 changes: 8 additions & 10 deletions lib/puppeteer/puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ import Renderer from "../renderer/loader.js"
*/
const renderer = Renderer.getRenderer()
renderer.screenshot = async (name, data) => {
let img = await renderer.render(name, data)
return img ? segment.image(img) : img
const img = await renderer.render(name, data)
return img ? segment.image(img) : img
}
renderer.screenshots = async (name, data) => {
data.multiPage = true
let imgs = await renderer.render(name, data) || []
let ret = []
for (let img of imgs) {
ret.push(img ? segment.image(img) : img)
}
return ret.length > 0 ? ret : false
data.multiPage = true
const imgs = await renderer.render(name, data) || []
const ret = []
for (const img of imgs)
ret.push(img ? segment.image(img) : img)
return ret.length > 0 ? ret : false
}

export default renderer
5 changes: 5 additions & 0 deletions renderers/puppeteer/config_default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ args:

# puppeteer截图超时时间
puppeteerTimeout:

# 页面goto时的参数
pageGotoParams:
timeout: 120000
waitUntil: networkidle2
19 changes: 11 additions & 8 deletions renderers/puppeteer/lib/puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export default class Puppeteer extends Renderer {
this.config.wsEndpoint = config.puppeteerWS || cfg?.bot?.puppeteer_ws
/** puppeteer超时超时时间 */
this.puppeteerTimeout = config.puppeteerTimeout || cfg?.bot?.puppeteer_timeout || 0
this.pageGotoParams = config.pageGotoParams || {
timeout: 120000,
waitUntil: "networkidle2",
}
}

/**
Expand Down Expand Up @@ -159,11 +163,11 @@ export default class Puppeteer extends Renderer {
return false
const pageHeight = data.multiPageHeight || 4000

let savePath = this.dealTpl(name, data)
const savePath = this.dealTpl(name, data)
if (!savePath) return false

let buff = ""
let start = Date.now()
const start = Date.now()

let ret = []
this.shoting.push(name)
Expand All @@ -183,30 +187,29 @@ export default class Puppeteer extends Renderer {

try {
const page = await this.browser.newPage()
let pageGotoParams = lodash.extend({ timeout: 120000 }, data.pageGotoParams || {})
const pageGotoParams = lodash.extend(this.pageGotoParams, data.pageGotoParams || {})
await page.goto(`file://${_path}${lodash.trim(savePath, ".")}`, pageGotoParams)
let body = await page.$("#container") || await page.$("body")
const body = await page.$("#container") || await page.$("body")

// 计算页面高度
const boundingBox = await body.boundingBox()
// 分页数
let num = 1

let randData = {
const randData = {
type: data.imgType || "jpeg",
omitBackground: data.omitBackground || false,
quality: data.quality || 90,
path: data.path || ""
path: data.path || "",
}

if (data.multiPage) {
randData.type = "jpeg"
num = Math.round(boundingBox.height / pageHeight) || 1
}

if (data.imgType === "png") {
if (data.imgType === "png")
delete randData.quality
}

if (!data.multiPage) {
buff = await body.screenshot(randData)
Expand Down

0 comments on commit ed4d36b

Please sign in to comment.