alemonjs 1.0.26 → 1.0.27
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/core/buffer.js +24 -0
- package/lib/core/puppeteer.js +6 -25
- package/lib/core/screenshot.js +0 -1
- package/package.json +72 -71
- package/types/core/buffer.d.ts +7 -0
- package/types/core/screenshot.d.ts +0 -1
package/lib/core/buffer.js
CHANGED
|
@@ -2,6 +2,7 @@ import { readFileSync } from 'fs';
|
|
|
2
2
|
import http from 'http';
|
|
3
3
|
import https from 'https';
|
|
4
4
|
import { join } from 'path';
|
|
5
|
+
import { createCanvas, loadImage } from 'canvas';
|
|
5
6
|
/**
|
|
6
7
|
* 异步请求图片
|
|
7
8
|
* @param url
|
|
@@ -36,3 +37,26 @@ export function getPathBuffer(path) {
|
|
|
36
37
|
const BufferImage = Buffer.from(image);
|
|
37
38
|
return BufferImage;
|
|
38
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* 压缩buffer图片
|
|
42
|
+
* @param img
|
|
43
|
+
* @param quality
|
|
44
|
+
* @returns
|
|
45
|
+
*/
|
|
46
|
+
export async function compressImage(img, quality) {
|
|
47
|
+
const image = await loadImage(img);
|
|
48
|
+
const canvas = createCanvas(image.width, image.height);
|
|
49
|
+
const ctx = canvas.getContext('2d');
|
|
50
|
+
ctx.drawImage(image, 0, 0);
|
|
51
|
+
ctx.canvas.toDataURL('image/jpeg', quality);
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
canvas.toBuffer((err, buf) => {
|
|
54
|
+
if (err) {
|
|
55
|
+
reject(err);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
resolve(buf);
|
|
59
|
+
}
|
|
60
|
+
}, 'image/jpeg');
|
|
61
|
+
});
|
|
62
|
+
}
|
package/lib/core/puppeteer.js
CHANGED
|
@@ -16,18 +16,6 @@ let browser;
|
|
|
16
16
|
* 实例控制
|
|
17
17
|
*/
|
|
18
18
|
let isBrowser = false;
|
|
19
|
-
/**
|
|
20
|
-
* 对每个页面进行缓存
|
|
21
|
-
*/
|
|
22
|
-
const pageCache = {};
|
|
23
|
-
/**
|
|
24
|
-
* 清除缓存
|
|
25
|
-
*/
|
|
26
|
-
function delCache() {
|
|
27
|
-
for (const item in pageCache) {
|
|
28
|
-
delete pageCache[item];
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
19
|
/**
|
|
32
20
|
* 实例配置
|
|
33
21
|
*/
|
|
@@ -72,7 +60,6 @@ export async function pupStartCheck() {
|
|
|
72
60
|
*/
|
|
73
61
|
pic = 0;
|
|
74
62
|
console.info('[puppeteer] close');
|
|
75
|
-
delCache();
|
|
76
63
|
isBrowser = false;
|
|
77
64
|
browser.close().catch(err => console.error(err));
|
|
78
65
|
console.info('[puppeteer] reopen');
|
|
@@ -149,25 +136,22 @@ export async function screenshotByUrl(val) {
|
|
|
149
136
|
return false;
|
|
150
137
|
}
|
|
151
138
|
const { url, time, rand, params, tab, cache } = val;
|
|
152
|
-
|
|
153
|
-
pageCache[url] = await browser.newPage();
|
|
154
|
-
}
|
|
139
|
+
const page = await browser.newPage();
|
|
155
140
|
const isurl = params == undefined ? url : `${url}?${queryString.stringify(params ?? {})}`;
|
|
156
141
|
/**
|
|
157
142
|
* 启用页面缓存
|
|
158
143
|
*/
|
|
159
|
-
await
|
|
144
|
+
await page.setCacheEnabled(cache == undefined ? true : cache);
|
|
160
145
|
/**
|
|
161
146
|
* 启动网页
|
|
162
147
|
*/
|
|
163
|
-
await
|
|
148
|
+
await page.goto(isurl);
|
|
164
149
|
console.info(`open ${isurl}`);
|
|
165
150
|
/**
|
|
166
151
|
* 找到元素
|
|
167
152
|
*/
|
|
168
|
-
const body = await
|
|
153
|
+
const body = await page.$(tab ?? 'body');
|
|
169
154
|
if (!body) {
|
|
170
|
-
delete pageCache[url];
|
|
171
155
|
console.error(`tab err`);
|
|
172
156
|
return false;
|
|
173
157
|
}
|
|
@@ -192,11 +176,10 @@ export async function screenshotByUrl(val) {
|
|
|
192
176
|
* 打印错误
|
|
193
177
|
*/
|
|
194
178
|
if (!buff) {
|
|
195
|
-
// 确保页面已经不再使用后再删除
|
|
196
|
-
await pageCache[url].close();
|
|
197
|
-
delete pageCache[url];
|
|
198
179
|
console.error(`buff err:${url}`);
|
|
199
180
|
}
|
|
181
|
+
// 确保页面已经不再使用后再删除
|
|
182
|
+
await page.close();
|
|
200
183
|
return buff;
|
|
201
184
|
}
|
|
202
185
|
/**
|
|
@@ -207,14 +190,12 @@ export async function startChrom() {
|
|
|
207
190
|
try {
|
|
208
191
|
browser = await puppeteer.launch(LaunchCfg);
|
|
209
192
|
isBrowser = true;
|
|
210
|
-
delCache();
|
|
211
193
|
console.info('[puppeteer] open success');
|
|
212
194
|
return true;
|
|
213
195
|
}
|
|
214
196
|
catch (err) {
|
|
215
197
|
console.error(err);
|
|
216
198
|
isBrowser = false;
|
|
217
|
-
delCache();
|
|
218
199
|
console.error('[puppeteer] open fail');
|
|
219
200
|
return false;
|
|
220
201
|
}
|
package/lib/core/screenshot.js
CHANGED
package/package.json
CHANGED
|
@@ -1,72 +1,73 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "alemonjs",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "docs https://alemonjs.com/",
|
|
5
|
-
"author": "ningmengchongshui",
|
|
6
|
-
"license": "GPL-2.0",
|
|
7
|
-
"type": "module",
|
|
8
|
-
"scripts": {
|
|
9
|
-
"tsc": "npx tsc",
|
|
10
|
-
"format": "prettier --write .",
|
|
11
|
-
"lint": "eslint . --ext .js,.ts --fix --ignore-path .gitignore"
|
|
12
|
-
},
|
|
13
|
-
"dependencies": {
|
|
14
|
-
"@discordjs/builders": "^1.6.3",
|
|
15
|
-
"@discordjs/core": "^0.6.0",
|
|
16
|
-
"@discordjs/rest": "^1.7.1",
|
|
17
|
-
"axios": "^1.4.0",
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"qq-
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"@types/
|
|
32
|
-
"@types/
|
|
33
|
-
"@types/
|
|
34
|
-
"@types/
|
|
35
|
-
"@
|
|
36
|
-
"@typescript-eslint/
|
|
37
|
-
"eslint": "^
|
|
38
|
-
"eslint
|
|
39
|
-
"eslint-
|
|
40
|
-
"prettier": "^2.
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"run.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
"
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
"
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "alemonjs",
|
|
3
|
+
"version": "1.0.27",
|
|
4
|
+
"description": "docs https://alemonjs.com/",
|
|
5
|
+
"author": "ningmengchongshui",
|
|
6
|
+
"license": "GPL-2.0",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"tsc": "npx tsc",
|
|
10
|
+
"format": "prettier --write .",
|
|
11
|
+
"lint": "eslint . --ext .js,.ts --fix --ignore-path .gitignore"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@discordjs/builders": "^1.6.3",
|
|
15
|
+
"@discordjs/core": "^0.6.0",
|
|
16
|
+
"@discordjs/rest": "^1.7.1",
|
|
17
|
+
"axios": "^1.4.0",
|
|
18
|
+
"canvas": "^2.11.2",
|
|
19
|
+
"discord.js": "^14.11.0",
|
|
20
|
+
"image-size": "^1.0.2",
|
|
21
|
+
"kook-ws": "^1.0.4",
|
|
22
|
+
"lodash": "^4.17.21",
|
|
23
|
+
"mys-villa": "^1.1.8",
|
|
24
|
+
"public-ip": "^6.0.1",
|
|
25
|
+
"puppeteer": "^20.7.1",
|
|
26
|
+
"qq-channel": "^1.0.0",
|
|
27
|
+
"qq-guild-bot": "^2.9.5",
|
|
28
|
+
"qrcode": "^1.5.3"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/lodash": "^4.14.186",
|
|
32
|
+
"@types/node": "^20.2.4",
|
|
33
|
+
"@types/progress": "^2.0.5",
|
|
34
|
+
"@types/qrcode": "^1.5.0",
|
|
35
|
+
"@types/ws": "^8.5.5",
|
|
36
|
+
"@typescript-eslint/eslint-plugin": "^5.60.0",
|
|
37
|
+
"@typescript-eslint/parser": "^5.60.0",
|
|
38
|
+
"eslint": "^8.43.0",
|
|
39
|
+
"eslint-config-prettier": "^8.8.0",
|
|
40
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
41
|
+
"prettier": "^2.8.8",
|
|
42
|
+
"ts-node": "^10.9.1",
|
|
43
|
+
"typescript": ">=5.0.4 <5.1.0"
|
|
44
|
+
},
|
|
45
|
+
"types": "types",
|
|
46
|
+
"main": "lib/index.js",
|
|
47
|
+
"files": [
|
|
48
|
+
"bin",
|
|
49
|
+
"lib",
|
|
50
|
+
"types",
|
|
51
|
+
"run.js",
|
|
52
|
+
"run.d.ts"
|
|
53
|
+
],
|
|
54
|
+
"exports": {
|
|
55
|
+
".": {
|
|
56
|
+
"import": "./lib/index.js",
|
|
57
|
+
"types": "./types/index.d.ts"
|
|
58
|
+
},
|
|
59
|
+
"./run": {
|
|
60
|
+
"import": "./run.js",
|
|
61
|
+
"types": "./run.d.ts"
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"bin": {
|
|
65
|
+
"alemonjs": "bin/main.js"
|
|
66
|
+
},
|
|
67
|
+
"publishConfig": {
|
|
68
|
+
"registry": "https://registry.npmjs.org"
|
|
69
|
+
},
|
|
70
|
+
"engines": {
|
|
71
|
+
"node": ">=16.14.0"
|
|
72
|
+
}
|
|
72
73
|
}
|
package/types/core/buffer.d.ts
CHANGED
|
@@ -11,3 +11,10 @@ export declare function getUrlbuffer(url: string): Promise<Buffer>;
|
|
|
11
11
|
* @returns
|
|
12
12
|
*/
|
|
13
13
|
export declare function getPathBuffer(path: string): Buffer;
|
|
14
|
+
/**
|
|
15
|
+
* 压缩buffer图片
|
|
16
|
+
* @param img
|
|
17
|
+
* @param quality
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
export declare function compressImage(img: Buffer, quality: number): Promise<Buffer>;
|