XWX-AI Claude Opus 4.6 commited on
Commit
fb94a6b
·
1 Parent(s): f8322a5

feat: update server logic

Browse files

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. server.js +35 -18
server.js CHANGED
@@ -121,22 +121,35 @@ app.get('/', (req, res) => {
121
 
122
  app.post('/api/generate_pdf', async (req, res) => {
123
  const startTime = Date.now();
124
- const envText = isTest ? '测试环境' : '生产环境';
125
- const bgColor = isTest ? '\x1b[44m' : '\x1b[41m';
126
- const { platform, exportCount, language, exportPdf, exportMd, exportTxt, exportDocx, exportJson, exportClipboard } = req.body;
127
- const platformStr = platform ? `\x1b[36m${platform}\x1b[0m` : '\x1b[90munknown\x1b[0m';
128
- const countStr = exportCount != null ? `\x1b[33m${exportCount}次\x1b[0m` : '\x1b[90m-\x1b[0m';
129
- const langStr = language ? `\x1b[32m${language}\x1b[0m` : '\x1b[90m-\x1b[0m';
130
- const fmtStr = [
131
- exportPdf != null ? `PDF:${exportPdf}` : null,
132
- exportMd != null ? `MD:${exportMd}` : null,
133
- exportTxt != null ? `TXT:${exportTxt}` : null,
134
- exportDocx != null ? `DOCX:${exportDocx}` : null,
135
- exportJson != null ? `JSON:${exportJson}` : null,
136
- exportClipboard != null ? `CLIP:${exportClipboard}` : null,
137
- ].filter(Boolean).join(', ');
138
- console.log(`---------------\x1b[37m[PDF-GEN] 收到 API 请求\x1b[0m | 环境: ${envText} | 平台: ${platformStr} | 导出: ${countStr} | ${fmtStr} | 语言: ${langStr}---------------`);
139
-
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  const getElapsed = () => ((Date.now() - startTime) / 1000).toFixed(2) + 's';
141
  let browser = null;
142
 
@@ -172,7 +185,11 @@ app.post('/api/generate_pdf', async (req, res) => {
172
 
173
  console.log(`[PDF-GEN] [${getElapsed()}] 解析请求完成: HTML ${htmlSizeMB} MB, 图片 ${imgCount} 张 (${imgSizeMB} MB)`);
174
 
175
- // 关键修复:移除所有图片的 loading="lazy" 属性
 
 
 
 
176
  // 问题:loading="lazy" 导致视口外的图片在 PDF 中不渲染
177
  // 解决方案:https://stackoverflow.com/questions/79156691/puppeteersharp-fails-to-display-base64-encoded-images-in-pdf-output
178
  htmlToProcess = htmlToProcess.replace(/loading=["']lazy["']/gi, '');
@@ -382,7 +399,7 @@ app.post('/api/generate_pdf', async (req, res) => {
382
  await browser.close();
383
  browser = null;
384
 
385
- console.log(`\x1b[32m[PDF-GEN] [${getElapsed()}] 任务全部完成,已发送响应\x1b[0m`);
386
 
387
  res.setHeader('Content-Type', 'application/pdf');
388
  res.setHeader('Content-Disposition', 'attachment; filename=export.pdf');
 
121
 
122
  app.post('/api/generate_pdf', async (req, res) => {
123
  const startTime = Date.now();
124
+ const envText = isTest ? '测试环境' : '生产环境';
125
+ const { platform, exportCount, language, exportPdf, exportMd, exportTxt, exportDocx, exportJson, exportClipboard, exportNotion, extensionVersion, imageCount, totalImageSizeMB, isPro } = req.body;
126
+
127
+ // PRO status with text emphasis (no ANSI colors for Docker compatibility)
128
+ const userTag = isPro ? '★★★PRO★★★' : '◆FREE◆';
129
+ const platformStr = platform || 'unknown';
130
+ const versionStr = extensionVersion || '-';
131
+ const langStr = language || '-';
132
+
133
+ // Format counts
134
+ const fmtParts = [];
135
+ if (exportPdf != null) fmtParts.push(`PDF:${exportPdf}`);
136
+ if (exportMd != null) fmtParts.push(`MD:${exportMd}`);
137
+ if (exportTxt != null) fmtParts.push(`TXT:${exportTxt}`);
138
+ if (exportDocx != null) fmtParts.push(`DOCX:${exportDocx}`);
139
+ if (exportJson != null) fmtParts.push(`JSON:${exportJson}`);
140
+ if (exportClipboard != null) fmtParts.push(`CLIP:${exportClipboard}`);
141
+ if (exportNotion != null) fmtParts.push(`NOTION:${exportNotion}`);
142
+ const fmtStr = fmtParts.join(', ') || '-';
143
+
144
+ // Two-line log for readability
145
+ console.log(
146
+ `---------------[PDF-GEN] ${envText} ${userTag} 收到请求 | 平台: ${platformStr} | 版本: ${versionStr} | 语言: ${langStr}`
147
+ );
148
+ console.log(
149
+ ` 导出: ${exportCount ?? '-'}次 | 格式: ${fmtStr} | 图片: ${imageCount ?? '-'}张 (${totalImageSizeMB ?? '-'}MB)`
150
+ );
151
+ console.log(`---------------`);
152
+
153
  const getElapsed = () => ((Date.now() - startTime) / 1000).toFixed(2) + 's';
154
  let browser = null;
155
 
 
185
 
186
  console.log(`[PDF-GEN] [${getElapsed()}] 解析请求完成: HTML ${htmlSizeMB} MB, 图片 ${imgCount} 张 (${imgSizeMB} MB)`);
187
 
188
+ // 移除 <script> 标签防止 Puppeteer 执行阻塞(如 alert() 弹窗会导致 setContent 内部 CDP 调用超时)
189
+ htmlToProcess = htmlToProcess.replace(/<script[\s\S]*?<\/script>/gi, '');
190
+ console.log(`[PDF-GEN] [${getElapsed()}] 已移除 <script> 标签`);
191
+
192
+ // 移除所有图片的 loading="lazy" 属性
193
  // 问题:loading="lazy" 导致视口外的图片在 PDF 中不渲染
194
  // 解决方案:https://stackoverflow.com/questions/79156691/puppeteersharp-fails-to-display-base64-encoded-images-in-pdf-output
195
  htmlToProcess = htmlToProcess.replace(/loading=["']lazy["']/gi, '');
 
399
  await browser.close();
400
  browser = null;
401
 
402
+ console.log(`[PDF-GEN] [${getElapsed()}] >>> 任务全部完成 <<<`);
403
 
404
  res.setHeader('Content-Type', 'application/pdf');
405
  res.setHeader('Content-Disposition', 'attachment; filename=export.pdf');