koishi-plugin-cfmrmod 1.1.2 → 1.1.4
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/dist/mcmod/cards/author-card.js +472 -0
- package/dist/mcmod/cards/center-card.js +509 -0
- package/dist/mcmod/cards/info-card.js +262 -0
- package/dist/mcmod/cards/mod-card.js +828 -0
- package/dist/mcmod/cards/tutorial-card.js +531 -0
- package/dist/mcmod/cards.js +14 -0
- package/dist/mcmod/constants.js +13 -0
- package/dist/mcmod/http.js +195 -0
- package/dist/mcmod/index.js +5 -3483
- package/dist/mcmod/plugin.js +310 -0
- package/dist/mcmod/rendering.js +388 -0
- package/dist/mcmod/search.js +122 -0
- package/dist/mcmod/utils.js +97 -0
- package/package.json +1 -1
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setMcmodCookie = setMcmodCookie;
|
|
4
|
+
exports.getMcmodCookie = getMcmodCookie;
|
|
5
|
+
exports.loadManagedCookie = loadManagedCookie;
|
|
6
|
+
exports.fetchWithTimeout = fetchWithTimeout;
|
|
7
|
+
exports.getHeaders = getHeaders;
|
|
8
|
+
exports.getImageHeaders = getImageHeaders;
|
|
9
|
+
exports.ensureValidCookie = ensureValidCookie;
|
|
10
|
+
exports.fetchMcmodText = fetchMcmodText;
|
|
11
|
+
exports.fetchMcmodJson = fetchMcmodJson;
|
|
12
|
+
const constants_1 = require("./constants");
|
|
13
|
+
const fetch = require('node-fetch');
|
|
14
|
+
let cookieManager = null;
|
|
15
|
+
try {
|
|
16
|
+
cookieManager = require('../../cookie-manager');
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
// cookie-manager 不存在时静默忽略
|
|
20
|
+
}
|
|
21
|
+
let globalCookie = '';
|
|
22
|
+
let cookieLastCheck = 0;
|
|
23
|
+
const COOKIE_CHECK_INTERVAL = 30 * 60 * 1000;
|
|
24
|
+
function mergeCookie(name, value) {
|
|
25
|
+
if (!name || !value)
|
|
26
|
+
return;
|
|
27
|
+
const parts = String(globalCookie || '')
|
|
28
|
+
.split(';')
|
|
29
|
+
.map(part => part.trim())
|
|
30
|
+
.filter(Boolean)
|
|
31
|
+
.filter(part => part.split('=')[0] !== name);
|
|
32
|
+
parts.push(`${name}=${value}`);
|
|
33
|
+
globalCookie = parts.join('; ');
|
|
34
|
+
}
|
|
35
|
+
function rememberSetCookie(setCookie) {
|
|
36
|
+
if (!setCookie)
|
|
37
|
+
return;
|
|
38
|
+
const list = Array.isArray(setCookie) ? setCookie : [setCookie];
|
|
39
|
+
for (const line of list) {
|
|
40
|
+
const first = String(line || '').split(';')[0];
|
|
41
|
+
const idx = first.indexOf('=');
|
|
42
|
+
if (idx <= 0)
|
|
43
|
+
continue;
|
|
44
|
+
mergeCookie(first.slice(0, idx).trim(), first.slice(idx + 1).trim());
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function rememberResponseCookies(res) {
|
|
48
|
+
var _a, _b, _c, _d;
|
|
49
|
+
try {
|
|
50
|
+
const raw = (_b = (_a = res === null || res === void 0 ? void 0 : res.headers) === null || _a === void 0 ? void 0 : _a.raw) === null || _b === void 0 ? void 0 : _b.call(_a)['set-cookie'];
|
|
51
|
+
if (raw === null || raw === void 0 ? void 0 : raw.length)
|
|
52
|
+
rememberSetCookie(raw);
|
|
53
|
+
}
|
|
54
|
+
catch { }
|
|
55
|
+
try {
|
|
56
|
+
const one = (_d = (_c = res === null || res === void 0 ? void 0 : res.headers) === null || _c === void 0 ? void 0 : _c.get) === null || _d === void 0 ? void 0 : _d.call(_c, 'set-cookie');
|
|
57
|
+
if (one)
|
|
58
|
+
rememberSetCookie(one);
|
|
59
|
+
}
|
|
60
|
+
catch { }
|
|
61
|
+
}
|
|
62
|
+
function hasCookie(name) {
|
|
63
|
+
return String(globalCookie || '').split(';').some(part => part.trim().startsWith(`${name}=`));
|
|
64
|
+
}
|
|
65
|
+
function setMcmodCookie(cookie, checkedAt = Date.now()) {
|
|
66
|
+
globalCookie = String(cookie || '');
|
|
67
|
+
cookieLastCheck = checkedAt;
|
|
68
|
+
}
|
|
69
|
+
function getMcmodCookie() {
|
|
70
|
+
return globalCookie;
|
|
71
|
+
}
|
|
72
|
+
function loadManagedCookie(logger) {
|
|
73
|
+
if (!cookieManager)
|
|
74
|
+
return;
|
|
75
|
+
cookieManager.getCookie().then(cookie => {
|
|
76
|
+
var _a;
|
|
77
|
+
if (cookie) {
|
|
78
|
+
setMcmodCookie(cookie);
|
|
79
|
+
(_a = logger === null || logger === void 0 ? void 0 : logger.info) === null || _a === void 0 ? void 0 : _a.call(logger, '已自动获取 mcmod.cn Cookie');
|
|
80
|
+
}
|
|
81
|
+
}).catch(e => {
|
|
82
|
+
var _a;
|
|
83
|
+
(_a = logger === null || logger === void 0 ? void 0 : logger.warn) === null || _a === void 0 ? void 0 : _a.call(logger, '自动获取 Cookie 失败:', (e === null || e === void 0 ? void 0 : e.message) || e);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
async function fetchWithTimeout(url, opts = {}, timeout = 15000) {
|
|
87
|
+
const controller = new AbortController();
|
|
88
|
+
const id = setTimeout(() => controller.abort(), timeout);
|
|
89
|
+
try {
|
|
90
|
+
const res = await fetch(url, { ...opts, signal: controller.signal });
|
|
91
|
+
clearTimeout(id);
|
|
92
|
+
return res;
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
clearTimeout(id);
|
|
96
|
+
throw err;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function getHeaders(referer = `${constants_1.BASE_URL}/`) {
|
|
100
|
+
const headers = {
|
|
101
|
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
102
|
+
'Referer': referer,
|
|
103
|
+
'Accept-Language': 'zh-CN,zh;q=0.9',
|
|
104
|
+
'X-Requested-With': 'XMLHttpRequest',
|
|
105
|
+
};
|
|
106
|
+
if (globalCookie) {
|
|
107
|
+
headers['Cookie'] = globalCookie;
|
|
108
|
+
}
|
|
109
|
+
return headers;
|
|
110
|
+
}
|
|
111
|
+
function getImageHeaders(url, referer = `${constants_1.BASE_URL}/`) {
|
|
112
|
+
const headers = {
|
|
113
|
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
114
|
+
'Referer': referer,
|
|
115
|
+
'Accept': 'image/avif,image/webp,image/apng,image/*,*/*;q=0.8',
|
|
116
|
+
};
|
|
117
|
+
try {
|
|
118
|
+
const host = new URL(url).hostname;
|
|
119
|
+
if (globalCookie && /(^|\.)mcmod\.cn$/i.test(host)) {
|
|
120
|
+
headers['Cookie'] = globalCookie;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
catch { }
|
|
124
|
+
return headers;
|
|
125
|
+
}
|
|
126
|
+
async function ensureValidCookie() {
|
|
127
|
+
const now = Date.now();
|
|
128
|
+
if (hasCookie('MCMOD_SEED') && (now - cookieLastCheck) < COOKIE_CHECK_INTERVAL) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (cookieManager) {
|
|
132
|
+
try {
|
|
133
|
+
const cookie = await cookieManager.getCookie();
|
|
134
|
+
if (cookie) {
|
|
135
|
+
setMcmodCookie(cookie, now);
|
|
136
|
+
if (hasCookie('MCMOD_SEED'))
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
catch (e) {
|
|
141
|
+
// 静默失败
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
const res = await fetchWithTimeout(`${constants_1.BASE_URL}/`, {
|
|
146
|
+
headers: getHeaders(`${constants_1.BASE_URL}/`),
|
|
147
|
+
});
|
|
148
|
+
rememberResponseCookies(res);
|
|
149
|
+
cookieLastCheck = now;
|
|
150
|
+
try {
|
|
151
|
+
await res.text();
|
|
152
|
+
}
|
|
153
|
+
catch { }
|
|
154
|
+
}
|
|
155
|
+
catch (e) {
|
|
156
|
+
cookieLastCheck = now;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function withCurrentCookie(opts = {}) {
|
|
160
|
+
const headers = { ...(opts.headers || {}) };
|
|
161
|
+
if (globalCookie)
|
|
162
|
+
headers['Cookie'] = globalCookie;
|
|
163
|
+
return { ...opts, headers };
|
|
164
|
+
}
|
|
165
|
+
function rememberDocumentCookie(html) {
|
|
166
|
+
const text = String(html || '');
|
|
167
|
+
const regex = /document\.cookie\s*=\s*(['"])(.*?)\1/g;
|
|
168
|
+
let match;
|
|
169
|
+
while ((match = regex.exec(text)))
|
|
170
|
+
rememberSetCookie(match[2]);
|
|
171
|
+
}
|
|
172
|
+
function isCookieChallengeHtml(html) {
|
|
173
|
+
const text = String(html || '');
|
|
174
|
+
return text.length < 1024 && /document\.cookie\s*=/.test(text) && /window\.location\.href\s*=/.test(text);
|
|
175
|
+
}
|
|
176
|
+
async function fetchMcmodText(url, opts = {}, timeout = 15000) {
|
|
177
|
+
await ensureValidCookie();
|
|
178
|
+
let res = await fetchWithTimeout(url, withCurrentCookie(opts), timeout);
|
|
179
|
+
rememberResponseCookies(res);
|
|
180
|
+
let html = await res.text();
|
|
181
|
+
rememberDocumentCookie(html);
|
|
182
|
+
if (isCookieChallengeHtml(html)) {
|
|
183
|
+
cookieLastCheck = 0;
|
|
184
|
+
await ensureValidCookie();
|
|
185
|
+
res = await fetchWithTimeout(url, withCurrentCookie(opts), timeout);
|
|
186
|
+
rememberResponseCookies(res);
|
|
187
|
+
html = await res.text();
|
|
188
|
+
rememberDocumentCookie(html);
|
|
189
|
+
}
|
|
190
|
+
return html;
|
|
191
|
+
}
|
|
192
|
+
async function fetchMcmodJson(url, opts = {}, timeout = 15000) {
|
|
193
|
+
const text = await fetchMcmodText(url, opts, timeout);
|
|
194
|
+
return JSON.parse(text);
|
|
195
|
+
}
|