koishi-plugin-fimtale-api 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/index.js +24 -11
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -166,7 +166,6 @@ function apply(ctx, config) {
166
166
  light: { bg: "#ffffff", text: "#333333", accent: "#666" },
167
167
  dark: { bg: "#2b2b2b", text: "#dcdcdc", accent: "#888" },
168
168
  sepia: { bg: "#f5e8d0", text: "#5b4636", accent: "#8b7355" }
169
- // 羊皮纸
170
169
  };
171
170
  const t = themes[config.readTheme];
172
171
  const htmlContent = `
@@ -180,7 +179,7 @@ function apply(ctx, config) {
180
179
  background-color: ${t.bg};
181
180
  color: ${t.text};
182
181
  padding: 40px;
183
- width: 500px; /* 限制宽度适合手机观看 */
182
+ width: 480px; /* 稍微调窄一点,防止手机看不清 */
184
183
  line-height: 1.8;
185
184
  word-wrap: break-word;
186
185
  }
@@ -188,9 +187,9 @@ function apply(ctx, config) {
188
187
  .title { font-size: 26px; font-weight: bold; margin-bottom: 8px; }
189
188
  .meta { font-size: 14px; opacity: 0.8; }
190
189
  .content { font-size: 18px; text-align: justify; }
191
- /* Fimtale 的内容通常包含 p 标签,我们做一些优化 */
192
190
  .content p { margin-bottom: 1.2em; text-indent: 2em; }
193
- .content img { max-width: 100%; height: auto; border-radius: 6px; display: block; margin: 10px auto; }
191
+ /* 限制图片最大高度,防止一张图把 puppeteer 撑爆 */
192
+ .content img { max-width: 100%; max-height: 800px; height: auto; border-radius: 6px; display: block; margin: 10px auto; }
194
193
  .footer { margin-top: 40px; text-align: center; font-size: 12px; opacity: 0.6; }
195
194
  </style>
196
195
  </head>
@@ -209,21 +208,35 @@ function apply(ctx, config) {
209
208
  </div>
210
209
  </body>
211
210
  </html>`;
212
- let imgBuf;
211
+ const messageElements = [];
213
212
  try {
214
213
  const page = await ctx.puppeteer.page();
215
214
  await page.setContent(htmlContent);
216
215
  await new Promise((r) => setTimeout(r, 500));
217
- const body = await page.$("body");
218
- imgBuf = await body.screenshot({ type: "png" });
216
+ const bodyHeight = await page.evaluate(() => document.body.scrollHeight);
217
+ const MAX_HEIGHT = 5e3;
218
+ if (bodyHeight <= MAX_HEIGHT) {
219
+ const imgBuf = await page.screenshot({ fullPage: true, type: "jpeg", quality: 80 });
220
+ messageElements.push(import_koishi.h.image(imgBuf, "image/jpeg"));
221
+ } else {
222
+ const viewportHeight = 4e3;
223
+ let currentScroll = 0;
224
+ await page.setViewport({ width: 560, height: viewportHeight });
225
+ while (currentScroll < bodyHeight) {
226
+ const imgBuf = await page.screenshot({ type: "jpeg", quality: 100 });
227
+ messageElements.push(import_koishi.h.image(imgBuf, "image/jpeg"));
228
+ currentScroll += viewportHeight;
229
+ if (currentScroll < bodyHeight) {
230
+ await page.evaluate((y) => window.scrollTo(0, y), currentScroll);
231
+ await new Promise((r) => setTimeout(r, 200));
232
+ }
233
+ }
234
+ }
219
235
  await page.close();
220
236
  } catch (e) {
221
237
  ctx.logger("fimtale").error(e);
222
- return "❌ 生成图片失败,请检查服务器 Puppeteer 配置。";
238
+ return "❌ 图片生成失败:内容过长或 puppeteer 错误。";
223
239
  }
224
- const messageElements = [
225
- import_koishi.h.image(imgBuf, "image/png")
226
- ];
227
240
  const navs = [];
228
241
  if (prevId) navs.push(`⬅️上一章: ft.read ${prevId}`);
229
242
  if (nextId) navs.push(`➡️下一章: ft.read ${nextId}`);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-fimtale-api",
3
3
  "description": "自用Koishi插件,从fimtale-api获取信息",
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [