2024-03-07 20:43:30 +08:00
|
|
|
|
import Renderer from "../../../lib/renderer/Renderer.js"
|
|
|
|
|
import os from "node:os"
|
|
|
|
|
import lodash from "lodash"
|
|
|
|
|
import puppeteer from "puppeteer"
|
2023-04-18 02:37:38 +08:00
|
|
|
|
// 暂时保留对原config的兼容
|
2024-03-07 20:43:30 +08:00
|
|
|
|
import cfg from "../../../lib/config/config.js"
|
2023-04-16 14:42:45 +08:00
|
|
|
|
|
2023-09-19 03:55:29 +08:00
|
|
|
|
const _path = process.cwd()
|
2023-04-16 14:42:45 +08:00
|
|
|
|
// mac地址
|
2024-03-07 20:43:30 +08:00
|
|
|
|
let mac = ""
|
2023-04-16 14:42:45 +08:00
|
|
|
|
|
2023-09-15 08:15:25 +08:00
|
|
|
|
export default class Puppeteer extends Renderer {
|
2024-03-07 20:43:30 +08:00
|
|
|
|
constructor(config) {
|
2023-09-15 08:15:25 +08:00
|
|
|
|
super({
|
2024-03-07 20:43:30 +08:00
|
|
|
|
id: "puppeteer",
|
|
|
|
|
type: "image",
|
|
|
|
|
render: "screenshot"
|
2023-09-15 08:15:25 +08:00
|
|
|
|
})
|
2023-04-16 14:42:45 +08:00
|
|
|
|
this.browser = false
|
|
|
|
|
this.lock = false
|
|
|
|
|
this.shoting = []
|
|
|
|
|
/** 截图数达到时重启浏览器 避免生成速度越来越慢 */
|
|
|
|
|
this.restartNum = 100
|
|
|
|
|
/** 截图次数 */
|
|
|
|
|
this.renderNum = 0
|
|
|
|
|
this.config = {
|
2024-03-07 20:43:30 +08:00
|
|
|
|
headless: config.headless || "new",
|
|
|
|
|
args: config.args || [
|
|
|
|
|
"--disable-gpu",
|
|
|
|
|
"--disable-setuid-sandbox",
|
|
|
|
|
"--no-sandbox",
|
|
|
|
|
"--no-zygote"
|
|
|
|
|
]
|
2023-04-16 14:42:45 +08:00
|
|
|
|
}
|
2024-03-07 20:43:30 +08:00
|
|
|
|
if (config.chromiumPath || cfg?.bot?.chromium_path)
|
2023-04-16 14:42:45 +08:00
|
|
|
|
/** chromium其他路径 */
|
2023-04-18 02:53:24 +08:00
|
|
|
|
this.config.executablePath = config.chromiumPath || cfg?.bot?.chromium_path
|
2024-03-07 20:43:30 +08:00
|
|
|
|
if (config.puppeteerWS || cfg?.bot?.puppeteer_ws)
|
2023-04-21 13:52:09 +08:00
|
|
|
|
/** chromium其他路径 */
|
|
|
|
|
this.config.wsEndpoint = config.puppeteerWS || cfg?.bot?.puppeteer_ws
|
2023-09-29 04:02:31 +08:00
|
|
|
|
/** puppeteer超时超时时间 */
|
|
|
|
|
this.puppeteerTimeout = config.puppeteerTimeout || cfg?.bot?.puppeteer_timeout || 0
|
2023-04-16 14:42:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 初始化chromium
|
|
|
|
|
*/
|
2024-03-07 20:43:30 +08:00
|
|
|
|
async browserInit() {
|
2023-04-16 14:42:45 +08:00
|
|
|
|
if (this.browser) return this.browser
|
|
|
|
|
if (this.lock) return false
|
|
|
|
|
this.lock = true
|
|
|
|
|
|
2024-03-07 20:43:30 +08:00
|
|
|
|
logger.info("puppeteer Chromium 启动中...")
|
2023-04-16 14:42:45 +08:00
|
|
|
|
|
|
|
|
|
let connectFlag = false
|
|
|
|
|
try {
|
2023-04-21 13:52:09 +08:00
|
|
|
|
// 获取Mac地址
|
|
|
|
|
if (!mac) {
|
|
|
|
|
mac = await this.getMac()
|
|
|
|
|
this.browserMacKey = `Yz:chromium:browserWSEndpoint:${mac}`
|
|
|
|
|
}
|
|
|
|
|
// 是否有browser实例
|
|
|
|
|
const browserUrl = (await redis.get(this.browserMacKey)) || this.config.wsEndpoint
|
|
|
|
|
if (browserUrl) {
|
2024-03-07 20:43:30 +08:00
|
|
|
|
try {
|
|
|
|
|
const browserWSEndpoint = await puppeteer.connect({ browserWSEndpoint: browserUrl })
|
|
|
|
|
// 如果有实例,直接使用
|
|
|
|
|
if (browserWSEndpoint) {
|
|
|
|
|
this.browser = browserWSEndpoint
|
2023-04-21 13:52:09 +08:00
|
|
|
|
connectFlag = true
|
2023-04-16 14:42:45 +08:00
|
|
|
|
}
|
2024-03-07 20:43:30 +08:00
|
|
|
|
logger.info(`puppeteer Chromium 连接成功 ${browserUrl}`)
|
|
|
|
|
} catch (err) {
|
|
|
|
|
await redis.del(this.browserMacKey)
|
2023-04-16 14:42:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-07 20:43:30 +08:00
|
|
|
|
} catch (err) {}
|
2023-04-16 14:42:45 +08:00
|
|
|
|
|
|
|
|
|
if (!this.browser || !connectFlag) {
|
|
|
|
|
// 如果没有实例,初始化puppeteer
|
|
|
|
|
this.browser = await puppeteer.launch(this.config).catch((err, trace) => {
|
2024-03-07 20:43:30 +08:00
|
|
|
|
let errMsg = err.toString() + (trace ? trace.toString() : "")
|
|
|
|
|
if (typeof err == "object") {
|
2023-04-16 14:42:45 +08:00
|
|
|
|
logger.error(JSON.stringify(err))
|
|
|
|
|
} else {
|
|
|
|
|
logger.error(err.toString())
|
2024-03-07 20:43:30 +08:00
|
|
|
|
if (errMsg.includes("Could not find Chromium")) {
|
|
|
|
|
logger.error("没有正确安装 Chromium,可以尝试执行安装命令:node node_modules/puppeteer/install.js")
|
|
|
|
|
} else if (errMsg.includes("cannot open shared object file")) {
|
|
|
|
|
logger.error("没有正确安装 Chromium 运行库")
|
2023-04-16 14:42:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-06 14:48:25 +08:00
|
|
|
|
logger.error(err, trace)
|
2023-04-16 14:42:45 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.lock = false
|
|
|
|
|
|
|
|
|
|
if (!this.browser) {
|
2024-03-07 20:43:30 +08:00
|
|
|
|
logger.error("puppeteer Chromium 启动失败")
|
2023-04-16 14:42:45 +08:00
|
|
|
|
return false
|
|
|
|
|
}
|
2024-03-07 20:43:30 +08:00
|
|
|
|
if (!connectFlag) {
|
|
|
|
|
logger.info(`puppeteer Chromium 启动成功 ${this.browser.wsEndpoint()}`)
|
|
|
|
|
if (this.browserMacKey) {
|
2023-09-06 14:48:25 +08:00
|
|
|
|
// 缓存一下实例30天
|
2023-04-16 14:42:45 +08:00
|
|
|
|
const expireTime = 60 * 60 * 24 * 30
|
|
|
|
|
await redis.set(this.browserMacKey, this.browser.wsEndpoint(), { EX: expireTime })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 监听Chromium实例是否断开 */
|
2024-03-08 09:48:18 +08:00
|
|
|
|
this.browser.on("disconnected", () => this.restart(true))
|
2023-04-16 14:42:45 +08:00
|
|
|
|
|
|
|
|
|
return this.browser
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取Mac地址
|
2024-03-07 20:43:30 +08:00
|
|
|
|
getMac() {
|
|
|
|
|
let mac = "00:00:00:00:00:00"
|
2023-04-16 14:42:45 +08:00
|
|
|
|
try {
|
|
|
|
|
const network = os.networkInterfaces()
|
2023-09-29 04:02:31 +08:00
|
|
|
|
let macFlag = false
|
2023-09-06 14:48:25 +08:00
|
|
|
|
for (const a in network) {
|
|
|
|
|
for (const i of network[a]) {
|
2023-09-29 04:02:31 +08:00
|
|
|
|
if (i.mac && i.mac !== mac) {
|
|
|
|
|
macFlag = true
|
|
|
|
|
mac = i.mac
|
|
|
|
|
break
|
2023-09-06 14:48:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-29 04:02:31 +08:00
|
|
|
|
if (macFlag) {
|
|
|
|
|
break
|
|
|
|
|
}
|
2023-04-16 14:42:45 +08:00
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
}
|
2024-03-07 20:43:30 +08:00
|
|
|
|
mac = mac.replace(/:/g, "")
|
2023-04-16 14:42:45 +08:00
|
|
|
|
return mac
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* `chromium` 截图
|
2023-09-29 04:02:31 +08:00
|
|
|
|
* @param name
|
2023-04-16 14:42:45 +08:00
|
|
|
|
* @param data 模板参数
|
|
|
|
|
* @param data.tplFile 模板路径,必传
|
|
|
|
|
* @param data.saveId 生成html名称,为空name代替
|
|
|
|
|
* @param data.imgType screenshot参数,生成图片类型:jpeg,png
|
|
|
|
|
* @param data.quality screenshot参数,图片质量 0-100,jpeg是可传,默认90
|
|
|
|
|
* @param data.omitBackground screenshot参数,隐藏默认的白色背景,背景透明。默认不透明
|
|
|
|
|
* @param data.path screenshot参数,截图保存路径。截图图片类型将从文件扩展名推断出来。如果是相对路径,则从当前路径解析。如果没有指定路径,图片将不会保存到硬盘。
|
2023-04-18 02:37:38 +08:00
|
|
|
|
* @param data.multiPage 是否分页截图,默认false
|
|
|
|
|
* @param data.multiPageHeight 分页状态下页面高度,默认4000
|
|
|
|
|
* @param data.pageGotoParams 页面goto时的参数
|
2023-09-29 04:02:31 +08:00
|
|
|
|
* @return img 不做segment包裹
|
2023-04-16 14:42:45 +08:00
|
|
|
|
*/
|
2024-03-07 20:43:30 +08:00
|
|
|
|
async screenshot(name, data = {}) {
|
|
|
|
|
if (!await this.browserInit())
|
2023-04-16 14:42:45 +08:00
|
|
|
|
return false
|
2023-04-18 02:37:38 +08:00
|
|
|
|
const pageHeight = data.multiPageHeight || 4000
|
2023-04-16 14:42:45 +08:00
|
|
|
|
|
|
|
|
|
let savePath = this.dealTpl(name, data)
|
2024-03-07 20:43:30 +08:00
|
|
|
|
if (!savePath) return false
|
2023-04-16 14:42:45 +08:00
|
|
|
|
|
2024-03-07 20:43:30 +08:00
|
|
|
|
let buff = ""
|
2023-04-16 14:42:45 +08:00
|
|
|
|
let start = Date.now()
|
|
|
|
|
|
2023-04-18 02:37:38 +08:00
|
|
|
|
let ret = []
|
2023-04-16 14:42:45 +08:00
|
|
|
|
this.shoting.push(name)
|
|
|
|
|
|
2023-09-29 04:02:31 +08:00
|
|
|
|
const puppeteerTimeout = this.puppeteerTimeout
|
|
|
|
|
let overtime
|
|
|
|
|
if (puppeteerTimeout > 0) {
|
|
|
|
|
// TODO 截图超时处理
|
|
|
|
|
overtime = setTimeout(() => {
|
2024-03-07 20:43:30 +08:00
|
|
|
|
if (this.shoting.length) {
|
|
|
|
|
logger.error(`[图片生成][${name}] 截图超时,当前等待队列:${this.shoting.join(",")}`)
|
2023-09-29 04:02:31 +08:00
|
|
|
|
this.restart(true)
|
|
|
|
|
this.shoting = []
|
|
|
|
|
}
|
|
|
|
|
}, puppeteerTimeout)
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-16 14:42:45 +08:00
|
|
|
|
try {
|
|
|
|
|
const page = await this.browser.newPage()
|
2023-04-18 02:37:38 +08:00
|
|
|
|
let pageGotoParams = lodash.extend({ timeout: 120000 }, data.pageGotoParams || {})
|
2024-03-07 20:43:30 +08:00
|
|
|
|
await page.goto(`file://${_path}${lodash.trim(savePath, ".")}`, pageGotoParams)
|
|
|
|
|
let body = await page.$("#container") || await page.$("body")
|
2023-04-16 14:42:45 +08:00
|
|
|
|
|
2023-04-18 02:37:38 +08:00
|
|
|
|
// 计算页面高度
|
|
|
|
|
const boundingBox = await body.boundingBox()
|
|
|
|
|
// 分页数
|
|
|
|
|
let num = 1
|
|
|
|
|
|
2023-04-16 14:42:45 +08:00
|
|
|
|
let randData = {
|
2024-03-07 20:43:30 +08:00
|
|
|
|
type: data.imgType || "jpeg",
|
2023-04-16 14:42:45 +08:00
|
|
|
|
omitBackground: data.omitBackground || false,
|
|
|
|
|
quality: data.quality || 90,
|
2024-03-07 20:43:30 +08:00
|
|
|
|
path: data.path || ""
|
2023-04-16 14:42:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 02:37:38 +08:00
|
|
|
|
if (data.multiPage) {
|
2024-03-07 20:43:30 +08:00
|
|
|
|
randData.type = "jpeg"
|
2023-04-18 02:37:38 +08:00
|
|
|
|
num = Math.round(boundingBox.height / pageHeight) || 1
|
2023-04-16 14:42:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-07 20:43:30 +08:00
|
|
|
|
if (data.imgType === "png") {
|
2023-04-18 02:37:38 +08:00
|
|
|
|
delete randData.quality
|
2023-04-16 14:42:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 02:37:38 +08:00
|
|
|
|
if (!data.multiPage) {
|
|
|
|
|
buff = await body.screenshot(randData)
|
2024-03-07 20:43:30 +08:00
|
|
|
|
this.renderNum++
|
2023-04-18 02:37:38 +08:00
|
|
|
|
/** 计算图片大小 */
|
2024-03-07 20:43:30 +08:00
|
|
|
|
const kb = (buff.length / 1024).toFixed(2) + "KB"
|
2023-04-18 02:37:38 +08:00
|
|
|
|
logger.mark(`[图片生成][${name}][${this.renderNum}次] ${kb} ${logger.green(`${Date.now() - start}ms`)}`)
|
|
|
|
|
ret.push(buff)
|
|
|
|
|
} else {
|
|
|
|
|
// 分片截图
|
|
|
|
|
if (num > 1) {
|
2023-04-16 14:42:45 +08:00
|
|
|
|
await page.setViewport({
|
|
|
|
|
width: boundingBox.width,
|
2023-04-18 02:37:38 +08:00
|
|
|
|
height: pageHeight + 100
|
2023-04-16 14:42:45 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
2023-04-18 02:37:38 +08:00
|
|
|
|
for (let i = 1; i <= num; i++) {
|
|
|
|
|
if (i !== 1 && i === num) {
|
|
|
|
|
await page.setViewport({
|
|
|
|
|
width: boundingBox.width,
|
|
|
|
|
height: parseInt(boundingBox.height) - pageHeight * (num - 1)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
if (i !== 1 && i <= num) {
|
2023-04-18 11:45:23 +08:00
|
|
|
|
await page.evaluate(pageHeight => window.scrollBy(0, pageHeight), pageHeight)
|
2023-04-18 02:37:38 +08:00
|
|
|
|
}
|
|
|
|
|
if (num === 1) {
|
|
|
|
|
buff = await body.screenshot(randData)
|
|
|
|
|
} else {
|
|
|
|
|
buff = await page.screenshot(randData)
|
|
|
|
|
}
|
2023-09-06 14:48:25 +08:00
|
|
|
|
if (num > 2) {
|
2024-03-07 20:43:30 +08:00
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 200))
|
2023-09-06 14:48:25 +08:00
|
|
|
|
}
|
2023-04-18 02:37:38 +08:00
|
|
|
|
this.renderNum++
|
2023-04-16 14:42:45 +08:00
|
|
|
|
|
2023-04-18 02:37:38 +08:00
|
|
|
|
/** 计算图片大小 */
|
2024-03-07 20:43:30 +08:00
|
|
|
|
const kb = (buff.length / 1024).toFixed(2) + "KB"
|
2023-04-18 02:37:38 +08:00
|
|
|
|
logger.mark(`[图片生成][${name}][${i}/${num}] ${kb}`)
|
|
|
|
|
ret.push(buff)
|
2023-04-16 14:42:45 +08:00
|
|
|
|
}
|
2023-04-18 02:37:38 +08:00
|
|
|
|
if (num > 1) {
|
|
|
|
|
logger.mark(`[图片生成][${name}] 处理完成`)
|
2023-04-16 14:42:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-07 20:43:30 +08:00
|
|
|
|
page.close().catch(err => logger.error(err))
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error(`[图片生成][${name}] 图片生成失败`, err)
|
2023-04-16 14:42:45 +08:00
|
|
|
|
/** 关闭浏览器 */
|
2024-03-07 20:43:30 +08:00
|
|
|
|
this.restart(true)
|
|
|
|
|
if (overtime) clearTimeout(overtime)
|
2023-04-18 02:37:38 +08:00
|
|
|
|
ret = []
|
|
|
|
|
return false
|
2023-09-29 04:02:31 +08:00
|
|
|
|
} finally {
|
2024-03-07 20:43:30 +08:00
|
|
|
|
if (overtime) clearTimeout(overtime)
|
2023-04-18 02:37:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.shoting.pop()
|
|
|
|
|
|
|
|
|
|
if (ret.length === 0 || !ret[0]) {
|
2023-09-06 14:48:25 +08:00
|
|
|
|
logger.error(`[图片生成][${name}] 图片生成为空`)
|
2023-04-18 02:37:38 +08:00
|
|
|
|
return false
|
2023-04-16 14:42:45 +08:00
|
|
|
|
}
|
2023-04-18 02:37:38 +08:00
|
|
|
|
|
2024-03-07 20:43:30 +08:00
|
|
|
|
this.restart()
|
2023-04-18 02:37:38 +08:00
|
|
|
|
return data.multiPage ? ret : ret[0]
|
2023-04-16 14:42:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 重启 */
|
2024-03-07 20:43:30 +08:00
|
|
|
|
restart(force = false) {
|
2023-04-16 14:42:45 +08:00
|
|
|
|
/** 截图超过重启数时,自动关闭重启浏览器,避免生成速度越来越慢 */
|
2024-03-08 09:48:18 +08:00
|
|
|
|
if (!this.browser?.close || this.lock) return
|
2024-03-07 20:43:30 +08:00
|
|
|
|
if (!force) if (this.renderNum % this.restartNum !== 0 || this.shoting.length > 0) return
|
2024-03-08 09:48:18 +08:00
|
|
|
|
logger.info(`puppeteer Chromium ${force ? "强制" : ""}关闭重启...`)
|
|
|
|
|
this.stop(this.browser)
|
|
|
|
|
this.browser = false
|
|
|
|
|
return this.browserInit()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async stop(browser) {
|
|
|
|
|
try {
|
|
|
|
|
await browser.close()
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error("puppeteer Chromium 关闭错误", err)
|
|
|
|
|
}
|
2023-04-16 14:42:45 +08:00
|
|
|
|
}
|
2024-03-07 20:43:30 +08:00
|
|
|
|
}
|