Skip to content

Commit 9d8a6d8

Browse files
committed
使用独立puppeteer
1 parent 9a34bf4 commit 9d8a6d8

File tree

4 files changed

+320
-109
lines changed

4 files changed

+320
-109
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ plugin-ill/
4141
/apps/guessName.js
4242
/apps/chart.js
4343
/apps/chart.js
44-
plugin-ill/
44+
plugin-ill/
45+
46+
# 忽略临时文件
47+
/temp/

model/path.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export const _path = process.cwd()
88
export const pluginName = path.basename(path.join(import.meta.url, '../../'))
99
/**插件根目录 */
1010
export const pluginRoot = path.join(_path, 'plugins', pluginName)
11+
/**插件临时文件目录 */
12+
export const tempPath = path.join(pluginRoot, 'temp')
1113
/**插件资源目录 */
1214
export const pluginResources = path.join(pluginRoot, 'resources')
1315

model/picmodle.js

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import Config from '../components/Config.js'
21
import common from '../../../lib/common/common.js'
32
import puppeteer from './puppeteer.js'
4-
5-
3+
import { Data, Version, Plugin_Name, Display_Plugin_Name, Config } from '../components/index.js'
4+
import { _path, pluginResources, imgPath, tempPath } from './path.js'
5+
import fCompute from './fCompute.js'
6+
import fs from 'node:fs'
67
class picmodle {
78

89
constructor() {
@@ -14,17 +15,22 @@ class picmodle {
1415
this.rendering = []
1516
/**
1617
* puppeteer队列
17-
* @param {[puppeteer]} puppeteer
18+
* @type {puppeteer[]}
1819
*/
1920
this.puppeteer = []
2021
this.tot = 0
2122
}
2223

2324
async init() {
25+
/** 清理临时文件 */
26+
fs.rmSync(tempPath, { force: true, recursive: true })
27+
/** 初始化puppeteer实例 */
2428
let num = Config.getUserCfg('config', 'renderNum')
2529
for (let i = 0; i < num; i++) {
26-
this.puppeteer.push(new puppeteer(i))
27-
this.puppeteer[i].init(i)
30+
this.puppeteer.push(new puppeteer({
31+
puppeteerTimeout: Config.getUserCfg('config', 'timeout')
32+
}, i))
33+
this.puppeteer[i].browserInit(i)
2834
this.queue.push(i)
2935
}
3036
}
@@ -170,8 +176,42 @@ class picmodle {
170176
puppeteerNum = this.queue.shift()
171177
this.torender.shift()
172178
try {
179+
180+
let [app, tpl] = path.split('/')
181+
let layoutPath = pluginResources.replace(/\\/g, '/') + `/html/common/layout/`
182+
let resPath = pluginResources.replace(/\\/g, '/') + `/`
183+
184+
185+
Data.createDir(`data/html/${Plugin_Name}/${app}/${tpl}`, 'root')
186+
let data = {
187+
...params,
188+
waitUntil: ['networkidle0', 'load'],
189+
saveId: (params.saveId || params.save_id || tpl) + `${this.id}`,
190+
tplFile: `./plugins/${Plugin_Name}/resources/html/${app}/${tpl}.art`,
191+
pluResPath: resPath,
192+
_res_path: resPath,
193+
_imgPath: imgPath + '/',
194+
_layout_path: layoutPath,
195+
defaultLayout: layoutPath + 'default.art',
196+
elemLayout: layoutPath + 'elem.art',
197+
pageGotoParams: {
198+
waitUntil: ['networkidle2', 'load'],
199+
timeout: Config.getUserCfg('config', 'timeout'),
200+
},
201+
sys: {
202+
scale: `style=transform:scale(${cfg.scale || 1})`,
203+
copyright: `Created By Yunzai-Bot<span class="version">${Version.yunzai}</span> & phi-Plugin<span class="version">${Version.ver}</span>`
204+
},
205+
Version: { ...Version },
206+
_plugin: Display_Plugin_Name,
207+
Math,
208+
fCompute,
209+
}
210+
211+
/**返回图片信息 */
173212
this.rendering.push(id)
174-
ans = await this.puppeteer[puppeteerNum].render(path, params, cfg)
213+
ans = segment.image(await this.puppeteer[puppeteerNum].screenshot(`${Plugin_Name}/${app}/${tpl}`, data))
214+
175215
} catch (err) {
176216
logger.error(`[Phi-Plugin][渲染失败]`, id)
177217
logger.error(err)

0 commit comments

Comments
 (0)