UNPKG

@karinjs/plugin-puppeteer

Version:

karin的 puppeteer截图、渲染插件

122 lines (119 loc) 3.35 kB
import fs from 'node:fs'; import path from 'node:path'; import { karin } from 'node-karin'; import { basePath } from 'node-karin/root'; // package.json var package_default = { name: "@karinjs/plugin-puppeteer", version: "1.1.2", description: "karin\u7684 puppeteer\u622A\u56FE\u3001\u6E32\u67D3\u63D2\u4EF6", keywords: [ "karin", "node-karin", "karin-plugin", "puppeteer", "browser", "headless" ], license: "MIT", author: "sj817", type: "module", main: "dist/index.js", types: "dist/index.d.ts", files: [ "dist" ], scripts: { build: "tsc && tsup", dev: "tsx src/app.ts", "dev:test": "node dist/index.js", pub: "pnpm publish --access public", rm: `node -e "require('fs').rmSync('dist', { recursive: true, force: true })"`, sync: 'curl -X PUT "https://registry-direct.npmmirror.com/-/package/@karinjs/plugin-puppeteer/syncs"' }, dependencies: { "@snapka/puppeteer": "0.1.5" }, devDependencies: { "node-karin": "^1.13.0" }, karin: { main: "src/index.ts", apps: [], web: "dist/web.config.js", "ts-web": "src/web.config.ts", files: [ "config" ] }, timestamp: "2025-10-01T00:47:22.222Z" }; var HMR_KEY = "karin-plugin-puppeteer-hmr"; var defaultConfig = { protocol: "cdp", headless: "shell", debug: false, findBrowser: true, slowMo: 0, maxOpenPages: 10, pageMode: "reuse", pageIdleTimeout: 6e4, defaultViewport: { width: 800, height: 600 }, download: { enable: true, browser: "chrome-headless-shell", version: "latest" }, args: [ "--window-size=800,600", // 设置窗口大小 "--disable-gpu", // 禁用 GPU 硬件加速 "--no-sandbox", // 关闭 Chrome 的沙盒模式 "--disable-setuid-sandbox", // 进一步禁用 setuid 沙盒机制,通常和 --no-sandbox 配合使用,避免权限问题 "--no-zygote", // 关闭 Chrome 的 zygote 进程,减少进程开销,优化资源使用 "--disable-extensions", // 禁用扩展 "--disable-dev-shm-usage", // 禁用 /dev/shm(共享内存)用作临时存储,改用磁盘存储 "--disable-background-networking", // 禁用后台网络请求 "--disable-sync", // 禁用 Chrome 的同步功能 "--disable-crash-reporter", // 禁用崩溃报告 "--disable-translate", // 禁用翻译 "--disable-notifications", // 禁用通知 "--disable-device-discovery-notifications", // 禁用设备发现通知 "--disable-accelerated-2d-canvas" // 禁用 2D 画布的硬件加速 ] }; var pluginName = package_default.name.replace(/\//g, "-"); var pluginVersion = package_default.version; var configPath = path.resolve(basePath, pluginName, "config", "config.json"); var init = () => { if (!fs.existsSync(configPath)) { fs.mkdirSync(path.dirname(configPath), { recursive: true }); fs.writeFileSync(configPath, JSON.stringify(defaultConfig, null, 2)); } }; var getConfig = () => { const data = JSON.parse(fs.readFileSync(configPath, "utf-8")); return { ...defaultConfig, ...data }; }; var saveConfig = (config) => { fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); karin.emit(HMR_KEY, config); }; init(); export { HMR_KEY, getConfig, package_default, pluginName, pluginVersion, saveConfig };