🚑 保留老版puppeteer方法
This commit is contained in:
@@ -1,16 +1,20 @@
|
||||
import fs from 'fs'
|
||||
import _ from 'lodash'
|
||||
import puppeteer, { KnownDevices } from 'puppeteer'
|
||||
import puppeteer from 'puppeteer'
|
||||
import pet from '../../../../lib/puppeteer/puppeteer.js'
|
||||
import { Data, Version, Plugin_Name, Config } from '../../components/index.js'
|
||||
|
||||
let devices = puppeteer.devices
|
||||
if (!devices) {
|
||||
devices = (await import('puppeteer')).KnownDevices
|
||||
}
|
||||
const _path = process.cwd()
|
||||
export default new class newPuppeteer {
|
||||
export default new (class newPuppeteer {
|
||||
constructor () {
|
||||
this.devices = {
|
||||
QQTheme: {
|
||||
name: 'QQTheme',
|
||||
userAgent: 'Mozilla/5.0 (Linux; Android 12; M2012K11AC Build/SKQ1.220303.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/98.0.4758.102 MQQBrowser/6.2 TBS/046317 Mobile Safari/537.36 V1_AND_SQ_8.9.10_3296_YYB_D A_8091000 QQ/8.9.10.9145 NetType/WIFI WebP/0.3.0 Pixel/1080 StatusBarHeight/80 SimpleUISwitch/0 QQTheme/1000 InMagicWin/0 StudyMode/0 CurrentMode/0 CurrentFontScale/1.0 GlobalDensityScale/0.98181814 AppId/537135947',
|
||||
userAgent:
|
||||
'Mozilla/5.0 (Linux; Android 12; M2012K11AC Build/SKQ1.220303.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/98.0.4758.102 MQQBrowser/6.2 TBS/046317 Mobile Safari/537.36 V1_AND_SQ_8.9.10_3296_YYB_D A_8091000 QQ/8.9.10.9145 NetType/WIFI WebP/0.3.0 Pixel/1080 StatusBarHeight/80 SimpleUISwitch/0 QQTheme/1000 InMagicWin/0 StudyMode/0 CurrentMode/0 CurrentFontScale/1.0 GlobalDensityScale/0.98181814 AppId/537135947',
|
||||
viewport: {
|
||||
width: 375,
|
||||
height: 667,
|
||||
@@ -20,30 +24,28 @@ export default new class newPuppeteer {
|
||||
isLandscape: false
|
||||
}
|
||||
},
|
||||
...KnownDevices
|
||||
...devices
|
||||
}
|
||||
this.browser = false
|
||||
this.config = {
|
||||
headless: 'chrome',
|
||||
args: [
|
||||
'--no-sandbox'
|
||||
]
|
||||
args: ['--no-sandbox']
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 返回网页截图
|
||||
* @param {Object} Methods 参数对象
|
||||
* @param {String} Methods.url 网页链接
|
||||
* @param {Object} Methods.headers 请求头
|
||||
* @param {Object} Methods.setViewport 设置宽度和高度和缩放
|
||||
* @param {Boolean} Methods.font 是否修改字体
|
||||
* @param {Object} Methods.cookie 设置cookie
|
||||
* @param {Boolean} Methods.fullPage 是否截取完整网页
|
||||
* @param {Object} Methods.emulate 模拟设备
|
||||
* @param {Object} Methods.click 点击事件
|
||||
* @return {img} 可直接发送的构造图片
|
||||
*/
|
||||
* @description: 返回网页截图
|
||||
* @param {Object} Methods 参数对象
|
||||
* @param {String} Methods.url 网页链接
|
||||
* @param {Object} Methods.headers 请求头
|
||||
* @param {Object} Methods.setViewport 设置宽度和高度和缩放
|
||||
* @param {Boolean} Methods.font 是否修改字体
|
||||
* @param {Object} Methods.cookie 设置cookie
|
||||
* @param {Boolean} Methods.fullPage 是否截取完整网页
|
||||
* @param {Object} Methods.emulate 模拟设备
|
||||
* @param {Object} Methods.click 点击事件
|
||||
* @return {img} 可直接发送的构造图片
|
||||
*/
|
||||
async Webpage ({
|
||||
url,
|
||||
headers = false,
|
||||
@@ -54,7 +56,7 @@ export default new class newPuppeteer {
|
||||
emulate = false,
|
||||
click = false
|
||||
}) {
|
||||
if (!await pet.browserInit()) {
|
||||
if (!(await pet.browserInit())) {
|
||||
return false
|
||||
}
|
||||
let buff = ''
|
||||
@@ -74,9 +76,19 @@ export default new class newPuppeteer {
|
||||
// 打卡新标签页
|
||||
await page.goto(url, { timeout: 1000 * 60, waitUntil: 'networkidle0' })
|
||||
// 设置字体
|
||||
if (font) await page.addStyleTag({ content: '* {font-family: "汉仪文黑-65W","雅痞-简","圆体-简","PingFang SC","微软雅黑", sans-serif !important;}' })
|
||||
if (font) {
|
||||
await page.addStyleTag({
|
||||
content:
|
||||
'* {font-family: "汉仪文黑-65W","雅痞-简","圆体-简","PingFang SC","微软雅黑", sans-serif !important;}'
|
||||
})
|
||||
}
|
||||
// 点击事件
|
||||
if (click) for (let i of click) { await page.click(i.selector); await page.waitForTimeout(i.time) }
|
||||
if (click) {
|
||||
for (let i of click) {
|
||||
await page.click(i.selector)
|
||||
await page.waitForTimeout(i.time)
|
||||
}
|
||||
}
|
||||
|
||||
buff = await page.screenshot({
|
||||
// path: './paper.jpeg',
|
||||
@@ -107,7 +119,11 @@ export default new class newPuppeteer {
|
||||
/** 计算图片大小 */
|
||||
let kb = (buff.length / 1024).toFixed(2) + 'kb'
|
||||
|
||||
logger.mark(`[网页截图][${name}][${pet.renderNum}次] ${kb} ${logger.green(`${Date.now() - start}ms`)}`)
|
||||
logger.mark(
|
||||
`[网页截图][${name}][${pet.renderNum}次] ${kb} ${logger.green(
|
||||
`${Date.now() - start}ms`
|
||||
)}`
|
||||
)
|
||||
|
||||
pet.restart()
|
||||
return segment.image(buff)
|
||||
@@ -119,7 +135,9 @@ export default new class newPuppeteer {
|
||||
this.browser = await puppeteer.launch(this.config).catch((err) => {
|
||||
logger.error(err.toString())
|
||||
if (String(err).includes('correct Chromium')) {
|
||||
logger.error('没有正确安装Chromium,可以尝试执行安装命令:node ./node_modules/puppeteer/install.js')
|
||||
logger.error(
|
||||
'没有正确安装Chromium,可以尝试执行安装命令:node ./node_modules/puppeteer/install.js'
|
||||
)
|
||||
}
|
||||
})
|
||||
if (!this.browser) {
|
||||
@@ -137,14 +155,14 @@ export default new class newPuppeteer {
|
||||
}
|
||||
|
||||
async get (url, waitSelector) {
|
||||
if (!await this.launch()) {
|
||||
if (!(await this.launch())) {
|
||||
return false
|
||||
}
|
||||
const page = await this.browser.newPage()
|
||||
try {
|
||||
logger.debug('Puppeteer get', url)
|
||||
await page.goto(url)
|
||||
await page.waitForSelector(waitSelector).catch(e => {
|
||||
await page.waitForSelector(waitSelector).catch((e) => {
|
||||
console.error(`Puppeteer get "${url}" wait "${waitSelector}" error`)
|
||||
console.error(e)
|
||||
})
|
||||
@@ -162,15 +180,16 @@ export default new class newPuppeteer {
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 渲染HTML
|
||||
* @param {String} path 文件路径
|
||||
* @param {Object} params 参数
|
||||
* @param {Object} cfg
|
||||
*/
|
||||
* @description: 渲染HTML
|
||||
* @param {String} path 文件路径
|
||||
* @param {Object} params 参数
|
||||
* @param {Object} cfg
|
||||
*/
|
||||
async render (path, params, cfg) {
|
||||
let [app, tpl] = path.split('/')
|
||||
let { e } = cfg
|
||||
let layoutPath = process.cwd() + `/plugins/${Plugin_Name}/resources/common/layout/`
|
||||
let layoutPath =
|
||||
process.cwd() + `/plugins/${Plugin_Name}/resources/common/layout/`
|
||||
let resPath = `../../../../../plugins/${Plugin_Name}/resources/`
|
||||
Data.createDir(`data/html/${Plugin_Name}/${app}/${tpl}`, 'root')
|
||||
let data = {
|
||||
@@ -181,14 +200,17 @@ export default new class newPuppeteer {
|
||||
pluResPath: resPath,
|
||||
_res_path: resPath,
|
||||
_layout_path: layoutPath,
|
||||
_tpl_path: process.cwd() + `/plugins/${Plugin_Name}/resources/common/tpl/`,
|
||||
_tpl_path:
|
||||
process.cwd() + `/plugins/${Plugin_Name}/resources/common/tpl/`,
|
||||
defaultLayout: layoutPath + 'default.html',
|
||||
elemLayout: layoutPath + 'elem.html',
|
||||
pageGotoParams: {
|
||||
waitUntil: 'networkidle0'
|
||||
},
|
||||
sys: {
|
||||
scale: `style=transform:scale(${Config.Notice.renderScale / 100 || cfg.scale || 1})`,
|
||||
scale: `style=transform:scale(${
|
||||
Config.Notice.renderScale / 100 || cfg.scale || 1
|
||||
})`,
|
||||
copyright: `Created By Yunzai-Bot<span class="version">${Version.yunzai}</span> & yenai-Plugin<span class="version">${Version.ver}</span>`
|
||||
},
|
||||
quality: 100
|
||||
@@ -211,4 +233,4 @@ export default new class newPuppeteer {
|
||||
}
|
||||
return cfg.retMsgId ? ret : true
|
||||
}
|
||||
}()
|
||||
})()
|
||||
|
||||
Reference in New Issue
Block a user