koishi-plugin-luogu-saver-bot 0.1.0 → 0.1.1
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 +2 -0
- package/lib/index.js +36 -0
- package/package.json +3 -2
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Context, Schema } from 'koishi';
|
|
2
2
|
export declare const name = "luogu-saver-bot";
|
|
3
|
+
export declare const inject: string[];
|
|
3
4
|
export interface Config {
|
|
4
5
|
endpoint?: string;
|
|
5
6
|
userAgent?: string;
|
|
@@ -94,6 +95,7 @@ declare class LuoguSaverClient {
|
|
|
94
95
|
declare module 'koishi' {
|
|
95
96
|
interface Context {
|
|
96
97
|
luogu_saver: LuoguSaverClient;
|
|
98
|
+
puppeteer?: any;
|
|
97
99
|
}
|
|
98
100
|
}
|
|
99
101
|
export declare function apply(ctx: Context, config?: Config): void;
|
package/lib/index.js
CHANGED
|
@@ -22,6 +22,7 @@ var src_exports = {};
|
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
Config: () => Config,
|
|
24
24
|
apply: () => apply,
|
|
25
|
+
inject: () => inject,
|
|
25
26
|
name: () => name
|
|
26
27
|
});
|
|
27
28
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -43,6 +44,7 @@ __name(statusToString, "statusToString");
|
|
|
43
44
|
|
|
44
45
|
// src/index.ts
|
|
45
46
|
var name = "luogu-saver-bot";
|
|
47
|
+
var inject = ["puppeteer"];
|
|
46
48
|
var Config = import_koishi.Schema.object({
|
|
47
49
|
endpoint: import_koishi.Schema.string().description("自定义 API endpoint,结尾无需斜杠").role("input").default(""),
|
|
48
50
|
userAgent: import_koishi.Schema.string().description("自定义 User-Agent").role("input").default("Uptime-Kuma")
|
|
@@ -135,11 +137,45 @@ function apply(ctx, config = {}) {
|
|
|
135
137
|
if (typeof task === "object" && "status" in task) return `任务 ${id} 状态: ${statusToString(task.status)}`;
|
|
136
138
|
return JSON.stringify(task);
|
|
137
139
|
});
|
|
140
|
+
ctx.command("获取文章 <id>", "获取文章并截取长图").option("width", "-w <width:number>", { fallback: 960 }).action(async ({ session, options }, id) => {
|
|
141
|
+
if (!id) return "请提供文章 ID";
|
|
142
|
+
const art = await ctx.luogu_saver.getArticle(id);
|
|
143
|
+
if (!art) return "未找到文章";
|
|
144
|
+
const content = art.renderedContent ?? art.content ?? "";
|
|
145
|
+
const title = art.title ?? "";
|
|
146
|
+
const escapeHtml = /* @__PURE__ */ __name((s) => s.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]), "escapeHtml");
|
|
147
|
+
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>`;
|
|
148
|
+
if (!ctx.puppeteer) return "当前没有可用的 puppeteer 服务。";
|
|
149
|
+
const page = await ctx.puppeteer.page();
|
|
150
|
+
try {
|
|
151
|
+
const width = Number(options.width) || 960;
|
|
152
|
+
await page.setViewport({ width, height: 800 });
|
|
153
|
+
if (typeof page.emulateMediaFeatures === "function") {
|
|
154
|
+
await page.emulateMediaFeatures([{ name: "prefers-color-scheme", value: "light" }]);
|
|
155
|
+
}
|
|
156
|
+
await page.setContent(html, { waitUntil: "networkidle0" });
|
|
157
|
+
try {
|
|
158
|
+
await page.evaluate(() => {
|
|
159
|
+
document.documentElement.style.background = "#ffffff";
|
|
160
|
+
if (document.body) document.body.style.background = "#ffffff";
|
|
161
|
+
});
|
|
162
|
+
} catch (e) {
|
|
163
|
+
}
|
|
164
|
+
const buffer = await page.screenshot({ fullPage: true, type: "png", omitBackground: false });
|
|
165
|
+
return import_koishi.h.image(buffer, "image/png");
|
|
166
|
+
} catch (err) {
|
|
167
|
+
ctx.logger.error("截图文章失败", err);
|
|
168
|
+
return "获取失败";
|
|
169
|
+
} finally {
|
|
170
|
+
page.close();
|
|
171
|
+
}
|
|
172
|
+
});
|
|
138
173
|
}
|
|
139
174
|
__name(apply, "apply");
|
|
140
175
|
// Annotate the CommonJS export names for ESM import in node:
|
|
141
176
|
0 && (module.exports = {
|
|
142
177
|
Config,
|
|
143
178
|
apply,
|
|
179
|
+
inject,
|
|
144
180
|
name
|
|
145
181
|
});
|
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.
|
|
4
|
+
"version": "0.1.1",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
],
|
|
18
18
|
"devDependencies": {},
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"koishi": "^4.18.7"
|
|
20
|
+
"koishi": "^4.18.7",
|
|
21
|
+
"puppeteer": "^3.9.0"
|
|
21
22
|
}
|
|
22
23
|
}
|