Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions plugins/f-provider/public/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"title": "ZTools 提供商",
"description": "OCR + 翻译提供商集合(百度/谷歌/有道/微软翻译、微信 OCR)",
"author": "kaineooo",
"version": "1.0.0",
"version": "1.0.1",
"main": "index.html",
"preload": "preload/services.js",
"logo": "logo.png",
Expand Down Expand Up @@ -44,9 +44,7 @@
"explain": "OCR 识别与翻译(关键词进入管理;图片/选中文本进入识别或翻译)",
"icon": "logo.png",
"cmds": [
{
"label": "ZTools 提供商"
},
"ZTools 提供商",
{
"type": "img",
"label": "识别图片文字"
Expand Down Expand Up @@ -84,9 +82,7 @@
"icon": "logo.png",
"platform": ["win32", "darwin"],
"cmds": [
{
"label": "截图识别文字"
}
"截图识别文字"
]
}
],
Expand Down
30 changes: 30 additions & 0 deletions plugins/f-provider/public/preload/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ window.services = {
return filePath
},

// 插件 logo 绝对路径(用于 createBrowserWindow 的 icon,让子窗口任务栏/标题栏用插件图标)。
// logo.png 位于插件根目录(plugin.json 的 logo 字段)。
pluginLogoPath() {
return path.join(__dirname, '..', 'logo.png')
},

// 插件 logo 的 data URI(用于子窗口 <img> 展示图标,注入到渲染层)。
pluginLogoDataUrl() {
try {
const p = path.join(__dirname, '..', 'logo.png')
const buf = fs.readFileSync(p)
return 'data:image/png;base64,' + buf.toString('base64')
} catch (_) {
return ''
}
},

// 插件 logo 的 NativeImage 对象(用于 createBrowserWindow 的 icon)。
// Windows 下传字符串路径时任务栏按钮仍按 AppID 取宿主 exe 图标,
// 传 NativeImage 才能让任务栏真正显示插件图标。
pluginLogoNativeImage() {
try {
// ZTools 在 preload 提供 require('electron').nativeImage
const { nativeImage } = require('electron')
return nativeImage.createFromPath(path.join(__dirname, '..', 'logo.png'))
} catch (_) {
return null
}
},
Comment on lines +80 to +102

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

每次调用 pluginLogoDataUrl 都会进行同步文件读取 (fs.readFileSync),而每次调用 pluginLogoNativeImage 都会重新创建 NativeImage 对象。由于插件图标在运行期间不会改变,建议对这两个方法的结果进行缓存,以避免不必要的磁盘 I/O 和内存开销。


// ─── 微信 OCR(基于 wechat_ocr.node 原生模块)─────────────────────────
// 原生模块懒加载:首次调用时才 require + init,避免插件加载即拉起
// WeChatOCR.exe 子进程(Windows)。
Expand Down
12 changes: 12 additions & 0 deletions plugins/f-provider/screen-ocr-result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>截图识别结果</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/screenOcrResult.ts"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions plugins/f-provider/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ declare global {
readFileAsDataURL: (file: string) => string
writeTextFile: (text: string) => string
writeImageFile: (base64Url: string) => string | undefined
/** 插件 logo 绝对路径(用于 createBrowserWindow 的 icon)。 */
pluginLogoPath: () => string
/** 插件 logo 的 data URI(用于子窗口 <img> 展示图标)。 */
pluginLogoDataUrl: () => string
/** 插件 logo 的 NativeImage(用于 createBrowserWindow 的 icon,Windows 任务栏更可靠)。 */
pluginLogoNativeImage: () => BrowserWindow.NativeImage | null
/**
* OCR Provider 核心能力:image 为 本地路径 / data URI / http(s) URL。
* 返回 provider 契约结构;失败抛错。
Expand Down
23 changes: 23 additions & 0 deletions plugins/f-provider/src/screenOcrResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createApp } from 'vue'
import ZToolsUI from 'ztools-ui'
import 'ztools-ui/style'
import { useZtoolsTheme } from 'ztools-ui'
import './main.css'
import ScreenOcrResult from './views/ScreenOcrResult.vue'

// 结果展示窗口入口(由 ztools.createBrowserWindow 打开)。
//
// 与主窗口的区别:该窗口由 createBrowserWindow 创建,**不带 preload**,
// 也没有可靠的 window.ztools API(子窗口未必注入)。因此窗口只做展示——
// 主窗口在外层完成「截图 + OCR 识别」后,通过 webContents.executeJavaScript
// 把识别结果(imageSrc + lines + isDark)注入到本窗口。
//
// 注入方式:主窗口调用 executeJavaScript('window.__loadScreenOcrResult({...})')。
// 本入口在挂载后挂载 window.__loadScreenOcrResult,并把数据交给组件渲染。

// useZtoolsTheme 在无 window.ztools 时会优雅降级(不报错),主题以注入的 isDark 为准。
useZtoolsTheme()

const app = createApp(ScreenOcrResult)
app.use(ZToolsUI)
app.mount('#app')
Loading
Loading