koishi-plugin-luogu-saver-bot 0.1.2 → 0.1.3

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 +50 -4
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -150,16 +150,39 @@ function apply(ctx, config = {}) {
150
150
  const content = art.renderedContent ?? art.content ?? "";
151
151
  const title = art.title ?? "";
152
152
  const escapeHtml = /* @__PURE__ */ __name((s) => s.replace(/[&<>"']/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[c]), "escapeHtml");
153
- const html = `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><style>body{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial;padding:20px;line-height:1.8;color:#222}img{max-width:100%}h1{font-size:24px;margin-bottom:12px}</style></head><body><h1>${escapeHtml(title)}</h1>${content}</body></html>`;
153
+ const html = `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><style>:root{--bg:#f4f6fb;--card:#ffffff;--text:#111;--muted:#6b7280;--accent:#2563eb}html,body{height:100%}body{margin:0;background:var(--bg);-webkit-font-smoothing:antialiased;font-family:Inter,-apple-system,system-ui,"Segoe UI",Roboto,"Helvetica Neue",Arial;padding:40px;color:var(--text)}.container{max-width:900px;margin:0 auto}.card{background:var(--card);padding:32px;border-radius:12px;box-shadow:0 10px 30px rgba(2,6,23,0.08)}h1{font-size:28px;margin:0 0 12px}.meta{color:var(--muted);font-size:13px;margin-bottom:12px}article{line-height:1.75;color:var(--text)}img{max-width:100%;border-radius:8px}pre,code{background:#f6f8fa;padding:12px;border-radius:8px;overflow:auto;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:13px}blockquote{border-left:4px solid #e6e9ef;padding:8px 16px;color:var(--muted);margin:8px 0}a{color:var(--accent)}</style></head><body><div class="container"><div class="card"><h1>${escapeHtml(title)}</h1><div class="meta">作者 ${escapeHtml(String(art.authorId))}</div><article>${content}</article></div></div></body></html>`;
154
154
  if (!ctx.puppeteer) return "当前没有可用的 puppeteer 服务。";
155
155
  const page = await ctx.puppeteer.page();
156
156
  try {
157
157
  const width = Number(options.width) || 960;
158
- await page.setViewport({ width, height: 800 });
158
+ await page.setViewport({ width, height: 800, deviceScaleFactor: 2 });
159
159
  if (typeof page.emulateMediaFeatures === "function") {
160
160
  await page.emulateMediaFeatures([{ name: "prefers-color-scheme", value: "light" }]);
161
161
  }
162
162
  await page.setContent(html, { waitUntil: "networkidle0" });
163
+ try {
164
+ await page.evaluate(() => new Promise((resolve) => {
165
+ const imgs = Array.from(document.images);
166
+ if (!imgs.length) return resolve(null);
167
+ let loaded = 0;
168
+ imgs.forEach((img) => {
169
+ if (img.complete) {
170
+ loaded++;
171
+ return;
172
+ }
173
+ img.addEventListener("load", () => {
174
+ loaded++;
175
+ if (loaded === imgs.length) resolve(null);
176
+ });
177
+ img.addEventListener("error", () => {
178
+ loaded++;
179
+ if (loaded === imgs.length) resolve(null);
180
+ });
181
+ });
182
+ if (loaded === imgs.length) resolve(null);
183
+ }));
184
+ } catch (e) {
185
+ }
163
186
  try {
164
187
  await page.evaluate(() => {
165
188
  document.documentElement.style.background = "#ffffff";
@@ -183,16 +206,39 @@ function apply(ctx, config = {}) {
183
206
  const content = paste.renderedContent ?? paste.content ?? "";
184
207
  const title = paste.id ?? "";
185
208
  const escapeHtml = /* @__PURE__ */ __name((s) => s.replace(/[&<>\"']/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[c]), "escapeHtml");
186
- const html = `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><style>body{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial;padding:20px;line-height:1.8;color:#222}pre,code{white-space:pre-wrap;word-break:break-word;background:#f8f8f8;padding:12px;border-radius:6px}h1{font-size:18px;margin-bottom:8px}</style></head><body><h1>${escapeHtml(title)}</h1>${content}</body></html>`;
209
+ const html = `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><style>:root{--bg:#f4f6fb;--card:#ffffff;--text:#111;--muted:#6b7280;--accent:#2563eb}html,body{height:100%}body{margin:0;background:var(--bg);-webkit-font-smoothing:antialiased;font-family:Inter,-apple-system,system-ui,"Segoe UI",Roboto,"Helvetica Neue",Arial;padding:40px;color:var(--text)}.container{max-width:900px;margin:0 auto}.card{background:var(--card);padding:24px;border-radius:12px;box-shadow:0 8px 20px rgba(2,6,23,0.06)}h1{font-size:20px;margin:0 0 8px}pre,code{white-space:pre-wrap;word-break:break-word;background:#f6f8fa;padding:12px;border-radius:8px;overflow:auto;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:13px}img{max-width:100%;border-radius:6px}a{color:var(--accent)}</style></head><body><div class="container"><div class="card"><h1>${escapeHtml(title)}</h1><article>${content}</article></div></div></body></html>`;
187
210
  if (!ctx.puppeteer) return "当前没有可用的 puppeteer 服务。";
188
211
  const page = await ctx.puppeteer.page();
189
212
  try {
190
213
  const width = Number(options.width) || 960;
191
- await page.setViewport({ width, height: 800 });
214
+ await page.setViewport({ width, height: 800, deviceScaleFactor: 2 });
192
215
  if (typeof page.emulateMediaFeatures === "function") {
193
216
  await page.emulateMediaFeatures([{ name: "prefers-color-scheme", value: "light" }]);
194
217
  }
195
218
  await page.setContent(html, { waitUntil: "networkidle0" });
219
+ try {
220
+ await page.evaluate(() => new Promise((resolve) => {
221
+ const imgs = Array.from(document.images);
222
+ if (!imgs.length) return resolve(null);
223
+ let loaded = 0;
224
+ imgs.forEach((img) => {
225
+ if (img.complete) {
226
+ loaded++;
227
+ return;
228
+ }
229
+ img.addEventListener("load", () => {
230
+ loaded++;
231
+ if (loaded === imgs.length) resolve(null);
232
+ });
233
+ img.addEventListener("error", () => {
234
+ loaded++;
235
+ if (loaded === imgs.length) resolve(null);
236
+ });
237
+ });
238
+ if (loaded === imgs.length) resolve(null);
239
+ }));
240
+ } catch (e) {
241
+ }
196
242
  try {
197
243
  await page.evaluate(() => {
198
244
  document.documentElement.style.background = "#ffffff";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-luogu-saver-bot",
3
3
  "description": "洛谷保存站机器人",
4
- "version": "0.1.2",
4
+ "version": "0.1.3",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [