From 52cf19d0db084154c53190e727b2ad829708e35a Mon Sep 17 00:00:00 2001 From: Ayakura Yuki Date: Wed, 6 Sep 2023 10:51:06 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20.puppeteerrc.cjs=20?= =?UTF-8?q?=E4=BB=A5=E6=94=AF=E6=8C=81=E5=AE=9A=E4=BD=8D=20macOS=20?= =?UTF-8?q?=E4=B8=8A=E7=9A=84=20Chrome=20=E6=88=96=20Microsoft=20Edge=20?= =?UTF-8?q?=E6=B5=8F=E8=A7=88=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .puppeteerrc.cjs | 77 +++++++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 27 deletions(-) diff --git a/.puppeteerrc.cjs b/.puppeteerrc.cjs index 2f3a7b5..ffefdc9 100644 --- a/.puppeteerrc.cjs +++ b/.puppeteerrc.cjs @@ -1,39 +1,62 @@ -const os = require("os") -const { existsSync, realpathSync } = require("fs") -const { execSync } = require("child_process") +const os = require('os') +const {existsSync, realpathSync} = require('fs') +const {execSync} = require('child_process') const arch = os.arch() let skipDownload = false let executablePath -if (process.platform == "linux" || process.platform == "android") +// linux / android +if (process.platform === 'linux' || process.platform === 'android') { for (const item of [ - "chromium", - "chromium-browser", - "chrome", - "chrome-browser", - ]) try { - const chromiumPath = execSync(`command -v ${item}`).toString().trim() - if (chromiumPath && existsSync(chromiumPath)) { - executablePath = realpathSync(chromiumPath) - break + 'chromium', + 'chromium-browser', + 'chrome', + 'chrome-browser', + ]) { + try { + const chromiumPath = execSync(`command -v ${item}`).toString().trim() + if (chromiumPath && existsSync(chromiumPath)) { + executablePath = realpathSync(chromiumPath) + break + } + } catch (err) { } - } catch (err) {} - -if (!executablePath) for (const item of [ - "C:/Program Files/Google/Chrome/Application/chrome.exe", - "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe", - "/usr/bin/chromium", - "/usr/bin/chromium-browser", - "/snap/bin/chromium", -]) if (existsSync(item)) { - executablePath = item - break + } } -if (executablePath || arch == "arm64" || arch == "aarch64") { - (typeof logger == "object" ? logger : console).info(`[Chromium] ${executablePath}`) +// macOS +if (process.platform === 'darwin') { + for (const item of [ + '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', + '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge', + ]) { + if (existsSync(item)) { + executablePath = item + break + } + } +} + +// windows 或其他 +if (!executablePath) { + for (const item of [ + 'C:/Program Files/Google/Chrome/Application/chrome.exe', + 'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe', + '/usr/bin/chromium', + '/usr/bin/chromium-browser', + '/snap/bin/chromium', + ]) { + if (existsSync(item)) { + executablePath = item + break + } + } +} + +if (executablePath || arch === 'arm64' || arch === 'aarch64') { + (typeof logger == 'object' ? logger : console).info(`[Chromium] ${executablePath}`) skipDownload = true } -module.exports = { skipDownload, executablePath } \ No newline at end of file +module.exports = {skipDownload, executablePath} From 730b58ed08a766252d682583949c6b778b9131a4 Mon Sep 17 00:00:00 2001 From: Ayakura Yuki Date: Wed, 6 Sep 2023 11:37:42 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=20.editorconfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .editorconfig | 23 +++++++++++++++-------- .puppeteerrc.cjs | 6 +++--- lib/common/common.js | 24 ++++++++++++++---------- lib/puppeteer/puppeteer.js | 1 - lib/renderer/Renderer.js | 1 - lib/tools/command.js | 3 +-- 6 files changed, 33 insertions(+), 25 deletions(-) diff --git a/.editorconfig b/.editorconfig index 7c19a87..1b6e051 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,7 +1,6 @@ root = true [*] -# EditorConfig 标准配置项 charset = utf-8 indent_size = 2 indent_style = space @@ -10,18 +9,26 @@ insert_final_newline = true end_of_line = lf trim_trailing_whitespace = true max_line_length = off - -# IntelliJ 系列配置项 ij_any_space_before_colon = true ij_any_space_after_colon = true ij_any_keep_indents_on_empty_lines = false + +[{*.cjs,*.js}] +ij_javascript_align_imports = false ij_javascript_use_semicolon_after_statement = false ij_javascript_use_double_quotes = false ij_javascript_enforce_trailing_comma = keep -ij_typescript_use_semicolon_after_statement = false -ij_typescript_use_double_quotes = false -ij_typescript_enforce_trailing_comma = keep +ij_javascript_space_before_method_parentheses = true +ij_javascript_spaces_within_object_literal_braces = true +ij_javascript_indent_chained_calls = true -[*.json] -ij_any_space_before_colon = false +[{*.json,*.json5}] ij_json_keep_trailing_comma = false +ij_json_keep_blank_lines_in_code = 0 +ij_json_keep_indents_on_empty_lines = false +ij_json_space_after_colon = true +ij_json_space_after_comma = true +ij_json_space_before_colon = false +ij_json_space_before_comma = false +ij_json_spaces_within_braces = false +ij_json_spaces_within_brackets = false diff --git a/.puppeteerrc.cjs b/.puppeteerrc.cjs index ffefdc9..7ea196e 100644 --- a/.puppeteerrc.cjs +++ b/.puppeteerrc.cjs @@ -1,6 +1,6 @@ const os = require('os') -const {existsSync, realpathSync} = require('fs') -const {execSync} = require('child_process') +const { existsSync, realpathSync } = require('fs') +const { execSync } = require('child_process') const arch = os.arch() let skipDownload = false @@ -59,4 +59,4 @@ if (executablePath || arch === 'arm64' || arch === 'aarch64') { skipDownload = true } -module.exports = {skipDownload, executablePath} +module.exports = { skipDownload, executablePath } diff --git a/lib/common/common.js b/lib/common/common.js index 4ad440c..cf82dd7 100644 --- a/lib/common/common.js +++ b/lib/common/common.js @@ -6,10 +6,10 @@ import path from 'node:path' /** * 发送私聊消息,仅给好友发送 - * @param user_id qq号 + * @param userId qq号 * @param msg 消息 */ -async function relpyPrivate(userId, msg) { +async function relpyPrivate (userId, msg) { userId = Number(userId) let friend = Bot.fl.get(userId) @@ -25,7 +25,7 @@ async function relpyPrivate(userId, msg) { * 休眠函数 * @param ms 毫秒 */ -function sleep(ms) { +function sleep (ms) { return new Promise((resolve) => setTimeout(resolve, ms)) } @@ -33,8 +33,9 @@ function sleep(ms) { * 下载保存文件 * @param fileUrl 下载地址 * @param savePath 保存路径 + * @param param */ -async function downFile(fileUrl, savePath, param = {}) { +async function downFile (fileUrl, savePath, param = {}) { try { mkdirs(path.dirname(savePath)) logger.debug(`[下载文件] ${fileUrl}`) @@ -48,7 +49,7 @@ async function downFile(fileUrl, savePath, param = {}) { } } -function mkdirs(dirname) { +function mkdirs (dirname) { if (fs.existsSync(dirname)) { return true } else { @@ -66,9 +67,11 @@ function mkdirs(dirname) { * @param dec 转发描述 * @param msgsscr 转发信息是否伪装 */ -async function makeForwardMsg(e, msg = [], dec = '', msgsscr = false) { +async function makeForwardMsg (e, msg = [], dec = '', msgsscr = false) { - if (!Array.isArray(msg)) msg = [msg] + if (!Array.isArray(msg)) { + msg = [msg] + } let name = msgsscr ? e.sender.card || e.user_id : Bot.nickname let id = msgsscr ? e.user_id : Bot.uin @@ -84,14 +87,15 @@ async function makeForwardMsg(e, msg = [], dec = '', msgsscr = false) { } let forwardMsg = [] - for (const message of msg){ - if(!message) continue + for (const message of msg) { + if (!message) { + continue + } forwardMsg.push({ ...userInfo, message: message }) } - /** 制作转发内容 */ if (e?.group?.makeForwardMsg) { diff --git a/lib/puppeteer/puppeteer.js b/lib/puppeteer/puppeteer.js index 9ca241a..47498bf 100644 --- a/lib/puppeteer/puppeteer.js +++ b/lib/puppeteer/puppeteer.js @@ -25,4 +25,3 @@ export default { return ret.length > 0 ? ret : false } } - diff --git a/lib/renderer/Renderer.js b/lib/renderer/Renderer.js index 4b64b00..d81c904 100644 --- a/lib/renderer/Renderer.js +++ b/lib/renderer/Renderer.js @@ -37,4 +37,3 @@ export default { return rendererBackends[cfg.renderer?.name || 'puppeteer'] } } - diff --git a/lib/tools/command.js b/lib/tools/command.js index 96ae975..7552f93 100644 --- a/lib/tools/command.js +++ b/lib/tools/command.js @@ -1,4 +1,3 @@ - import '../config/init.js' import log4js from 'log4js' import PluginsLoader from '../plugins/loader.js' @@ -49,7 +48,7 @@ class Command { group_id: data.group_id || 826198224, group_name: data.group_name || '测试群', user_id: data.user_id, - user_avatar:`https://q1.qlogo.cn/g?b=qq&s=0&nk=${data.user_id}`, + user_avatar: `https://q1.qlogo.cn/g?b=qq&s=0&nk=${data.user_id}`, anonymous: null, message: [{ type: 'text', text }], raw_message: text, From 0c737301e8b9bbb85582df8dad30c3991ac01129 Mon Sep 17 00:00:00 2001 From: Ayakura Yuki Date: Wed, 6 Sep 2023 14:48:25 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=B8=8E=E4=B8=8A=E6=B8=B8=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .puppeteerrc.cjs | 17 +++--- renderers/puppeteer/lib/puppeteer.js | 84 ++++++++++++++-------------- 2 files changed, 49 insertions(+), 52 deletions(-) diff --git a/.puppeteerrc.cjs b/.puppeteerrc.cjs index 7ea196e..79228df 100644 --- a/.puppeteerrc.cjs +++ b/.puppeteerrc.cjs @@ -1,5 +1,5 @@ const os = require('os') -const { existsSync, realpathSync } = require('fs') +const { existsSync } = require('fs') const { execSync } = require('child_process') const arch = os.arch() @@ -9,15 +9,14 @@ let executablePath // linux / android if (process.platform === 'linux' || process.platform === 'android') { for (const item of [ - 'chromium', - 'chromium-browser', - 'chrome', - 'chrome-browser', + "chromium", + "chromium-browser", + "chrome", ]) { try { const chromiumPath = execSync(`command -v ${item}`).toString().trim() if (chromiumPath && existsSync(chromiumPath)) { - executablePath = realpathSync(chromiumPath) + executablePath = chromiumPath break } } catch (err) { @@ -41,11 +40,11 @@ if (process.platform === 'darwin') { // windows 或其他 if (!executablePath) { for (const item of [ - 'C:/Program Files/Google/Chrome/Application/chrome.exe', - 'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe', '/usr/bin/chromium', '/usr/bin/chromium-browser', - '/snap/bin/chromium', + '/usr/bin/chrome', + 'C:/Program Files/Google/Chrome/Application/chrome.exe', + 'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe', ]) { if (existsSync(item)) { executablePath = item diff --git a/renderers/puppeteer/lib/puppeteer.js b/renderers/puppeteer/lib/puppeteer.js index d8c8236..ce2c40f 100644 --- a/renderers/puppeteer/lib/puppeteer.js +++ b/renderers/puppeteer/lib/puppeteer.js @@ -23,7 +23,7 @@ export default class PuppeteerRenderer { /** 截图次数 */ this.renderNum = 0 this.config = { - headless: Data.def(config.headless, true), + headless: Data.def(config.headless, 'new'), args: Data.def(config.args, [ '--disable-gpu', '--disable-setuid-sandbox', @@ -61,11 +61,15 @@ export default class PuppeteerRenderer { * 初始化chromium */ async browserInit () { - if (this.browser) return this.browser - if (this.lock) return false + if (this.browser) { + return this.browser + } + if (this.lock) { + return false + } this.lock = true - logger.mark('puppeteer Chromium 启动中...') + logger.info('puppeteer Chromium 启动中...') let connectFlag = false try { @@ -77,7 +81,7 @@ export default class PuppeteerRenderer { // 是否有browser实例 const browserUrl = (await redis.get(this.browserMacKey)) || this.config.wsEndpoint if (browserUrl) { - logger.mark(`puppeteer Chromium from ${browserUrl}`) + logger.info(`puppeteer Chromium from ${browserUrl}`) const browserWSEndpoint = await puppeteer.connect({ browserWSEndpoint: browserUrl }).catch((err) => { logger.error('puppeteer Chromium 缓存的实例已关闭') redis.del(this.browserMacKey) @@ -91,7 +95,7 @@ export default class PuppeteerRenderer { } } } catch (e) { - logger.mark('puppeteer Chromium 不存在已有实例') + logger.info('puppeteer Chromium 不存在已有实例') } if (!this.browser || !connectFlag) { @@ -103,12 +107,12 @@ export default class PuppeteerRenderer { } else { logger.error(err.toString()) if (errMsg.includes('Could not find Chromium')) { - logger.error('没有正确安装Chromium,可以尝试执行安装命令:node ./node_modules/puppeteer/install.js') - } else if (errMsg.includes('libatk-bridge')) { - logger.error('没有正确安装Chromium,可尝试执行 sudo yum install -y chromium') + logger.error('没有正确安装 Chromium,可以尝试执行安装命令:node node_modules/puppeteer/install.js') + } else if (errMsg.includes('cannot open shared object file')) { + logger.error('没有正确安装 Chromium 运行库') } } - console.log(err, trace) + logger.error(err, trace) }) } @@ -119,20 +123,20 @@ export default class PuppeteerRenderer { return false } if (connectFlag) { - logger.mark('puppeteer Chromium 已连接启动的实例') + logger.info('puppeteer Chromium 已连接启动的实例') } else { - console.log('chromium', this.browser.wsEndpoint()) + logger.info(`[Chromium] ${this.browser.wsEndpoint()}`) if (process.env.pm_id && this.browserMacKey) { - //缓存一下实例30天 + // 缓存一下实例30天 const expireTime = 60 * 60 * 24 * 30 await redis.set(this.browserMacKey, this.browser.wsEndpoint(), { EX: expireTime }) } - logger.mark('puppeteer Chromium 启动成功') + logger.info('puppeteer Chromium 启动成功') } /** 监听Chromium实例是否断开 */ this.browser.on('disconnected', (e) => { - logger.error('Chromium实例关闭或崩溃!') + logger.error('Chromium 实例关闭或崩溃!') this.browser = false }) @@ -140,29 +144,19 @@ export default class PuppeteerRenderer { } // 获取Mac地址 - async getMac () { - // 获取Mac地址 - let mac = '00:00:00:00:00:00' + getMac () { + const mac = '00:00:00:00:00:00' try { const network = os.networkInterfaces() - let osMac - // 判断系统 - if (os.platform() === 'win32') { - // windows下获取mac地址 - let osMacList = Object.keys(network).map(key => network[key]).flat() - osMacList = osMacList.filter(item => item.family === 'IPv4' && item.mac !== mac) - osMac = osMacList[0].mac - } else if (os.platform() === 'linux') { - // linux下获取mac地址 - osMac = network.enp6s18.filter(item => item.family === 'IPv4' && item.mac !== mac)[0].mac - } - if (osMac) { - mac = String(osMac) + for (const a in network) { + for (const i of network[a]) { + if (i.mac && i.mac != mac) { + return i.mac + } + } } } catch (e) { - console.log('获取Mac地址失败', e.toString()) } - mac = mac.replace(/:/g, '') return mac } @@ -187,7 +181,9 @@ export default class PuppeteerRenderer { const pageHeight = data.multiPageHeight || 4000 let savePath = this.dealTpl(name, data) - if (!savePath) return false + if (!savePath) { + return false + } let buff = '' let start = Date.now() @@ -225,7 +221,7 @@ export default class PuppeteerRenderer { if (!data.multiPage) { buff = await body.screenshot(randData) /** 计算图片大小 */ - const kb = (buff.length / 1024).toFixed(2) + 'kb' + const kb = (buff.length / 1024).toFixed(2) + 'KB' logger.mark(`[图片生成][${name}][${this.renderNum}次] ${kb} ${logger.green(`${Date.now() - start}ms`)}`) this.renderNum++ ret.push(buff) @@ -252,11 +248,13 @@ export default class PuppeteerRenderer { } else { buff = await page.screenshot(randData) } - if (num > 2) await Data.sleep(200) + if (num > 2) { + await Data.sleep(200) + } this.renderNum++ /** 计算图片大小 */ - const kb = (buff.length / 1024).toFixed(2) + 'kb' + const kb = (buff.length / 1024).toFixed(2) + 'KB' logger.mark(`[图片生成][${name}][${i}/${num}] ${kb}`) ret.push(buff) } @@ -265,9 +263,8 @@ export default class PuppeteerRenderer { } } page.close().catch((err) => logger.error(err)) - } catch (error) { - logger.error(`图片生成失败:${name}:${error}`) + logger.error(`[图片生成][${name}] 图片生成失败:${error}`) /** 关闭浏览器 */ if (this.browser) { await this.browser.close().catch((err) => logger.error(err)) @@ -280,7 +277,7 @@ export default class PuppeteerRenderer { this.shoting.pop() if (ret.length === 0 || !ret[0]) { - logger.error(`图片生成为空:${name}`) + logger.error(`[图片生成][${name}] 图片生成为空`) return false } @@ -323,7 +320,9 @@ export default class PuppeteerRenderer { /** 监听配置文件 */ watch (tplFile) { - if (this.watcher[tplFile]) return + if (this.watcher[tplFile]) { + return + } const watcher = chokidar.watch(tplFile) watcher.on('change', path => { @@ -344,10 +343,9 @@ export default class PuppeteerRenderer { await this.browser.close().catch((err) => logger.error(err)) } this.browser = false - logger.mark('puppeteer 关闭重启...') + logger.info('puppeteer Chromium 关闭重启...') }, 100) } } } } - From 31e99f275094888e4487c725f9027d58805bbe75 Mon Sep 17 00:00:00 2001 From: Ayakura Yuki Date: Wed, 6 Sep 2023 15:33:46 +0800 Subject: [PATCH 4/4] fmt --- .editorconfig | 1 + .puppeteerrc.cjs | 58 ++++++++++++++++++------------------------------ 2 files changed, 23 insertions(+), 36 deletions(-) diff --git a/.editorconfig b/.editorconfig index 1b6e051..a80eb5a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -21,6 +21,7 @@ ij_javascript_enforce_trailing_comma = keep ij_javascript_space_before_method_parentheses = true ij_javascript_spaces_within_object_literal_braces = true ij_javascript_indent_chained_calls = true +ij_javascript_if_brace_force = if_multiline [{*.json,*.json5}] ij_json_keep_trailing_comma = false diff --git a/.puppeteerrc.cjs b/.puppeteerrc.cjs index 79228df..988a079 100644 --- a/.puppeteerrc.cjs +++ b/.puppeteerrc.cjs @@ -6,51 +6,37 @@ const arch = os.arch() let skipDownload = false let executablePath -// linux / android -if (process.platform === 'linux' || process.platform === 'android') { +if (process.platform === 'linux' || process.platform === 'android') for (const item of [ "chromium", "chromium-browser", "chrome", - ]) { - try { - const chromiumPath = execSync(`command -v ${item}`).toString().trim() - if (chromiumPath && existsSync(chromiumPath)) { - executablePath = chromiumPath - break - } - } catch (err) { + ]) try { + const chromiumPath = execSync(`command -v ${item}`).toString().trim() + if (chromiumPath && existsSync(chromiumPath)) { + executablePath = chromiumPath + break } - } -} + } catch (err) {} // macOS -if (process.platform === 'darwin') { - for (const item of [ - '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', - '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge', - ]) { - if (existsSync(item)) { - executablePath = item - break - } - } +if (process.platform === 'darwin') for (const item of [ + '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', + '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge', +]) if (existsSync(item)) { + executablePath = item + break } -// windows 或其他 -if (!executablePath) { - for (const item of [ - '/usr/bin/chromium', - '/usr/bin/chromium-browser', - '/usr/bin/chrome', - 'C:/Program Files/Google/Chrome/Application/chrome.exe', - 'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe', - ]) { - if (existsSync(item)) { - executablePath = item - break - } - } +if (!executablePath) for (const item of [ + '/usr/bin/chromium', + '/usr/bin/chromium-browser', + '/usr/bin/chrome', + 'C:/Program Files/Google/Chrome/Application/chrome.exe', + 'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe', +]) if (existsSync(item)) { + executablePath = item + break } if (executablePath || arch === 'arm64' || arch === 'aarch64') {