koishi-plugin-codec-tools 1.1.0 → 1.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.js +14 -65
- package/lib/locales/zh-CN.yml +0 -7
- package/package.json +2 -2
- package/readme.md +0 -3
- package/src/locales/zh-CN.yml +0 -7
package/lib/index.js
CHANGED
|
@@ -6,78 +6,27 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.Config = exports.using = exports.name = void 0;
|
|
7
7
|
exports.apply = apply;
|
|
8
8
|
const koishi_1 = require("koishi");
|
|
9
|
-
const jimp_1 = __importDefault(require("jimp"));
|
|
10
|
-
const jsqr_1 = __importDefault(require("jsqr"));
|
|
11
|
-
const library_1 = require("@zxing/library");
|
|
12
9
|
const fs_1 = __importDefault(require("fs"));
|
|
13
10
|
const path_1 = __importDefault(require("path"));
|
|
14
11
|
const yaml_1 = __importDefault(require("yaml"));
|
|
15
12
|
exports.name = 'codec-tools';
|
|
16
13
|
exports.using = ['i18n'];
|
|
17
14
|
exports.Config = koishi_1.Schema.object({}).i18n({ 'zh-CN': {} });
|
|
18
|
-
async function getImageUrl(session) {
|
|
19
|
-
let imageUrl = null;
|
|
20
|
-
session.elements.forEach(el => {
|
|
21
|
-
if (el.type === 'image' && el.attrs?.src) {
|
|
22
|
-
imageUrl = el.attrs.src;
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
if (imageUrl)
|
|
26
|
-
return imageUrl;
|
|
27
|
-
if (session.quote) {
|
|
28
|
-
try {
|
|
29
|
-
const msg = await session.bot.getMessage(session.channelId, session.quote.messageId);
|
|
30
|
-
msg.elements.forEach(el => {
|
|
31
|
-
if (el.type === 'image' && el.attrs?.src) {
|
|
32
|
-
imageUrl = el.attrs.src;
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
catch (e) { }
|
|
37
|
-
}
|
|
38
|
-
return imageUrl;
|
|
39
|
-
}
|
|
40
15
|
function apply(ctx, config) {
|
|
41
16
|
const zhCNPath = path_1.default.join(__dirname, './locales/zh-CN.yml');
|
|
42
|
-
const
|
|
17
|
+
const zhCNContent = fs_1.default.readFileSync(zhCNPath, 'utf8');
|
|
18
|
+
const zhCN = yaml_1.default.parse(zhCNContent);
|
|
43
19
|
ctx.i18n.define('zh-CN', zhCN);
|
|
44
|
-
ctx.command('url-encode <text:text>')
|
|
45
|
-
|
|
46
|
-
ctx.command('
|
|
47
|
-
|
|
48
|
-
ctx.command('
|
|
49
|
-
|
|
50
|
-
ctx.command('decode
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
try {
|
|
59
|
-
const buffer = await ctx.http.get(imageUrl, { responseType: 'arraybuffer' });
|
|
60
|
-
const img = await jimp_1.default.read(Buffer.from(buffer));
|
|
61
|
-
if (img.bitmap.width > 1000 || img.bitmap.height > 1000)
|
|
62
|
-
img.scaleToFit(800, 800);
|
|
63
|
-
img.contrast(0.2);
|
|
64
|
-
const imageData = { data: new Uint8ClampedArray(img.bitmap.data), width: img.bitmap.width, height: img.bitmap.height };
|
|
65
|
-
const qrResult = (0, jsqr_1.default)(imageData.data, imageData.width, imageData.height);
|
|
66
|
-
if (qrResult) {
|
|
67
|
-
await session.send(session.text('.qr-content', { content: qrResult.data }), { quote: session.messageId });
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
const hints = new Map();
|
|
71
|
-
hints.set(library_1.DecodeHintType.POSSIBLE_FORMATS, [library_1.BarcodeFormat.QR_CODE, library_1.BarcodeFormat.CODE_128, library_1.BarcodeFormat.EAN_13, library_1.BarcodeFormat.EAN_8, library_1.BarcodeFormat.UPC_A, library_1.BarcodeFormat.UPC_E, library_1.BarcodeFormat.CODE_39, library_1.BarcodeFormat.ITF]);
|
|
72
|
-
const reader = new library_1.MultiFormatReader();
|
|
73
|
-
const luminanceSource = new library_1.RGBLuminanceSource(imageData.data, imageData.width, imageData.height);
|
|
74
|
-
const binaryBitmap = new library_1.BinaryBitmap(new library_1.HybridBinarizer(luminanceSource));
|
|
75
|
-
const barcodeResult = reader.decode(binaryBitmap, hints);
|
|
76
|
-
await session.send(session.text('.barcode-content', { content: barcodeResult.text }), { quote: session.messageId });
|
|
77
|
-
}
|
|
78
|
-
catch (e) {
|
|
79
|
-
console.error(e);
|
|
80
|
-
await session.send(session.text('.no-code'), { quote: session.messageId });
|
|
81
|
-
}
|
|
82
|
-
});
|
|
20
|
+
ctx.command('url-encode <text:text>')
|
|
21
|
+
.action((_, text) => encodeURIComponent(text));
|
|
22
|
+
ctx.command('url-decode <text:text>')
|
|
23
|
+
.action((_, text) => decodeURIComponent(text));
|
|
24
|
+
ctx.command('base64-encode <text:text>')
|
|
25
|
+
.action((_, text) => Buffer.from(text).toString('base64'));
|
|
26
|
+
ctx.command('base64-decode <text:text>')
|
|
27
|
+
.action((_, text) => Buffer.from(text, 'base64').toString());
|
|
28
|
+
ctx.command('unicode-encode <text:text>')
|
|
29
|
+
.action((_, text) => text.split('').map(c => '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0')).join(''));
|
|
30
|
+
ctx.command('unicode-decode <text:text>')
|
|
31
|
+
.action((_, text) => text.replace(/\\u([0-9a-fA-F]{4})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))));
|
|
83
32
|
}
|
package/lib/locales/zh-CN.yml
CHANGED
|
@@ -11,12 +11,5 @@ commands:
|
|
|
11
11
|
description: Unicode 编码
|
|
12
12
|
unicode-decode:
|
|
13
13
|
description: Unicode 解码
|
|
14
|
-
decode-code:
|
|
15
|
-
description: 识别二维码/条形码
|
|
16
|
-
messages:
|
|
17
|
-
no-image: 未找到图片!请发送图片或引用包含图片的消息后重试。
|
|
18
|
-
qr-content: 二维码内容:{{content}}
|
|
19
|
-
barcode-content: 条形码内容:{{content}}
|
|
20
|
-
no-code: 未识别到二维码/条形码!
|
|
21
14
|
|
|
22
15
|
_config: {}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koishi-plugin-codec-tools",
|
|
3
|
-
"description": "编码&解码 URL/Base64/Unicode
|
|
4
|
-
"version": "1.1.
|
|
3
|
+
"description": "编码&解码 URL/Base64/Unicode的 Koishi 插件",
|
|
4
|
+
"version": "1.1.2",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
7
7
|
"files": [
|
package/readme.md
CHANGED
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
- 支持 URL 编码/解码
|
|
8
8
|
- 支持 Base64 编码/解码
|
|
9
9
|
- 支持 Unicode 编码/解码
|
|
10
|
-
- 支持从图片中解析二维码/条形码内容
|
|
11
10
|
|
|
12
11
|
### English
|
|
13
12
|
This is an encoding/decoding tool plugin developed for the Koishi chatbot framework. This plugin has the following features:
|
|
@@ -27,7 +26,6 @@ This is an encoding/decoding tool plugin developed for the Koishi chatbot framew
|
|
|
27
26
|
| base64-decode <文本> | 对指定 Base64 编码文本进行解码 |
|
|
28
27
|
| unicode-encode <文本> | 对指定文本进行 Unicode 编码 |
|
|
29
28
|
| unicode-decode <文本> | 对指定 Unicode 编码文本进行解码 |
|
|
30
|
-
| decode-code | 解析附带图片中的二维码 / 条形码内容 |
|
|
31
29
|
|
|
32
30
|
### English
|
|
33
31
|
| Command | Description |
|
|
@@ -38,7 +36,6 @@ This is an encoding/decoding tool plugin developed for the Koishi chatbot framew
|
|
|
38
36
|
| base64-decode <text> | Decode the specified Base64-encoded text |
|
|
39
37
|
| unicode-encode <text> | Encode the specified text with Unicode encoding |
|
|
40
38
|
| unicode-decode <text> | Decode the specified Unicode-encoded text |
|
|
41
|
-
| decode-code | Parse QR code/barcode content from the attached image |
|
|
42
39
|
|
|
43
40
|
## 项目贡献者 (Contributors)
|
|
44
41
|
| 贡献者 (Contributor) | 贡献内容 (Contribution) |
|
package/src/locales/zh-CN.yml
CHANGED
|
@@ -11,12 +11,5 @@ commands:
|
|
|
11
11
|
description: Unicode 编码
|
|
12
12
|
unicode-decode:
|
|
13
13
|
description: Unicode 解码
|
|
14
|
-
decode-code:
|
|
15
|
-
description: 识别二维码/条形码
|
|
16
|
-
messages:
|
|
17
|
-
no-image: 未找到图片!请发送图片或引用包含图片的消息后重试。
|
|
18
|
-
qr-content: 二维码内容:{{content}}
|
|
19
|
-
barcode-content: 条形码内容:{{content}}
|
|
20
|
-
no-code: 未识别到二维码/条形码!
|
|
21
14
|
|
|
22
15
|
_config: {}
|