koishi-plugin-luogu-saver-bot 0.1.1 → 0.1.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.
package/lib/index.d.ts CHANGED
@@ -38,6 +38,23 @@ export type ArticleHistory = {
38
38
  content: string;
39
39
  createdAt: string;
40
40
  };
41
+ export type Paste = {
42
+ id: string;
43
+ content: string;
44
+ authorId: number;
45
+ deleted?: number | boolean;
46
+ createdAt?: string;
47
+ updatedAt?: string;
48
+ deleteReason?: string;
49
+ renderedContent?: string | null;
50
+ author?: {
51
+ id: number;
52
+ name: string;
53
+ color?: string;
54
+ createdAt?: string;
55
+ updatedAt?: string;
56
+ };
57
+ };
41
58
  export type CountResponse = {
42
59
  count: number;
43
60
  };
@@ -81,6 +98,7 @@ declare class LuoguSaverClient {
81
98
  private buildUrl;
82
99
  private headers;
83
100
  getArticle(id: string, extraHeaders?: Record<string, string>): Promise<Article>;
101
+ getPaste(id: string, extraHeaders?: Record<string, string>): Promise<Paste>;
84
102
  getRecent(opts?: {
85
103
  count?: number;
86
104
  updated_after?: string;
package/lib/index.js CHANGED
@@ -75,6 +75,12 @@ var LuoguSaverClient = class {
75
75
  if (res.code !== 200) return null;
76
76
  return res.data;
77
77
  }
78
+ async getPaste(id, extraHeaders) {
79
+ const url = this.buildUrl(`/paste/query/${encodeURIComponent(id)}`);
80
+ const res = await this.ctx.http.get(url, { headers: this.headers(extraHeaders) });
81
+ if (res.code !== 200) return null;
82
+ return res.data;
83
+ }
78
84
  async getRecent(opts, extraHeaders) {
79
85
  const params = new URLSearchParams();
80
86
  if (opts?.count != null) params.set("count", String(opts.count));
@@ -170,6 +176,39 @@ function apply(ctx, config = {}) {
170
176
  page.close();
171
177
  }
172
178
  });
179
+ ctx.command("获取剪贴板 <id>", "获取剪贴板内容并截取长图").option("width", "-w <width:number>", { fallback: 960 }).action(async ({ session, options }, id) => {
180
+ if (!id) return "请提供剪贴板 ID";
181
+ const paste = await ctx.luogu_saver.getPaste(id);
182
+ if (!paste) return "未找到剪贴板内容";
183
+ const content = paste.renderedContent ?? paste.content ?? "";
184
+ const title = paste.id ?? "";
185
+ 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>`;
187
+ if (!ctx.puppeteer) return "当前没有可用的 puppeteer 服务。";
188
+ const page = await ctx.puppeteer.page();
189
+ try {
190
+ const width = Number(options.width) || 960;
191
+ await page.setViewport({ width, height: 800 });
192
+ if (typeof page.emulateMediaFeatures === "function") {
193
+ await page.emulateMediaFeatures([{ name: "prefers-color-scheme", value: "light" }]);
194
+ }
195
+ await page.setContent(html, { waitUntil: "networkidle0" });
196
+ try {
197
+ await page.evaluate(() => {
198
+ document.documentElement.style.background = "#ffffff";
199
+ if (document.body) document.body.style.background = "#ffffff";
200
+ });
201
+ } catch (e) {
202
+ }
203
+ const buffer = await page.screenshot({ fullPage: true, type: "png", omitBackground: false });
204
+ return import_koishi.h.image(buffer, "image/png");
205
+ } catch (err) {
206
+ ctx.logger.error("截图剪贴板失败", err);
207
+ return "获取失败。";
208
+ } finally {
209
+ page.close();
210
+ }
211
+ });
173
212
  }
174
213
  __name(apply, "apply");
175
214
  // Annotate the CommonJS export names for ESM import in node:
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.1",
4
+ "version": "0.1.2",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [