update renderers/puppeteer/lib/puppeteer.js.
Signed-off-by: hbj白夜 <hei_bai_jun@163.com>
This commit is contained in:
parent
cc9a7fecad
commit
feffb2076b
|
@ -1,22 +1,20 @@
|
||||||
import Renderer from '../../../lib/renderer/Renderer.js'
|
import fs from 'node:fs'
|
||||||
import os from 'node:os'
|
import os from 'node:os'
|
||||||
import lodash from 'lodash'
|
import lodash from 'lodash'
|
||||||
|
import template from 'art-template'
|
||||||
|
import chokidar from 'chokidar'
|
||||||
import puppeteer from 'puppeteer'
|
import puppeteer from 'puppeteer'
|
||||||
// 暂时保留对原config的兼容
|
// 暂时保留对原config的兼容
|
||||||
import cfg from '../../../lib/config/config.js'
|
import cfg from '../../../lib/config/config.js'
|
||||||
import { Data } from '#miao'
|
import { Data } from '#miao'
|
||||||
import path from 'path'
|
|
||||||
|
const _path = process.cwd()
|
||||||
|
|
||||||
// mac地址
|
// mac地址
|
||||||
let mac = ''
|
let mac = ''
|
||||||
|
|
||||||
export default class Puppeteer extends Renderer {
|
export default class PuppeteerRenderer {
|
||||||
constructor(config) {
|
constructor(config) {
|
||||||
super({
|
|
||||||
id: 'puppeteer',
|
|
||||||
type: 'image',
|
|
||||||
render: 'screenshot'
|
|
||||||
})
|
|
||||||
this.browser = false
|
this.browser = false
|
||||||
this.lock = false
|
this.lock = false
|
||||||
this.shoting = []
|
this.shoting = []
|
||||||
|
@ -41,6 +39,22 @@ export default class Puppeteer extends Renderer {
|
||||||
/** chromium其他路径 */
|
/** chromium其他路径 */
|
||||||
this.config.wsEndpoint = config.puppeteerWS || cfg?.bot?.puppeteer_ws
|
this.config.wsEndpoint = config.puppeteerWS || cfg?.bot?.puppeteer_ws
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.html = {}
|
||||||
|
this.watcher = {}
|
||||||
|
this.createDir('./temp/html')
|
||||||
|
}
|
||||||
|
|
||||||
|
createDir(dir) {
|
||||||
|
if (!fs.existsSync(dir)) {
|
||||||
|
let dirs = dir.split('/')
|
||||||
|
for (let idx = 1; idx <= dirs.length; idx++) {
|
||||||
|
let temp = dirs.slice(0, idx).join('/')
|
||||||
|
if (!fs.existsSync(temp)) {
|
||||||
|
fs.mkdirSync(temp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -176,7 +190,7 @@ export default class Puppeteer extends Renderer {
|
||||||
try {
|
try {
|
||||||
const page = await this.browser.newPage()
|
const page = await this.browser.newPage()
|
||||||
let pageGotoParams = lodash.extend({ timeout: 120000 }, data.pageGotoParams || {})
|
let pageGotoParams = lodash.extend({ timeout: 120000 }, data.pageGotoParams || {})
|
||||||
await page.goto('file://' + path.resolve(savePath), pageGotoParams)
|
await page.goto(`file://${_path}${lodash.trim(savePath, '.')}`, pageGotoParams)
|
||||||
let body = await page.$('#container') || await page.$('body')
|
let body = await page.$('#container') || await page.$('body')
|
||||||
|
|
||||||
// 计算页面高度
|
// 计算页面高度
|
||||||
|
@ -268,6 +282,51 @@ export default class Puppeteer extends Renderer {
|
||||||
return data.multiPage ? ret : ret[0]
|
return data.multiPage ? ret : ret[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 模板 */
|
||||||
|
dealTpl(name, data) {
|
||||||
|
let { tplFile, saveId = name } = data
|
||||||
|
let savePath = `./temp/html/${name}/${saveId}.html`
|
||||||
|
|
||||||
|
/** 读取html模板 */
|
||||||
|
if (!this.html[tplFile]) {
|
||||||
|
this.createDir(`./temp/html/${name}`)
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.html[tplFile] = fs.readFileSync(tplFile, 'utf8')
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`加载html错误:${tplFile}`)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
this.watch(tplFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
data.resPath = `${_path}/resources/`
|
||||||
|
|
||||||
|
/** 替换模板 */
|
||||||
|
let tmpHtml = template.render(this.html[tplFile], data)
|
||||||
|
|
||||||
|
/** 保存模板 */
|
||||||
|
fs.writeFileSync(savePath, tmpHtml)
|
||||||
|
|
||||||
|
logger.debug(`[图片生成][使用模板] ${savePath}`)
|
||||||
|
|
||||||
|
return savePath
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 监听配置文件 */
|
||||||
|
watch(tplFile) {
|
||||||
|
if (this.watcher[tplFile]) return
|
||||||
|
|
||||||
|
const watcher = chokidar.watch(tplFile)
|
||||||
|
watcher.on('change', path => {
|
||||||
|
delete this.html[tplFile]
|
||||||
|
logger.mark(`[修改html模板] ${tplFile}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
this.watcher[tplFile] = watcher
|
||||||
|
}
|
||||||
|
|
||||||
/** 重启 */
|
/** 重启 */
|
||||||
restart() {
|
restart() {
|
||||||
/** 截图超过重启数时,自动关闭重启浏览器,避免生成速度越来越慢 */
|
/** 截图超过重启数时,自动关闭重启浏览器,避免生成速度越来越慢 */
|
||||||
|
|
Loading…
Reference in New Issue