Skip to content

Commit

Permalink
optimize: render
Browse files Browse the repository at this point in the history
  • Loading branch information
snowtafir committed Sep 9, 2024
1 parent 8ea369e commit f666cb3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 2.0.3
* 优化截图、静态资源引入方式
* 优化获取完整文章动态
* fix addBiliSub
* 优化提示信息
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yz-yuki-plugin",
"version": "2.0.3-5",
"version": "2.0.3-6",
"description": "优纪插件,yunzaijs 关于 微博推送、B站推送 等功能的拓展插件",
"author": "snowtafir",
"type": "module",
Expand Down Expand Up @@ -30,13 +30,13 @@
"md5": "^2.3.0",
"moment": "^2.30.1",
"node-fetch": "^3.3.2",
"puppeteer": "^22.15.0",
"puppeteer": "^23.3.0",
"qrcode": "^1.5.4",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-puppeteer": "^1.0.3",
"react-puppeteer": "^1.0.7-rc.0",
"redis": "^4.7.0",
"yaml": "^2.5.0",
"yaml": "^2.5.1",
"yarn": "^1.19.1"
},
"devDependencies": {
Expand Down Expand Up @@ -72,11 +72,11 @@
"node-fetch": "^3.3.2",
"nodemon": "^3.1.4",
"prettier": "^3.3.3",
"puppeteer": "^22.15.0",
"puppeteer": "^23.3.0",
"qrcode": "^1.5.4",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-puppeteer": "1.0.3",
"react-puppeteer": "^1.0.7-rc.0",
"redis": "^4.7.0",
"rollup": "^4.20.0",
"rollup-plugin-auto-external": "^2.0.0",
Expand All @@ -87,8 +87,8 @@
"ts-node": "^10.9.2",
"tsx": "^4.19.0",
"typescript": "^5.5.4",
"yunzai": "^1.1.3",
"yunzai-mys": "^1.0.5"
"yunzai": "^1.1.4",
"yunzai-mys": "^1.0.6"
},
"files": [
"public",
Expand Down
22 changes: 10 additions & 12 deletions src/utils/image.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import React from 'react';
import { Puppeteer, Picture, Component, ComponentCreateOpsionType } from 'react-puppeteer';
import { Picture, ComponentCreateOpsionType } from 'react-puppeteer';
import { YukiPuppeteerRender, ScreenshotOptions } from '@/utils/puppeteer.render';
import * as ReactPages from '@/components/index'

// 初始化 组件渲染对象
const com = new Component()

const yukiPuppeteerRender = new YukiPuppeteerRender();

export class Image extends Picture {
private yukiPuppeteerRender: YukiPuppeteerRender;
/**
* 初始化运行Puppeteer
* 整合截图方法
*/
constructor() {
// 继承实例
super()
// start
this.Pup = new Puppeteer()
this.Pup.start()
// 组件渲染对象
this.Com;
// start puppeteer
this.Pup.start();
this.yukiPuppeteerRender = new YukiPuppeteerRender(this.Pup);
}

/**
Expand All @@ -36,8 +34,8 @@ export class Image extends Picture {
ComponentCreateOpsion?: ComponentCreateOpsionType,
): Promise<false | { img: Buffer[]; }> {
const Page = ReactPages[page]
return yukiPuppeteerRender.yukiScreenshot(
com.compile({
return this.yukiPuppeteerRender.yukiScreenshot(
this.Com.compile({
join_dir: page,
html_name: `${uid}.html`,
html_body: <Page {...props} />,
Expand Down
11 changes: 7 additions & 4 deletions src/utils/puppeteer.render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ export type ScreenshotOptions = {
saveHtmlfile?: boolean
}

export class YukiPuppeteerRender extends Puppeteer {

export class YukiPuppeteerRender {
private puppeteerInstance: Puppeteer;
constructor(puppeteerInstance: Puppeteer) {
this.puppeteerInstance = puppeteerInstance;
}
/**
* 截图并返回buffer
* @param htmlPath 绝对路径
Expand All @@ -40,12 +43,12 @@ export class YukiPuppeteerRender extends Puppeteer {
* @returns {false | {img: buffer[]}}
*/
async yukiScreenshot(htmlPath: string, Options?: ScreenshotOptions): Promise<false | { img: Buffer[]; }> {
if (!(await this.isStart())) return false
if (!(await this.puppeteerInstance.isStart())) return false
let name = Options?.modelName ?? 'yuki-plugin';
let pageHeight = Options?.pageSplitHeight ?? 8000; // 分割图片高度,默认 8000

try {
const page = await this.browser?.newPage().catch(err => {
const page = await this.puppeteerInstance.browser?.newPage().catch(err => {
logger.error(err)
})
if (!page) return false
Expand Down

0 comments on commit f666cb3

Please sign in to comment.