@mytools-all/mytools-ocr 0.0.0-stage → 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 MyTools Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,21 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ # 讯飞 OCR
2
+
3
+ 讯飞 OCR
4
+
5
+ 版本:0.1.0;适用于 MyTools,不能作为独立网页调用宿主能力。
6
+
7
+ ## 安装
8
+
9
+ 在 MyTools 插件管理的 npm 安装入口输入 `@mytools-all/mytools-ocr@0.1.0`。也可以下载本仓库,在开发者页面选择“安装本地目录”。
10
+
11
+ ## 开发
12
+
13
+ 直接编辑本目录的 HTML、JavaScript 和样式,无需依赖宿主源码或其他插件仓库。修改后重新安装本地目录。运行 `npm test` 检查包内契约和逻辑。插件 API 规范见 [mytools](https://github.com/mytools-all/mytools/tree/main/docs)。
14
+
15
+ ## 权限
16
+
17
+ 声明的宿主能力:net.fetch、storage.get、storage.set、storage.del、clipboard.write。目录和危险操作仍需用户授权。
18
+
19
+ ## 许可
20
+
21
+ 项目代码使用 MIT;随附的第三方代码遵循各自随附的许可证。
package/THIRD_PARTY.md ADDED
@@ -0,0 +1,25 @@
1
+ # 离线依赖与许可
2
+
3
+ 这些库只用于插件页面,许可证随每个使用它的独立包分发。资源随各插件独立维护。
4
+ 不复制或分发 Rubick 参考插件的实现或图标。
5
+
6
+ | 库 | 版本 | 用途 | 上游 | 许可证 |
7
+ |---|---|---|---|---|
8
+ | marked | 18.1.2 | Markdown 解析 | https://github.com/markedjs/marked | MIT,marked.LICENSE |
9
+ | DOMPurify | 3.4.17 | Markdown HTML 清洗 | https://github.com/cure53/DOMPurify | Apache-2.0 OR MPL-2.0,purify.LICENSE |
10
+ | js-yaml | 5.5.0 | JSON / YAML 转换 | https://github.com/nodeca/js-yaml | MIT,yaml.LICENSE |
11
+ | js-md5 | 0.9.2 | MD5 与讯飞 WebAPI 签名 | https://github.com/emn178/js-md5 | MIT,md5.LICENSE |
12
+
13
+ 各库版权与完整条款以对应 LICENSE 文件为准。依赖本地载入,不从 CDN 动态执行代码。
14
+
15
+ ## 优先修复使用的编辑器组件
16
+
17
+ | 组件 | 版本 | 来源 | 许可证 |
18
+ |---|---|---|---|
19
+ | md-editor-v3 | 2.11.3 | https://github.com/imzbf/md-editor-v3 | MIT,md-editor.LICENSE |
20
+ | Vue | 3.5.42 | https://github.com/vuejs/core | MIT,vue.LICENSE |
21
+ | Cropper.js | 1.5.13 | https://github.com/fengyuanchen/cropperjs | MIT,cropper.LICENSE |
22
+ | screenfull | 5.2.0 | https://github.com/sindresorhus/screenfull | MIT,screenfull.LICENSE |
23
+ | highlight.js | 11.7.0 | https://github.com/highlightjs/highlight.js | BSD-3-Clause,highlight.LICENSE |
24
+
25
+ 这些资源由 Markdown 包本地携带。`md-editor.mjs` 仅将 Vue 导入改为本地 `./vue.mjs`;图标脚本为该系列编辑器的默认图标资源。其余编辑器扩展通过本地实例注入,不在运行时向 CDN 请求脚本。
package/app.mjs ADDED
@@ -0,0 +1,202 @@
1
+ import {
2
+ $,
3
+ $$,
4
+ action,
5
+ copy,
6
+ status,
7
+ ready,
8
+ call,
9
+ download,
10
+ } from "./common.mjs";
11
+ import md5 from "./md5.mjs";
12
+ let image = "",
13
+ fileName = "",
14
+ rawText = "",
15
+ history = [],
16
+ busy = false;
17
+ function tab(id) {
18
+ for (const section of ["recognize", "settings", "history"])
19
+ $("#" + section).classList.toggle("hidden", section !== id);
20
+ for (const name of ["recognize", "settings", "history"])
21
+ $("#" + name + "-tab").classList.toggle("active", name === id);
22
+ if (id === "history") renderHistory();
23
+ }
24
+ for (const name of ["recognize", "settings", "history"])
25
+ $("#" + name + "-tab").onclick = () => tab(name);
26
+ function settings() {
27
+ return {
28
+ appid: $("#appid").value.trim(),
29
+ key: $("#key").value.trim(),
30
+ mode: $("#mode").value,
31
+ };
32
+ }
33
+ function populate(c) {
34
+ $("#appid").value = c.appid || "";
35
+ $("#key").value = c.key || "";
36
+ $("#mode").value = c.mode || "print";
37
+ }
38
+ async function loadFile(file) {
39
+ if (!file) return;
40
+ if (!["image/png", "image/jpeg", "image/bmp"].includes(file.type))
41
+ throw Error("仅支持 JPG / PNG / BMP 图片");
42
+ if (file.size > 3 * 1024 * 1024) throw Error("图片不能超过 3 MB");
43
+ image = await new Promise((resolve, reject) => {
44
+ const r = new FileReader();
45
+ r.onload = () => resolve(r.result);
46
+ r.onerror = () => reject(Error("图片读取失败"));
47
+ r.readAsDataURL(file);
48
+ });
49
+ fileName = file.name || "粘贴的图片";
50
+ $("#image").src = image;
51
+ $("#image").hidden = false;
52
+ tab("recognize");
53
+ status(fileName + " · " + Math.round(file.size / 1024) + " KB");
54
+ }
55
+ $("#choose").onclick = () => $("#file").click();
56
+ $("#file").onchange = action(() => loadFile($("#file").files[0]));
57
+ $("#drop").ondragover = (e) => {
58
+ e.preventDefault();
59
+ $("#drop").classList.add("over");
60
+ };
61
+ $("#drop").ondragleave = () => $("#drop").classList.remove("over");
62
+ $("#drop").ondrop = action(async (e) => {
63
+ e.preventDefault();
64
+ $("#drop").classList.remove("over");
65
+ await loadFile(e.dataTransfer.files[0]);
66
+ });
67
+ document.addEventListener(
68
+ "paste",
69
+ action(async (e) => {
70
+ const item = [...e.clipboardData.items].find((i) =>
71
+ i.type.startsWith("image/"),
72
+ );
73
+ if (item) {
74
+ e.preventDefault();
75
+ await loadFile(item.getAsFile());
76
+ }
77
+ }),
78
+ );
79
+ $("#save").onclick = action(async () => {
80
+ const c = settings();
81
+ if (!c.appid || !c.key) throw Error("请填写 App ID 和 API Key");
82
+ if ($("#remember").checked)
83
+ await call("storage.set", { key: "credentials", value: c });
84
+ else await call("storage.del", { key: "credentials" });
85
+ status($("#remember").checked ? "设置已保存在本机" : "设置仅保留到关闭插件");
86
+ tab("recognize");
87
+ });
88
+ $("#forget").onclick = action(async () => {
89
+ await call("storage.del", { key: "credentials" });
90
+ populate({});
91
+ $("#remember").checked = false;
92
+ status("已清除本机保存的凭据");
93
+ });
94
+ function resultText(response) {
95
+ const blocks = response.data?.block || [];
96
+ return blocks
97
+ .flatMap((b) =>
98
+ (b.line || []).map((l) =>
99
+ (l.word || []).map((w) => w.content || "").join(""),
100
+ ),
101
+ )
102
+ .join("\n");
103
+ }
104
+ $("#run").onclick = action(async () => {
105
+ const c = settings();
106
+ if (!c.appid || !c.key) {
107
+ tab("settings");
108
+ throw Error("请先配置自己的讯飞 OCR WebAPI 凭据");
109
+ }
110
+ if (!image) throw Error("请先选择或粘贴图片");
111
+ if (busy) return;
112
+ busy = true;
113
+ $("#run").disabled = true;
114
+ try {
115
+ status("正在识别…");
116
+ const time = String(Math.floor(Date.now() / 1000));
117
+ const param = btoa(
118
+ JSON.stringify({ language: "cn|en", location: "false" }),
119
+ );
120
+ const response = await call("net.fetch", {
121
+ url:
122
+ "https://webapi.xfyun.cn/v1/service/v1/ocr/" +
123
+ (c.mode === "handwrite" ? "handwriting" : "general"),
124
+ method: "POST",
125
+ headers: {
126
+ "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
127
+ "X-Appid": c.appid,
128
+ "X-CurTime": time,
129
+ "X-Param": param,
130
+ "X-CheckSum": md5(c.key + time + param),
131
+ },
132
+ body: "image=" + encodeURIComponent(image.split(",")[1]),
133
+ });
134
+ if (response.status !== 200)
135
+ throw Error("讯飞服务返回 HTTP " + response.status);
136
+ let result;
137
+ try {
138
+ result = JSON.parse(response.body);
139
+ } catch {
140
+ throw Error("讯飞响应不是有效 JSON");
141
+ }
142
+ if (String(result.code) !== "0")
143
+ throw Error(
144
+ "识别失败(" +
145
+ result.code +
146
+ "):" +
147
+ (result.desc || result.message || "请检查接口权限与凭据"),
148
+ );
149
+ rawText = resultText(result);
150
+ if (!rawText) throw Error("未识别到文字,请检查图片清晰度");
151
+ $("#result").value = $("#merge").checked
152
+ ? rawText.replace(/\n/g, "")
153
+ : rawText;
154
+ history = [
155
+ { name: fileName, time: Date.now(), text: rawText },
156
+ ...history,
157
+ ].slice(0, 30);
158
+ status("识别完成 · " + rawText.length + " 字符");
159
+ } finally {
160
+ busy = false;
161
+ $("#run").disabled = false;
162
+ }
163
+ });
164
+ $("#merge").onchange = () => {
165
+ $("#result").value = $("#merge").checked
166
+ ? $("#result").value.replace(/\n/g, "")
167
+ : rawText;
168
+ };
169
+ $("#copy").onclick = action(() => copy($("#result").value));
170
+ $("#export").onclick = () => download("识别结果.txt", $("#result").value);
171
+ function renderHistory() {
172
+ $("#history").replaceChildren();
173
+ if (!history.length) {
174
+ $("#history").textContent = "本次会话还没有识别记录;关闭插件后清除。";
175
+ return;
176
+ }
177
+ for (const entry of history) {
178
+ const b = document.createElement("button");
179
+ b.style.cssText =
180
+ "display:block;width:100%;text-align:left;margin-bottom:8px;white-space:normal";
181
+ b.textContent =
182
+ entry.name +
183
+ " · " +
184
+ new Date(entry.time).toLocaleString() +
185
+ "\n" +
186
+ entry.text.slice(0, 100);
187
+ b.onclick = () => {
188
+ rawText = entry.text;
189
+ $("#result").value = entry.text;
190
+ tab("recognize");
191
+ };
192
+ $("#history").append(b);
193
+ }
194
+ }
195
+ ready(async () => {
196
+ const c = await call("storage.get", { key: "credentials" });
197
+ if (c) {
198
+ populate(c);
199
+ $("#remember").checked = true;
200
+ }
201
+ status("请选择图片 · 需要自行开通讯飞印刷 / 手写文字识别 WebAPI");
202
+ });
package/common.mjs ADDED
@@ -0,0 +1,69 @@
1
+ export const $ = (selector) => document.querySelector(selector);
2
+ export const $$ = (selector) => [...document.querySelectorAll(selector)];
3
+ export function status(text, error = false) {
4
+ const el = $("#status");
5
+ if (el) {
6
+ el.textContent = text;
7
+ el.dataset.error = String(error);
8
+ }
9
+ }
10
+ export function action(fn) {
11
+ return async (...args) => {
12
+ try {
13
+ return await fn(...args);
14
+ } catch (e) {
15
+ status(e.message || String(e), true);
16
+ }
17
+ };
18
+ }
19
+ export async function call(id, args = {}) {
20
+ if (!window.mytools) throw Error("请从 MyTools 打开此工具");
21
+ return window.mytools.call(id, args);
22
+ }
23
+ export const copy = async (text) => {
24
+ await call("clipboard.write", { text: String(text) });
25
+ status("已复制到剪贴板");
26
+ };
27
+ export function ready(fn) {
28
+ if (window.mytools) window.mytools.onPluginReady(action(fn));
29
+ else status("预览模式:系统能力需从 MyTools 使用");
30
+ }
31
+ export const escapeHtml = (text) =>
32
+ String(text).replace(
33
+ /[&<>"']/g,
34
+ (c) =>
35
+ ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[
36
+ c
37
+ ],
38
+ );
39
+ export function download(name, text, type = "text/plain") {
40
+ const url = URL.createObjectURL(new Blob([text], { type }));
41
+ const a = document.createElement("a");
42
+ a.href = url;
43
+ a.download = name;
44
+ a.click();
45
+ setTimeout(() => URL.revokeObjectURL(url), 1000);
46
+ }
47
+ export function debounce(fn, ms = 200) {
48
+ let timer;
49
+ return (...args) => {
50
+ clearTimeout(timer);
51
+ timer = setTimeout(() => fn(...args), ms);
52
+ };
53
+ }
54
+ export const formatBytes = (n) =>
55
+ n < 1024
56
+ ? n + " B"
57
+ : n < 1048576
58
+ ? (n / 1024).toFixed(1) + " KB"
59
+ : (n / 1048576).toFixed(1) + " MB";
60
+ const savedTheme = localStorage.getItem("tool-theme");
61
+ document.documentElement.dataset.theme =
62
+ savedTheme ||
63
+ (matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light");
64
+ $("#theme")?.addEventListener("click", () => {
65
+ const theme =
66
+ document.documentElement.dataset.theme === "dark" ? "light" : "dark";
67
+ document.documentElement.dataset.theme = theme;
68
+ localStorage.setItem("tool-theme", theme);
69
+ });
package/index.html ADDED
@@ -0,0 +1,104 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width,initial-scale=1" />
6
+ <title>讯飞 OCR</title>
7
+ <link rel="stylesheet" href="tool.css" />
8
+ <style>
9
+ #drop {
10
+ border: 2px dashed var(--border);
11
+ text-align: center;
12
+ padding: 24px;
13
+ }
14
+ #drop.over {
15
+ border-color: var(--accent);
16
+ background: var(--soft);
17
+ }
18
+ </style>
19
+ </head>
20
+ <body>
21
+ <main>
22
+ <div class="toolbar">
23
+ <strong>讯飞 OCR</strong>
24
+ <div class="tabs">
25
+ <button id="recognize-tab" class="active">文字识别</button
26
+ ><button id="settings-tab">接口设置</button
27
+ ><button id="history-tab">识别历史</button>
28
+ </div>
29
+ <span class="spacer"></span><button id="theme">◐</button>
30
+ </div>
31
+ <section
32
+ id="recognize"
33
+ style="display: flex; flex-direction: column; flex: 1; min-height: 0"
34
+ >
35
+ <div class="split">
36
+ <div class="content stack">
37
+ <div id="drop" class="panel" tabindex="0">
38
+ <p>拖拽图片到这里,或 Ctrl+V 粘贴图片</p>
39
+ <button id="choose">选择图片</button
40
+ ><input
41
+ id="file"
42
+ type="file"
43
+ accept="image/png,image/jpeg,image/bmp"
44
+ hidden
45
+ />
46
+ <p class="muted">JPG / PNG / BMP · 最大 3 MB</p>
47
+ <img
48
+ id="image"
49
+ style="max-width: 100%; max-height: 230px"
50
+ hidden
51
+ />
52
+ </div>
53
+ <button id="run" class="primary">开始识别</button>
54
+ </div>
55
+ <textarea
56
+ id="result"
57
+ class="editor"
58
+ placeholder="识别结果将在这里显示,可直接编辑…"
59
+ aria-label="OCR 识别结果"
60
+ ></textarea>
61
+ </div>
62
+ <div class="toolbar">
63
+ <label><input id="merge" type="checkbox" />合并行</label
64
+ ><span class="spacer"></span><button id="copy">复制文字</button
65
+ ><button id="export">导出 TXT</button>
66
+ </div>
67
+ </section>
68
+ <section id="settings" class="content stack hidden">
69
+ <strong>讯飞通用文字识别 WebAPI</strong>
70
+ <p class="muted">
71
+ 图片仅在点击「开始识别」后上传至讯飞。请填写自己的接口凭据。
72
+ </p>
73
+ <label
74
+ >App ID <input id="appid" autocomplete="off" style="flex: 1" /></label
75
+ ><label
76
+ >API Key
77
+ <input
78
+ id="key"
79
+ type="password"
80
+ autocomplete="off"
81
+ style="flex: 1" /></label
82
+ ><label
83
+ >识别模式
84
+ <select id="mode">
85
+ <option value="print">印刷文字识别(WebAPI)</option>
86
+ <option value="handwrite">手写文字识别</option>
87
+ </select></label
88
+ ><label
89
+ ><input
90
+ id="remember"
91
+ type="checkbox"
92
+ />将凭据保存在本机(插件存储为明文)</label
93
+ >
94
+ <div class="row">
95
+ <button id="save" class="primary">保存设置</button
96
+ ><button id="forget">清除保存的凭据</button>
97
+ </div>
98
+ </section>
99
+ <section id="history" class="content hidden"></section>
100
+ <footer id="status" class="status" role="status"></footer>
101
+ </main>
102
+ <script type="module" src="app.mjs"></script>
103
+ </body>
104
+ </html>
package/md5.LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014-2026 Chen, Yi-Cyuan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/md5.mjs ADDED
@@ -0,0 +1,10 @@
1
+ function t(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var r,e={exports:{}};
2
+ /**
3
+ * [js-md5]{@link https://github.com/emn178/js-md5}
4
+ *
5
+ * @namespace md5
6
+ * @version 0.9.2
7
+ * @author Chen, Yi-Cyuan [emn178@gmail.com]
8
+ * @copyright Chen, Yi-Cyuan 2014-2026
9
+ * @license MIT
10
+ */r=e,function(){var t="input is invalid type",e="object"==typeof window,i=e?window:{};i.JS_MD5_NO_WINDOW&&(e=!1),"undefined"!=typeof WorkerGlobalScope&&"undefined"!=typeof self&&self instanceof WorkerGlobalScope&&(i=self);var h=!i.JS_MD5_NO_COMMON_JS&&r.exports,s=!i.JS_MD5_NO_ARRAY_BUFFER&&"undefined"!=typeof ArrayBuffer,a="0123456789abcdef".split(""),n=[128,32768,8388608,-2147483648],o=[0,8,16,24],f=["hex","array","digest","arrayBuffer"];f.push("buffer"),f.push("base64");var u,p="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split(""),y=[];if(s){var c=new ArrayBuffer(68);u=new Uint8Array(c),y=new Uint32Array(c)}var l=Array.isArray;!i.JS_MD5_NO_NODE_JS&&l||(l=function(t){return"[object Array]"===Object.prototype.toString.call(t)});var d=ArrayBuffer.isView;!s||!i.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW&&d||(d=function(t){return"object"==typeof t&&t.buffer&&t.buffer.constructor===ArrayBuffer});var b=function(r){var e=typeof r;if("string"===e)return[r,!0];if("object"!==e||null===r)throw new Error(t);if(s&&r.constructor===ArrayBuffer)return[new Uint8Array(r),!1];if(!l(r)&&!d(r))throw new Error(t);return[r,!1]},v=function(t){return function(r){return new A(!0).update(r)[t]()}},w=function(t){return function(r,e){return new _(r,!0).update(e)[t]()}};function A(t){if(t)y[0]=y[16]=y[1]=y[2]=y[3]=y[4]=y[5]=y[6]=y[7]=y[8]=y[9]=y[10]=y[11]=y[12]=y[13]=y[14]=y[15]=0,this.blocks=y,this.buffer8=u;else if(s){var r=new ArrayBuffer(68);this.buffer8=new Uint8Array(r),this.blocks=new Uint32Array(r)}else this.blocks=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];this.h0=this.h1=this.h2=this.h3=this.start=this.bytes=this.hBytes=0,this.finalized=this.hashed=!1,this.first=!0}function _(t,r){var e,i=b(t);if(t=i[0],i[1]){var h,s=[],a=t.length,n=0;for(e=0;e<a;++e)(h=t.charCodeAt(e))<128?s[n++]=h:h<2048?(s[n++]=192|h>>>6,s[n++]=128|63&h):h<55296||h>=57344?(s[n++]=224|h>>>12,s[n++]=128|h>>>6&63,s[n++]=128|63&h):(h=65536+((1023&h)<<10|1023&t.charCodeAt(++e)),s[n++]=240|h>>>18,s[n++]=128|h>>>12&63,s[n++]=128|h>>>6&63,s[n++]=128|63&h);t=s}t.length>64&&(t=new A(!0).update(t).array());var o=[],f=[];for(e=0;e<64;++e){var u=t[e]||0;o[e]=92^u,f[e]=54^u}A.call(this,r),this.update(f),this.oKeyPad=o,this.inner=!0,this.sharedMemory=r}A.prototype.update=function(t){if(this.finalized)throw new Error("finalize already called");var r=b(t);t=r[0];for(var e,i,h=r[1],a=0,n=t.length,f=this.blocks,u=this.buffer8;a<n;){if(this.hashed&&(this.hashed=!1,f[0]=f[16],f[16]=f[1]=f[2]=f[3]=f[4]=f[5]=f[6]=f[7]=f[8]=f[9]=f[10]=f[11]=f[12]=f[13]=f[14]=f[15]=0),h)if(s)for(i=this.start;a<n&&i<64;++a)(e=t.charCodeAt(a))<128?u[i++]=e:e<2048?(u[i++]=192|e>>>6,u[i++]=128|63&e):e<55296||e>=57344?(u[i++]=224|e>>>12,u[i++]=128|e>>>6&63,u[i++]=128|63&e):(e=65536+((1023&e)<<10|1023&t.charCodeAt(++a)),u[i++]=240|e>>>18,u[i++]=128|e>>>12&63,u[i++]=128|e>>>6&63,u[i++]=128|63&e);else for(i=this.start;a<n&&i<64;++a)(e=t.charCodeAt(a))<128?f[i>>>2]|=e<<o[3&i++]:e<2048?(f[i>>>2]|=(192|e>>>6)<<o[3&i++],f[i>>>2]|=(128|63&e)<<o[3&i++]):e<55296||e>=57344?(f[i>>>2]|=(224|e>>>12)<<o[3&i++],f[i>>>2]|=(128|e>>>6&63)<<o[3&i++],f[i>>>2]|=(128|63&e)<<o[3&i++]):(e=65536+((1023&e)<<10|1023&t.charCodeAt(++a)),f[i>>>2]|=(240|e>>>18)<<o[3&i++],f[i>>>2]|=(128|e>>>12&63)<<o[3&i++],f[i>>>2]|=(128|e>>>6&63)<<o[3&i++],f[i>>>2]|=(128|63&e)<<o[3&i++]);else if(s)for(i=this.start;a<n&&i<64;++a)u[i++]=t[a];else for(i=this.start;a<n&&i<64;++a)f[i>>>2]|=t[a]<<o[3&i++];this.lastByteIndex=i,this.bytes+=i-this.start,i>=64?(this.start=i-64,this.hash(),this.hashed=!0):this.start=i}return this.bytes>4294967295&&(this.hBytes+=this.bytes/4294967296|0,this.bytes=this.bytes%4294967296),this},A.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,r=this.lastByteIndex;t[r>>>2]|=n[3&r],r>=56&&(this.hashed||this.hash(),t[0]=t[16],t[16]=t[1]=t[2]=t[3]=t[4]=t[5]=t[6]=t[7]=t[8]=t[9]=t[10]=t[11]=t[12]=t[13]=t[14]=t[15]=0),t[14]=this.bytes<<3,t[15]=this.hBytes<<3|this.bytes>>>29,this.hash()}},A.prototype.hash=function(){var t,r,e,i,h,s,a=this.blocks;this.first?r=((r=((t=((t=a[0]-680876937)<<7|t>>>25)-271733879|0)^(e=((e=(-271733879^(i=((i=(-1732584194^2004318071&t)+a[1]-117830708)<<12|i>>>20)+t|0)&(-271733879^t))+a[2]-1126478375)<<17|e>>>15)+i|0)&(i^t))+a[3]-1316259209)<<22|r>>>10)+e|0:(t=this.h0,r=this.h1,e=this.h2,r=((r+=((t=((t+=((i=this.h3)^r&(e^i))+a[0]-680876936)<<7|t>>>25)+r|0)^(e=((e+=(r^(i=((i+=(e^t&(r^e))+a[1]-389564586)<<12|i>>>20)+t|0)&(t^r))+a[2]+606105819)<<17|e>>>15)+i|0)&(i^t))+a[3]-1044525330)<<22|r>>>10)+e|0),r=((r+=((t=((t+=(i^r&(e^i))+a[4]-176418897)<<7|t>>>25)+r|0)^(e=((e+=(r^(i=((i+=(e^t&(r^e))+a[5]+1200080426)<<12|i>>>20)+t|0)&(t^r))+a[6]-1473231341)<<17|e>>>15)+i|0)&(i^t))+a[7]-45705983)<<22|r>>>10)+e|0,r=((r+=((t=((t+=(i^r&(e^i))+a[8]+1770035416)<<7|t>>>25)+r|0)^(e=((e+=(r^(i=((i+=(e^t&(r^e))+a[9]-1958414417)<<12|i>>>20)+t|0)&(t^r))+a[10]-42063)<<17|e>>>15)+i|0)&(i^t))+a[11]-1990404162)<<22|r>>>10)+e|0,r=((r+=((t=((t+=(i^r&(e^i))+a[12]+1804603682)<<7|t>>>25)+r|0)^(e=((e+=(r^(i=((i+=(e^t&(r^e))+a[13]-40341101)<<12|i>>>20)+t|0)&(t^r))+a[14]-1502002290)<<17|e>>>15)+i|0)&(i^t))+a[15]+1236535329)<<22|r>>>10)+e|0,r=((r+=((i=((i+=(r^e&((t=((t+=(e^i&(r^e))+a[1]-165796510)<<5|t>>>27)+r|0)^r))+a[6]-1069501632)<<9|i>>>23)+t|0)^t&((e=((e+=(t^r&(i^t))+a[11]+643717713)<<14|e>>>18)+i|0)^i))+a[0]-373897302)<<20|r>>>12)+e|0,r=((r+=((i=((i+=(r^e&((t=((t+=(e^i&(r^e))+a[5]-701558691)<<5|t>>>27)+r|0)^r))+a[10]+38016083)<<9|i>>>23)+t|0)^t&((e=((e+=(t^r&(i^t))+a[15]-660478335)<<14|e>>>18)+i|0)^i))+a[4]-405537848)<<20|r>>>12)+e|0,r=((r+=((i=((i+=(r^e&((t=((t+=(e^i&(r^e))+a[9]+568446438)<<5|t>>>27)+r|0)^r))+a[14]-1019803690)<<9|i>>>23)+t|0)^t&((e=((e+=(t^r&(i^t))+a[3]-187363961)<<14|e>>>18)+i|0)^i))+a[8]+1163531501)<<20|r>>>12)+e|0,r=((r+=((i=((i+=(r^e&((t=((t+=(e^i&(r^e))+a[13]-1444681467)<<5|t>>>27)+r|0)^r))+a[2]-51403784)<<9|i>>>23)+t|0)^t&((e=((e+=(t^r&(i^t))+a[7]+1735328473)<<14|e>>>18)+i|0)^i))+a[12]-1926607734)<<20|r>>>12)+e|0,r=((r+=((s=(i=((i+=((h=r^e)^(t=((t+=(h^i)+a[5]-378558)<<4|t>>>28)+r|0))+a[8]-2022574463)<<11|i>>>21)+t|0)^t)^(e=((e+=(s^r)+a[11]+1839030562)<<16|e>>>16)+i|0))+a[14]-35309556)<<23|r>>>9)+e|0,r=((r+=((s=(i=((i+=((h=r^e)^(t=((t+=(h^i)+a[1]-1530992060)<<4|t>>>28)+r|0))+a[4]+1272893353)<<11|i>>>21)+t|0)^t)^(e=((e+=(s^r)+a[7]-155497632)<<16|e>>>16)+i|0))+a[10]-1094730640)<<23|r>>>9)+e|0,r=((r+=((s=(i=((i+=((h=r^e)^(t=((t+=(h^i)+a[13]+681279174)<<4|t>>>28)+r|0))+a[0]-358537222)<<11|i>>>21)+t|0)^t)^(e=((e+=(s^r)+a[3]-722521979)<<16|e>>>16)+i|0))+a[6]+76029189)<<23|r>>>9)+e|0,r=((r+=((s=(i=((i+=((h=r^e)^(t=((t+=(h^i)+a[9]-640364487)<<4|t>>>28)+r|0))+a[12]-421815835)<<11|i>>>21)+t|0)^t)^(e=((e+=(s^r)+a[15]+530742520)<<16|e>>>16)+i|0))+a[2]-995338651)<<23|r>>>9)+e|0,r=((r+=((i=((i+=(r^((t=((t+=(e^(r|~i))+a[0]-198630844)<<6|t>>>26)+r|0)|~e))+a[7]+1126891415)<<10|i>>>22)+t|0)^((e=((e+=(t^(i|~r))+a[14]-1416354905)<<15|e>>>17)+i|0)|~t))+a[5]-57434055)<<21|r>>>11)+e|0,r=((r+=((i=((i+=(r^((t=((t+=(e^(r|~i))+a[12]+1700485571)<<6|t>>>26)+r|0)|~e))+a[3]-1894986606)<<10|i>>>22)+t|0)^((e=((e+=(t^(i|~r))+a[10]-1051523)<<15|e>>>17)+i|0)|~t))+a[1]-2054922799)<<21|r>>>11)+e|0,r=((r+=((i=((i+=(r^((t=((t+=(e^(r|~i))+a[8]+1873313359)<<6|t>>>26)+r|0)|~e))+a[15]-30611744)<<10|i>>>22)+t|0)^((e=((e+=(t^(i|~r))+a[6]-1560198380)<<15|e>>>17)+i|0)|~t))+a[13]+1309151649)<<21|r>>>11)+e|0,r=((r+=((i=((i+=(r^((t=((t+=(e^(r|~i))+a[4]-145523070)<<6|t>>>26)+r|0)|~e))+a[11]-1120210379)<<10|i>>>22)+t|0)^((e=((e+=(t^(i|~r))+a[2]+718787259)<<15|e>>>17)+i|0)|~t))+a[9]-343485551)<<21|r>>>11)+e|0,this.first?(this.h0=t+1732584193|0,this.h1=r-271733879|0,this.h2=e-1732584194|0,this.h3=i+271733878|0,this.first=!1):(this.h0=this.h0+t|0,this.h1=this.h1+r|0,this.h2=this.h2+e|0,this.h3=this.h3+i|0)},A.prototype.hex=function(){this.finalize();var t=this.h0,r=this.h1,e=this.h2,i=this.h3;return a[t>>>4&15]+a[15&t]+a[t>>>12&15]+a[t>>>8&15]+a[t>>>20&15]+a[t>>>16&15]+a[t>>>28&15]+a[t>>>24&15]+a[r>>>4&15]+a[15&r]+a[r>>>12&15]+a[r>>>8&15]+a[r>>>20&15]+a[r>>>16&15]+a[r>>>28&15]+a[r>>>24&15]+a[e>>>4&15]+a[15&e]+a[e>>>12&15]+a[e>>>8&15]+a[e>>>20&15]+a[e>>>16&15]+a[e>>>28&15]+a[e>>>24&15]+a[i>>>4&15]+a[15&i]+a[i>>>12&15]+a[i>>>8&15]+a[i>>>20&15]+a[i>>>16&15]+a[i>>>28&15]+a[i>>>24&15]},A.prototype.toString=A.prototype.hex,A.prototype.digest=function(){this.finalize();var t=this.h0,r=this.h1,e=this.h2,i=this.h3;return[255&t,t>>>8&255,t>>>16&255,t>>>24&255,255&r,r>>>8&255,r>>>16&255,r>>>24&255,255&e,e>>>8&255,e>>>16&255,e>>>24&255,255&i,i>>>8&255,i>>>16&255,i>>>24&255]},A.prototype.array=A.prototype.digest,A.prototype.arrayBuffer=function(){this.finalize();var t=new ArrayBuffer(16),r=new Uint32Array(t);return r[0]=this.h0,r[1]=this.h1,r[2]=this.h2,r[3]=this.h3,t},A.prototype.buffer=A.prototype.arrayBuffer,A.prototype.base64=function(){for(var t,r,e,i="",h=this.array(),s=0;s<15;)t=h[s++],r=h[s++],e=h[s++],i+=p[t>>>2]+p[63&(t<<4|r>>>4)]+p[63&(r<<2|e>>>6)]+p[63&e];return t=h[s],i+=p[t>>>2]+p[t<<4&63]+"=="},_.prototype=new A,_.prototype.finalize=function(){if(A.prototype.finalize.call(this),this.inner){this.inner=!1;var t=this.array();A.call(this,this.sharedMemory),this.update(this.oKeyPad),this.update(t),A.prototype.finalize.call(this)}};var B=function(){var t=v("hex");t.create=function(){return new A},t.update=function(r){return t.create().update(r)};for(var r=0;r<f.length;++r){var e=f[r];t[e]=v(e)}return t}();B.md5=B,B.md5.hmac=function(){var t=w("hex");t.create=function(t){return new _(t)},t.update=function(r,e){return t.create(r).update(e)};for(var r=0;r<f.length;++r){var e=f[r];t[e]=w(e)}return t}(),h?r.exports=B:i.md5=B}();var i=t(e.exports);export{i as default,i as md5};
package/package.json CHANGED
@@ -1,6 +1,41 @@
1
- {
2
- "name": "@mytools-all/mytools-ocr",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "@mytools-all/mytools-ocr",
3
+ "version": "0.1.0",
4
+ "description": "讯飞 OCR",
5
+ "type": "module",
6
+ "main": "index.html",
7
+ "license": "MIT",
8
+ "author": "MyTools Contributors",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/mytools-all/mytools-ocr.git"
12
+ },
13
+ "homepage": "https://github.com/mytools-all/mytools-ocr#readme",
14
+ "bugs": {
15
+ "url": "https://github.com/mytools-all/mytools-ocr/issues"
16
+ },
17
+ "publishConfig": {
18
+ "access": "public",
19
+ "registry": "https://registry.npmjs.org/"
20
+ },
21
+ "files": [
22
+ "plugin.json",
23
+ "*.html",
24
+ "*.css",
25
+ "*.svg",
26
+ "*.png",
27
+ "vendor/",
28
+ "LICENSE",
29
+ "THIRD_PARTY.md",
30
+ "README.md",
31
+ "app.mjs",
32
+ "common.mjs",
33
+ "md5.mjs",
34
+ "utils.mjs",
35
+ "*.js",
36
+ "*.LICENSE"
37
+ ],
38
+ "scripts": {
39
+ "test": "node --test"
40
+ }
41
+ }
package/plugin.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "mytools-ocr",
3
+ "description": "讯飞 OCR",
4
+ "apiVersion": 1,
5
+ "main": "index.html",
6
+ "pluginName": "讯飞 OCR",
7
+ "version": "0.1.0",
8
+ "capabilities": [
9
+ "net.fetch",
10
+ "storage.get",
11
+ "storage.set",
12
+ "storage.del",
13
+ "clipboard.write"
14
+ ],
15
+ "features": [
16
+ {
17
+ "code": "ocr",
18
+ "explain": "打开讯飞 OCR",
19
+ "cmds": [
20
+ "OCR",
21
+ "文字识别",
22
+ "讯飞OCR",
23
+ "图片文字"
24
+ ]
25
+ }
26
+ ]
27
+ }
package/tool.css ADDED
@@ -0,0 +1,295 @@
1
+ :root {
2
+ font:
3
+ 13px/1.6 "Segoe UI",
4
+ "Microsoft YaHei",
5
+ sans-serif;
6
+ color-scheme: light;
7
+ --bg: #fff;
8
+ --surface: #f6f7f9;
9
+ --fg: #333b48;
10
+ --muted: #89919e;
11
+ --border: #e8ebf0;
12
+ --accent: #377cf6;
13
+ --soft: #edf3ff;
14
+ --danger: #db4552;
15
+ }
16
+ * {
17
+ box-sizing: border-box;
18
+ }
19
+ body {
20
+ margin: 0;
21
+ color: var(--fg);
22
+ background: var(--bg);
23
+ height: 100vh;
24
+ overflow: hidden;
25
+ }
26
+ button,
27
+ input,
28
+ textarea,
29
+ select {
30
+ font: inherit;
31
+ color: inherit;
32
+ }
33
+ button {
34
+ border: 1px solid var(--border);
35
+ border-radius: 5px;
36
+ background: var(--bg);
37
+ padding: 5px 12px;
38
+ cursor: pointer;
39
+ white-space: nowrap;
40
+ }
41
+ button:hover {
42
+ border-color: var(--accent);
43
+ color: var(--accent);
44
+ }
45
+ button:disabled {
46
+ opacity: 0.45;
47
+ cursor: default;
48
+ }
49
+ .primary {
50
+ background: var(--accent);
51
+ border-color: var(--accent);
52
+ color: white;
53
+ }
54
+ .primary:hover {
55
+ color: white;
56
+ filter: brightness(1.06);
57
+ }
58
+ input,
59
+ textarea,
60
+ select {
61
+ border: 1px solid var(--border);
62
+ border-radius: 4px;
63
+ padding: 7px 9px;
64
+ background: var(--bg);
65
+ min-width: 0;
66
+ }
67
+ textarea {
68
+ resize: none;
69
+ line-height: 1.7;
70
+ tab-size: 2;
71
+ font-family: Consolas, "Microsoft YaHei", monospace;
72
+ }
73
+ input:focus,
74
+ textarea:focus,
75
+ select:focus {
76
+ outline: 1px solid var(--accent);
77
+ }
78
+ button:focus-visible {
79
+ outline: 2px solid var(--accent);
80
+ outline-offset: 2px;
81
+ }
82
+ main {
83
+ display: flex;
84
+ flex-direction: column;
85
+ height: 100%;
86
+ min-height: 0;
87
+ }
88
+ .toolbar {
89
+ display: flex;
90
+ align-items: center;
91
+ gap: 8px;
92
+ padding: 10px 14px;
93
+ border-bottom: 1px solid var(--border);
94
+ flex-wrap: wrap;
95
+ }
96
+ .toolbar strong {
97
+ font-size: 14px;
98
+ margin-right: 10px;
99
+ }
100
+ .spacer {
101
+ flex: 1;
102
+ }
103
+ .content {
104
+ flex: 1;
105
+ min-height: 0;
106
+ overflow: auto;
107
+ padding: 16px;
108
+ }
109
+ .split {
110
+ display: grid;
111
+ grid-template-columns: 1fr 1fr;
112
+ flex: 1;
113
+ min-height: 0;
114
+ }
115
+ .split > * {
116
+ min-width: 0;
117
+ overflow: auto;
118
+ }
119
+ .split > :first-child {
120
+ border-right: 1px solid var(--border);
121
+ }
122
+ .editor {
123
+ width: 100%;
124
+ height: 100%;
125
+ border: 0;
126
+ border-radius: 0;
127
+ padding: 16px;
128
+ outline: none !important;
129
+ background: var(--bg);
130
+ }
131
+ .muted,
132
+ small {
133
+ color: var(--muted);
134
+ }
135
+ .status {
136
+ font-size: 12px;
137
+ min-height: 26px;
138
+ border-top: 1px solid var(--border);
139
+ padding: 3px 14px;
140
+ color: var(--muted);
141
+ }
142
+ .status[data-error="true"] {
143
+ color: var(--danger);
144
+ }
145
+ .tabs {
146
+ display: flex;
147
+ gap: 5px;
148
+ align-items: center;
149
+ }
150
+ .tabs button {
151
+ border: 0;
152
+ background: transparent;
153
+ }
154
+ .tabs .active {
155
+ color: var(--accent);
156
+ background: var(--soft);
157
+ }
158
+ .empty {
159
+ padding: 48px 16px;
160
+ text-align: center;
161
+ color: var(--muted);
162
+ }
163
+ table {
164
+ border-collapse: collapse;
165
+ width: 100%;
166
+ text-align: left;
167
+ }
168
+ th {
169
+ font-size: 12px;
170
+ color: var(--muted);
171
+ font-weight: 400;
172
+ background: var(--surface);
173
+ position: sticky;
174
+ top: 0;
175
+ }
176
+ th,
177
+ td {
178
+ padding: 9px 12px;
179
+ border-bottom: 1px solid var(--border);
180
+ }
181
+ tr.selected {
182
+ background: var(--soft);
183
+ }
184
+ pre {
185
+ white-space: pre-wrap;
186
+ overflow-wrap: anywhere;
187
+ margin: 0;
188
+ }
189
+ label {
190
+ display: flex;
191
+ gap: 8px;
192
+ align-items: center;
193
+ }
194
+ .stack {
195
+ display: flex;
196
+ flex-direction: column;
197
+ gap: 12px;
198
+ }
199
+ .row {
200
+ display: flex;
201
+ gap: 8px;
202
+ align-items: center;
203
+ }
204
+ .panel {
205
+ border: 1px solid var(--border);
206
+ border-radius: 6px;
207
+ padding: 14px;
208
+ }
209
+ .badge {
210
+ font-size: 11px;
211
+ padding: 1px 7px;
212
+ border-radius: 4px;
213
+ background: var(--surface);
214
+ color: var(--muted);
215
+ }
216
+ .danger {
217
+ color: var(--danger);
218
+ }
219
+ .hidden {
220
+ display: none !important;
221
+ }
222
+ #theme {
223
+ padding: 3px 8px;
224
+ }
225
+ .sidebar {
226
+ width: 155px;
227
+ background: var(--surface);
228
+ padding: 14px 10px;
229
+ border-right: 1px solid var(--border);
230
+ overflow: auto;
231
+ }
232
+ .sidebar button {
233
+ display: block;
234
+ width: 100%;
235
+ text-align: left;
236
+ border: 0;
237
+ background: transparent;
238
+ margin-bottom: 4px;
239
+ }
240
+ .sidebar .active {
241
+ background: var(--soft);
242
+ color: var(--accent);
243
+ }
244
+ .workspace {
245
+ display: flex;
246
+ flex: 1;
247
+ min-height: 0;
248
+ }
249
+ .workspace > .content {
250
+ flex: 1;
251
+ }
252
+ details {
253
+ border-bottom: 1px solid var(--border);
254
+ padding: 7px 0;
255
+ }
256
+ summary {
257
+ cursor: pointer;
258
+ }
259
+ ::-webkit-scrollbar {
260
+ width: 6px;
261
+ height: 6px;
262
+ }
263
+ ::-webkit-scrollbar-thumb {
264
+ background: #b9c0cb60;
265
+ border-radius: 8px;
266
+ }
267
+ html[data-theme="dark"] {
268
+ color-scheme: dark;
269
+ --bg: #1b1e24;
270
+ --surface: #222731;
271
+ --fg: #dce1eb;
272
+ --muted: #8894a8;
273
+ --border: #333a47;
274
+ --accent: #6ca7ff;
275
+ --soft: #27364c;
276
+ --danger: #ff8490;
277
+ }
278
+ @media (max-width: 560px) {
279
+ .sidebar {
280
+ width: 115px;
281
+ }
282
+ .toolbar {
283
+ padding: 8px;
284
+ gap: 5px;
285
+ }
286
+ button {
287
+ padding: 5px 8px;
288
+ }
289
+ }
290
+ @media (prefers-reduced-motion: reduce) {
291
+ * {
292
+ scroll-behavior: auto !important;
293
+ animation: none !important;
294
+ }
295
+ }
package/utils.mjs ADDED
@@ -0,0 +1,201 @@
1
+ export function evaluate(source) {
2
+ const text = String(source)
3
+ .replace(/[,,\s]/g, "")
4
+ .replace(/×/g, "*")
5
+ .replace(/÷/g, "/");
6
+ let p = 0;
7
+ const eat = (c) => (text[p] === c ? (p++, true) : false);
8
+ function expr() {
9
+ let v = term();
10
+ for (;;) {
11
+ if (eat("+")) v += term();
12
+ else if (eat("-")) v -= term();
13
+ else return v;
14
+ }
15
+ }
16
+ function term() {
17
+ let v = unary();
18
+ for (;;) {
19
+ if (eat("*")) v *= unary();
20
+ else if (eat("/")) {
21
+ const d = unary();
22
+ if (d === 0) throw Error("不能除以零");
23
+ v /= d;
24
+ } else return v;
25
+ }
26
+ }
27
+ function unary() {
28
+ if (eat("+")) return unary();
29
+ if (eat("-")) return -unary();
30
+ return power();
31
+ }
32
+ function power() {
33
+ let v = primary();
34
+ while (eat("%")) v /= 100;
35
+ if (eat("^")) v = Math.pow(v, unary());
36
+ return v;
37
+ }
38
+ function primary() {
39
+ if (eat("(")) {
40
+ const v = expr();
41
+ if (!eat(")")) throw Error("缺少右括号");
42
+ return v;
43
+ }
44
+ const m = text.slice(p).match(/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?/i);
45
+ if (!m) throw Error("请输入有效的计算公式");
46
+ p += m[0].length;
47
+ return Number(m[0]);
48
+ }
49
+ if (!text) throw Error("表达式为空");
50
+ const v = expr();
51
+ if (p !== text.length || !Number.isFinite(v)) throw Error("计算公式不完整");
52
+ return Number(v.toPrecision(14));
53
+ }
54
+ export function renamePreview(files, rule) {
55
+ const targets = new Map();
56
+ const rows = files.map((file, i) => {
57
+ const dot = file.directory ? -1 : file.name.lastIndexOf(".");
58
+ const ext = dot > 0 ? file.name.slice(dot) : "";
59
+ let base = rule.extension
60
+ ? file.name
61
+ : dot > 0
62
+ ? file.name.slice(0, dot)
63
+ : file.name;
64
+ if (rule.mode === "replace") {
65
+ if (rule.find) {
66
+ base = rule.regex
67
+ ? base.replace(new RegExp(rule.find, "g"), rule.replace)
68
+ : base.split(rule.find).join(rule.replace);
69
+ }
70
+ }
71
+ if (rule.mode === "append") {
72
+ const seq = String(
73
+ (Number.isFinite(Number(rule.start)) ? Number(rule.start) : 1) +
74
+ i * (Number(rule.step) || 1),
75
+ ).padStart(Number(rule.pad) || 1, "0");
76
+ const text = String(rule.text || "").replaceAll("{n}", seq);
77
+ base =
78
+ rule.position === "before"
79
+ ? text + base
80
+ : rule.position === "at"
81
+ ? base.slice(0, Number(rule.index) || 0) +
82
+ text +
83
+ base.slice(Number(rule.index) || 0)
84
+ : base + text;
85
+ }
86
+ if (rule.mode === "sequence")
87
+ base =
88
+ (rule.prefix || "") +
89
+ String(
90
+ (Number.isFinite(Number(rule.start)) ? Number(rule.start) : 1) +
91
+ i * (Number(rule.step) || 1),
92
+ ).padStart(Number(rule.pad) || 1, "0") +
93
+ (rule.suffix || "");
94
+ if (rule.mode === "case")
95
+ base =
96
+ rule.case === "upper"
97
+ ? base.toUpperCase()
98
+ : rule.case === "title"
99
+ ? base.replace(/\b\p{L}/gu, (c) => c.toUpperCase())
100
+ : base.toLowerCase();
101
+ if (rule.mode === "manual")
102
+ base =
103
+ String(rule.lines || "")
104
+ .split("\n")
105
+ [i]?.trim() || base;
106
+ const name = base + (rule.extension ? "" : ext);
107
+ const stem = name.split(".")[0];
108
+ const invalid =
109
+ /[\\/:*?"<>|\x00-\x1f]/.test(name) ||
110
+ /[. ]$/.test(name) ||
111
+ !name ||
112
+ /^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/i.test(stem);
113
+ const to = file.path.slice(0, file.path.length - file.name.length) + name;
114
+ const row = {
115
+ ...file,
116
+ to,
117
+ newName: name,
118
+ error: invalid ? "文件名无效" : "",
119
+ changed: name !== file.name,
120
+ };
121
+ const key = to.toLowerCase();
122
+ if (targets.has(key)) {
123
+ row.error = "目标重名";
124
+ targets.get(key).error = "目标重名";
125
+ }
126
+ targets.set(key, row);
127
+ return row;
128
+ });
129
+ const existing = new Set(files.map((f) => f.path.toLowerCase()));
130
+ for (const row of rows)
131
+ if (row.changed && existing.has(row.to.toLowerCase()))
132
+ row.error = "目标已存在";
133
+ return rows;
134
+ }
135
+ export function jsonPath(value, path) {
136
+ const parts = String(path)
137
+ .trim()
138
+ .replace(/^\$\.?/, "")
139
+ .replace(/\[(\d+)\]/g, ".$1")
140
+ .split(".")
141
+ .filter(Boolean);
142
+ if (!parts.length) return value;
143
+ let values = [value];
144
+ let wildcard = false;
145
+ for (const part of parts) {
146
+ if (part === "*") {
147
+ wildcard = true;
148
+ values = values.flatMap((v) =>
149
+ v && typeof v === "object" ? Object.values(v) : [],
150
+ );
151
+ } else
152
+ values = values
153
+ .map((v) =>
154
+ v != null && Object.hasOwn(Object(v), part) ? v[part] : undefined,
155
+ )
156
+ .filter((v) => v !== undefined);
157
+ }
158
+ if (!values.length) throw Error("路径未匹配任何值");
159
+ return wildcard ? values : values[0];
160
+ }
161
+ export function toXml(value, name = "root") {
162
+ const esc = (s) =>
163
+ String(s ?? "").replace(
164
+ /[<>&"']/g,
165
+ (c) =>
166
+ ({
167
+ "<": "&lt;",
168
+ ">": "&gt;",
169
+ "&": "&amp;",
170
+ '"': "&quot;",
171
+ "'": "&apos;",
172
+ })[c],
173
+ );
174
+ const tag = /^[A-Za-z_][\w.-]*$/.test(name) ? name : "item";
175
+ if (Array.isArray(value))
176
+ return `<${tag}>${value.map((v) => toXml(v, "item")).join("")}</${tag}>`;
177
+ if (value && typeof value === "object")
178
+ return `<${tag}>${Object.entries(value)
179
+ .map(([k, v]) => toXml(v, k))
180
+ .join("")}</${tag}>`;
181
+ return `<${tag}>${esc(value)}</${tag}>`;
182
+ }
183
+ export function toTypescript(value) {
184
+ function type(v) {
185
+ if (v === null) return "null";
186
+ if (Array.isArray(v))
187
+ return v.length
188
+ ? "(" + [...new Set(v.map(type))].join(" | ") + ")[]"
189
+ : "unknown[]";
190
+ if (typeof v === "object")
191
+ return (
192
+ "{\n" +
193
+ Object.entries(v)
194
+ .map(([k, x]) => " " + JSON.stringify(k) + ": " + type(x) + ";")
195
+ .join("\n") +
196
+ "\n}"
197
+ );
198
+ return typeof v;
199
+ }
200
+ return "type Root = " + type(value) + ";";
201
+ }