@mytools-all/mytools-ocr 0.1.0 → 0.2.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/README.md +32 -21
- package/THIRD_PARTY.md +3 -25
- package/app.mjs +95 -201
- package/history.mjs +13 -0
- package/index.html +5 -104
- package/package.json +42 -41
- package/plugin.json +38 -27
- package/program.mjs +72 -0
- package/tool.css +296 -295
- package/umi.mjs +34 -0
- package/common.mjs +0 -69
- package/md5.LICENSE +0 -20
- package/md5.mjs +0 -10
- package/utils.mjs +0 -201
package/plugin.json
CHANGED
|
@@ -1,27 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "mytools-ocr",
|
|
3
|
-
"description": "
|
|
4
|
-
"apiVersion": 1,
|
|
5
|
-
"main": "index.html",
|
|
6
|
-
"pluginName": "
|
|
7
|
-
"version": "0.1
|
|
8
|
-
"capabilities": [
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"storage.
|
|
12
|
-
"storage.
|
|
13
|
-
"clipboard.write"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "mytools-ocr",
|
|
3
|
+
"description": "通过本机 Umi-OCR 离线识别图片文字,无需登录",
|
|
4
|
+
"apiVersion": 1,
|
|
5
|
+
"main": "index.html",
|
|
6
|
+
"pluginName": "图片 OCR",
|
|
7
|
+
"version": "0.2.1",
|
|
8
|
+
"capabilities": [
|
|
9
|
+
"context.read",
|
|
10
|
+
"net.fetch",
|
|
11
|
+
"storage.get",
|
|
12
|
+
"storage.set",
|
|
13
|
+
"clipboard.write",
|
|
14
|
+
"fs.exporttext",
|
|
15
|
+
"clipboard.read",
|
|
16
|
+
"shell.open",
|
|
17
|
+
"fs.pickfiles",
|
|
18
|
+
"fs.globalsearch"
|
|
19
|
+
],
|
|
20
|
+
"features": [
|
|
21
|
+
{
|
|
22
|
+
"code": "ocr",
|
|
23
|
+
"explain": "打开图片 OCR",
|
|
24
|
+
"cmds": [
|
|
25
|
+
"图片OCR",
|
|
26
|
+
"图片 OCR",
|
|
27
|
+
"OCR",
|
|
28
|
+
"文字识别",
|
|
29
|
+
"图片文字",
|
|
30
|
+
"本地OCR",
|
|
31
|
+
"Umi-OCR"
|
|
32
|
+
]
|
|
33
|
+
}
|
|
34
|
+
],
|
|
35
|
+
"engines": {
|
|
36
|
+
"mytools": ">=0.2.0 <0.3.0"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/program.mjs
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {endpoint,responseJson,isConnectionFailure} from './umi.mjs';
|
|
2
|
+
|
|
3
|
+
export function installPath(value){
|
|
4
|
+
if(typeof value!=='string')throw Error('请输入 Umi-OCR 安装目录');
|
|
5
|
+
let path=value.trim().replace(/^"(.*)"$/,'$1').replaceAll('/','\\');
|
|
6
|
+
if(!path)return '';
|
|
7
|
+
if(!/^[a-z]:\\/i.test(path)||/[\x00-\x1f<>"|?*]/.test(path)||path.slice(2).includes(':')||path.split('\\').some(part=>part==='..'||part==='.'))throw Error('请选择本机磁盘上的完整安装目录');
|
|
8
|
+
path=path.replace(/\\Umi-OCR\.exe$/i,'').replace(/\\+$/,'');
|
|
9
|
+
return path.length===2?path+'\\':path;
|
|
10
|
+
}
|
|
11
|
+
export function executable(path){const directory=installPath(path);if(!directory)throw Error('请先填写或选择 Umi-OCR 安装目录');return directory.replace(/\\$/,'')+'\\Umi-OCR.exe';}
|
|
12
|
+
export function detectedPaths(entries){
|
|
13
|
+
if(!Array.isArray(entries))throw Error('自动检测返回的数据无效');
|
|
14
|
+
const found=new Map();
|
|
15
|
+
for(const entry of entries){if(entry.directory||typeof entry.path!=='string'||!/[\\/]Umi-OCR\.exe$/i.test(entry.path))continue;try{const path=installPath(entry.path);found.set(path.toLowerCase(),path);}catch{}}
|
|
16
|
+
return [...found.values()].sort((a,b)=>a.localeCompare(b));
|
|
17
|
+
}
|
|
18
|
+
export function programControls(doc,call,status,task){
|
|
19
|
+
const $=id=>doc.getElementById(id),win=doc.defaultView;
|
|
20
|
+
let disposed=false,saved='',queue=Promise.resolve(),waitTimer,wake;
|
|
21
|
+
const report=(text,state='unknown')=>{if(!disposed){$('program-status').textContent=text;$('program-status').dataset.state=state;}};
|
|
22
|
+
const action=fn=>async()=>{try{await task(fn);}catch(error){report('未连接','error');status(error.message||String(error),true);}};
|
|
23
|
+
function save(value){
|
|
24
|
+
const path=installPath(value);
|
|
25
|
+
const result=queue.then(async()=>{if(disposed)return;await call('storage.set',{key:'umi.program.v1',value:{path}});saved=path;});
|
|
26
|
+
queue=result.catch(()=>{});return result.then(()=>path);
|
|
27
|
+
}
|
|
28
|
+
async function apply(value){const path=await save(value);if(!disposed){$('program-path').value=path;status('安装路径已保存');}}
|
|
29
|
+
async function connected(){
|
|
30
|
+
const base=endpoint($('endpoint').value.trim());
|
|
31
|
+
try{const data=responseJson(await call('net.fetch',{url:base+'/api/ocr/get_options',method:'GET'}));if(!data?.['data.format']||!data?.['tbpu.parser'])throw Error('该地址不是兼容的 Umi-OCR 服务');return true;}
|
|
32
|
+
catch(error){if(isConnectionFailure(error))return false;throw error;}
|
|
33
|
+
}
|
|
34
|
+
$('program-path').onchange=async()=>{const draft=$('program-path').value;try{const path=await save(draft);if(!disposed&&$('program-path').value===draft)$('program-path').value=path;}catch(error){status(error.message,true);}};
|
|
35
|
+
$('program-path').onkeydown=event=>{if(event.key==='Enter'){event.preventDefault();void action(()=>apply($('program-path').value))();}};
|
|
36
|
+
$('program-pick').onclick=action(async()=>{
|
|
37
|
+
let result;try{result=await call('fs.pickfiles',{directories:true});}catch(error){if(/cancelled|取消/i.test(error.message||String(error)))return;throw error;}
|
|
38
|
+
const entry=result?.entries?.[0];if(!entry||disposed)return;if(!entry.directory)throw Error('请选择包含 Umi-OCR.exe 的文件夹');await apply(entry.path);
|
|
39
|
+
});
|
|
40
|
+
$('program-detect').onclick=action(async()=>{
|
|
41
|
+
status('正在检测 Umi-OCR 安装目录…');
|
|
42
|
+
let result;try{result=await call('fs.globalsearch',{query:'Umi-OCR.exe',limit:100});}catch(error){throw Error('自动检测未完成:'+(error.message||String(error))+'。可手动选择安装目录。');}
|
|
43
|
+
if(disposed)return;const paths=detectedPaths(result?.entries);
|
|
44
|
+
if(!paths.length)throw Error('未找到 Umi-OCR.exe,请手动选择安装目录;自动检测需要 Everything 已运行并完成索引');
|
|
45
|
+
const preferred=paths.find(path=>path.toLowerCase()===saved.toLowerCase());
|
|
46
|
+
if(paths.length===1||preferred){await apply(preferred||paths[0]);return;}
|
|
47
|
+
$('program-options').replaceChildren(...paths.map(path=>{const option=doc.createElement('option');option.value=path;option.textContent=path;return option;}));
|
|
48
|
+
$('program-dialog').showModal();$('program-cancel').focus();status('检测到多个安装目录,请选择要使用的版本');
|
|
49
|
+
});
|
|
50
|
+
$('program-cancel').onclick=()=>$('program-dialog').close();
|
|
51
|
+
$('program-confirm').onclick=action(async()=>{await apply($('program-options').value);if(!disposed)$('program-dialog').close();});
|
|
52
|
+
$('program-start').onclick=action(async()=>{
|
|
53
|
+
report('检查中…');
|
|
54
|
+
if(await connected()){report('已连接','connected');status('Umi-OCR 服务已运行,无需重复启动');return;}
|
|
55
|
+
if(disposed)return;
|
|
56
|
+
const target=executable($('program-path').value);await save($('program-path').value);if(disposed)return;
|
|
57
|
+
report('启动中…','starting');status('正在启动 Umi-OCR…');
|
|
58
|
+
try{await call('shell.open',{path:target});}catch(error){throw Error('无法启动,请确认目录中包含 Umi-OCR.exe:'+(error.message||String(error)));}
|
|
59
|
+
for(let attempt=0;attempt<8&&!disposed;attempt++){
|
|
60
|
+
if(await connected()){report('已连接','connected');status('Umi-OCR 已启动,服务连接成功');return;}
|
|
61
|
+
if(disposed)return;
|
|
62
|
+
await new Promise(resolve=>{wake=resolve;waitTimer=win.setTimeout(()=>{wake=null;resolve();},500);});
|
|
63
|
+
}
|
|
64
|
+
if(!disposed){report('未连接','error');status('已发送启动请求,但 HTTP 服务尚未连接。请在 Umi-OCR 全局设置中开启 HTTP 服务,并核对端口后点击“检查连接”');}
|
|
65
|
+
});
|
|
66
|
+
$('endpoint').addEventListener('input',()=>report('未检查'));
|
|
67
|
+
return {
|
|
68
|
+
markConnected(){report('已连接','connected');},
|
|
69
|
+
async init(){try{const config=await call('storage.get',{key:'umi.program.v1'});saved=installPath(config?.path??'');if(!disposed)$('program-path').value=saved;}catch(error){status('安装路径读取失败:'+error.message,true);}},
|
|
70
|
+
dispose(){disposed=true;if(waitTimer)win.clearTimeout(waitTimer);wake?.();}
|
|
71
|
+
};
|
|
72
|
+
}
|