From 41bdaf5b55e339976ef9e84462c9a6bcdb645e1f Mon Sep 17 00:00:00 2001 From: touchscale <11134128+touchscale_admin@user.noreply.gitee.com> Date: Wed, 17 May 2023 09:43:19 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D#=E6=98=9F=E9=93=81=E5=85=AC?= =?UTF-8?q?=E5=91=8A/=E8=B5=84=E8=AE=AF/=E6=B4=BB=E5=8A=A8=E6=8A=A5?= =?UTF-8?q?=E9=94=99puppeteer.browserInit=20is=20not=20a=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: touchscale <11134128+touchscale_admin@user.noreply.gitee.com> --- plugins/genshin/model/srmysNews.js | 79 ++---------------------------- 1 file changed, 3 insertions(+), 76 deletions(-) diff --git a/plugins/genshin/model/srmysNews.js b/plugins/genshin/model/srmysNews.js index 934e91b..8027f4c 100644 --- a/plugins/genshin/model/srmysNews.js +++ b/plugins/genshin/model/srmysNews.js @@ -44,86 +44,13 @@ export default class MysNews extends base { const param = await this.newsDetail(postId) - const img = await this.rander(param) + const img = await this.render(param) return await this.replyMsg(img, `崩坏星穹铁道${typeName}:${param.data.post.subject}`) } - async rander (param) { - const pageHeight = 7000 - - await puppeteer.browserInit() - - if (!puppeteer.browser) return false - - const savePath = puppeteer.dealTpl('mysNews', param) - if (!savePath) return false - - const page = await puppeteer.browser.newPage() - try { - await page.goto(`file://${_path}${lodash.trim(savePath, '.')}`, { timeout: 120000 }) - const body = await page.$('#container') || await page.$('body') - const boundingBox = await body.boundingBox() - - const num = Math.round(boundingBox.height / pageHeight) || 1 - - if (num > 1) { - await page.setViewport({ - width: boundingBox.width, - height: pageHeight + 100 - }) - } - - const img = [] - for (let i = 1; i <= num; i++) { - const randData = { - type: 'jpeg', - quality: 90 - } - - if (i != 1 && i == num) { - await page.setViewport({ - width: boundingBox.width, - height: parseInt(boundingBox.height) - pageHeight * (num - 1) - }) - } - - if (i != 1 && i <= num) { - await page.evaluate(() => window.scrollBy(0, 7000)) - } - - let buff - if (num == 1) { - buff = await body.screenshot(randData) - } else { - buff = await page.screenshot(randData) - } - - if (num > 2) await common.sleep(200) - - puppeteer.renderNum++ - /** 计算图片大小 */ - const kb = (buff.length / 1024).toFixed(2) + 'kb' - - logger.mark(`[图片生成][${this.model}][${puppeteer.renderNum}次] ${kb}`) - - img.push(segment.image(buff)) - } - - await page.close().catch((err) => logger.error(err)) - - if (num > 1) { - logger.mark(`[图片生成][${this.model}] 处理完成`) - } - return img - } catch (error) { - logger.error(`图片生成失败:${this.model}:${error}`) - /** 关闭浏览器 */ - if (puppeteer.browser) { - await puppeteer.browser.close().catch((err) => logger.error(err)) - } - puppeteer.browser = false - } + async render (param) { + return await puppeteer.screenshots(this.model, param) } async newsDetail (postId) {