diff --git a/plugins/traditional-simplified-cn/README.md b/plugins/traditional-simplified-cn/README.md new file mode 100644 index 00000000..07ebee4b --- /dev/null +++ b/plugins/traditional-simplified-cn/README.md @@ -0,0 +1,35 @@ +# 繁简转换(ZTools 插件) + +基于 [OpenCC](https://github.com/BYVoid/OpenCC)(`opencc-js`)的繁体字与简体字互转,支持台湾地区 / 香港地区与大陆简体之间的用词差异。 + +## 功能 + +- 界面**左侧为原文**,**右侧为转换结果**,可对照查看。 +- 在 ZTools 中复制文本后呼出启动器:剪贴板有内容时会出现 **「繁简转换(剪贴板)」**,进入插件后自动填入**原文**侧;在右侧点击 **繁 → 简** 或 **简 → 繁** 生成结果。 +- 剪贴板为空时,可搜索 **「繁简转换」** 或 **「繁简」** 手动打开。 +- **繁体标准**可在界面选择 **台湾(tw)** 或 **香港(hk)**,会影响词语级转换。 + +## 安装依赖 + +在插件根目录执行: + +```bash +npm install +``` + +需存在 `node_modules/opencc-js`(ZTools 通过 `preload.js` 以 CommonJS 加载)。 + +## 在 ZTools 中加载 + +1. 将本目录准备完整(含 `logo.png`、`plugin.json`、`preload.js`、`index.html` 等)。 +2. ZTools → **设置** → **开发者** → **加载本地插件**,选择本插件目录。 +3. 修改 `preload.js` 后若未生效,请对该插件 **卸载再重新加载**(preload 通常不热重载)。 + +## 文件说明 + +| 文件 | 说明 | +|------|------| +| `plugin.json` | 插件元数据、主搜索框推送(剪贴板入口)与手动打开关键字 | +| `preload.js` | 读取剪贴板、`opencc-js` 转换逻辑、`onMainPush` / `onPluginEnter` | +| `index.html` / `index.js` | 双栏界面与交互 | +| `package.json` | 声明 `opencc-js` 依赖 | diff --git a/plugins/traditional-simplified-cn/index.html b/plugins/traditional-simplified-cn/index.html new file mode 100644 index 00000000..386a94d4 --- /dev/null +++ b/plugins/traditional-simplified-cn/index.html @@ -0,0 +1,143 @@ + + + + + + 繁简转换 + + + +

繁简转换

+
+ + +
+
+
+ + +
+
+ + +
+
+
+ + + +
+ + + diff --git a/plugins/traditional-simplified-cn/index.js b/plugins/traditional-simplified-cn/index.js new file mode 100644 index 00000000..833dca2c --- /dev/null +++ b/plugins/traditional-simplified-cn/index.js @@ -0,0 +1,77 @@ +;(function () { + var originalEl = document.getElementById('inputOriginal') + var resultEl = document.getElementById('inputResult') + var variantEl = document.getElementById('variant') + + if (window.ztools && window.ztools.isDarkColors && window.ztools.isDarkColors()) { + document.body.classList.add('dark') + } + + function resizeHeight() { + setTimeout(function () { + var h = document.body.scrollHeight + if (window.ztools && window.ztools.setExpendHeight) { + window.ztools.setExpendHeight(h + 48) + } + }, 80) + } + + function applyVariant() { + if (window.nodeAPI && window.nodeAPI.setVariant) { + window.nodeAPI.setVariant(variantEl.value) + } + } + + variantEl.addEventListener('change', function () { + applyVariant() + resizeHeight() + }) + applyVariant() + + function onTextChange() { + resizeHeight() + } + originalEl.addEventListener('input', onTextChange) + resultEl.addEventListener('input', onTextChange) + + document.getElementById('btnToSimp').addEventListener('click', function () { + if (!window.nodeAPI) return + applyVariant() + resultEl.value = window.nodeAPI.toSimplified(originalEl.value) + resizeHeight() + }) + + document.getElementById('btnToTrad').addEventListener('click', function () { + if (!window.nodeAPI) return + applyVariant() + resultEl.value = window.nodeAPI.toTraditional(originalEl.value) + resizeHeight() + }) + + document.getElementById('btnCopy').addEventListener('click', function () { + var t = resultEl.value + if (!t) { + if (window.ztools && window.ztools.showNotification) { + window.ztools.showNotification('没有可复制的内容') + } + return + } + if (window.ztools && window.ztools.copyText) { + window.ztools.copyText(t) + window.ztools.showNotification('已复制转换结果') + } + }) + + window.addEventListener('plugin-enter', function (e) { + var action = e.detail || {} + var payload = action.payload + if (action.code === 'convert-clipboard' && typeof payload === 'string') { + applyVariant() + originalEl.value = payload + resultEl.value = '' + } + resizeHeight() + }) + + resizeHeight() +})() diff --git a/plugins/traditional-simplified-cn/logo.png b/plugins/traditional-simplified-cn/logo.png new file mode 100644 index 00000000..64fee38b Binary files /dev/null and b/plugins/traditional-simplified-cn/logo.png differ diff --git a/plugins/traditional-simplified-cn/package-lock.json b/plugins/traditional-simplified-cn/package-lock.json new file mode 100644 index 00000000..0966c825 --- /dev/null +++ b/plugins/traditional-simplified-cn/package-lock.json @@ -0,0 +1,21 @@ +{ + "name": "ztools-traditional-simplified", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "ztools-traditional-simplified", + "version": "1.0.0", + "dependencies": { + "opencc-js": "^1.0.5" + } + }, + "node_modules/opencc-js": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/opencc-js/-/opencc-js-1.3.1.tgz", + "integrity": "sha512-EyKDnjHNYlxo3Blll0OGH5qcyZBI3YmXpqeDEity/db50A5TFfCdvylrBlTyx1XvlSRUmh2a5AHvSf89h4u87Q==", + "license": "MIT" + } + } +} diff --git a/plugins/traditional-simplified-cn/package.json b/plugins/traditional-simplified-cn/package.json new file mode 100644 index 00000000..62fc10e3 --- /dev/null +++ b/plugins/traditional-simplified-cn/package.json @@ -0,0 +1,10 @@ +{ + "name": "ztools-traditional-simplified", + "version": "1.0.0", + "private": true, + "type": "commonjs", + "description": "ZTools 插件:繁体字与简体字互转(基于 opencc-js)", + "dependencies": { + "opencc-js": "^1.0.5" + } +} diff --git a/plugins/traditional-simplified-cn/plugin.json b/plugins/traditional-simplified-cn/plugin.json new file mode 100644 index 00000000..8708a37f --- /dev/null +++ b/plugins/traditional-simplified-cn/plugin.json @@ -0,0 +1,29 @@ +{ + "name": "traditional-simplified-cn", + "title": "繁简转换", + "description": "剪贴板有文本时在启动器中推送入口;亦可手动打开(OpenCC)", + "version": "1.0.0", + "author": "lecoix", + "preload": "preload.js", + "main": "index.html", + "logo": "logo.png", + "features": [ + { + "code": "convert-clipboard", + "explain": "剪贴板有文本时推送", + "cmds": [ + { + "type": "over", + "label": "繁简转换(剪贴板)", + "minLength": 0, + "maxLength": 50000 + } + ] + }, + { + "code": "convert-ui", + "explain": "手动打开(剪贴板为空时)", + "cmds": ["繁简转换", "繁简"] + } + ] +} diff --git a/plugins/traditional-simplified-cn/preload.js b/plugins/traditional-simplified-cn/preload.js new file mode 100644 index 00000000..e70cd59c --- /dev/null +++ b/plugins/traditional-simplified-cn/preload.js @@ -0,0 +1,87 @@ +const { clipboard } = require('electron') + +let OpenCC +try { + OpenCC = require('opencc-js') +} catch (e) { + console.error( + '[繁简转换] 未安装依赖。请在插件根目录打开终端执行: npm install' + ) + throw e +} +OpenCC = OpenCC && OpenCC.default ? OpenCC.default : OpenCC + +function readClipboardTextTrimmed() { + try { + return (clipboard.readText() || '').trim() + } catch (e) { + return '' + } +} + +/** 从主搜索框选中「剪贴板」推送项时,暂存全文供进入 UI 后注入 */ +let pendingClipText = null + +function buildConverters(variant) { + const fromTrad = variant === 'hk' ? 'hk' : 'tw' + return { + toSimplified: OpenCC.Converter({ from: fromTrad, to: 'cn' }), + toTraditional: OpenCC.Converter({ from: 'cn', to: fromTrad }) + } +} + +let converters = buildConverters('tw') + +window.nodeAPI = { + setVariant: function (v) { + converters = buildConverters(v === 'hk' ? 'hk' : 'tw') + }, + toSimplified: function (text) { + return converters.toSimplified(text || '') + }, + toTraditional: function (text) { + return converters.toTraditional(text || '') + } +} + +if (window.ztools && typeof window.ztools.onMainPush === 'function') { + window.ztools.onMainPush( + function () { + var text = readClipboardTextTrimmed() + if (!text) return [] + var oneLine = text.replace(/\s+/g, ' ') + var preview = + oneLine.length > 40 ? oneLine.slice(0, 40) + '…' : oneLine + return [ + { + icon: 'logo.png', + text: '繁简转换(剪贴板)', + title: '繁简转换(剪贴板)', + description: preview, + _clipboardText: text + } + ] + }, + function (selectData) { + if (selectData && typeof selectData._clipboardText === 'string') { + pendingClipText = selectData._clipboardText + } + return true + } + ) +} + +if (window.ztools && typeof window.ztools.onPluginEnter === 'function') { + window.ztools.onPluginEnter(function (action) { + var detail = action + if (pendingClipText != null) { + detail = Object.assign({}, action, { + code: 'convert-clipboard', + type: 'text', + payload: pendingClipText + }) + pendingClipText = null + } + window.dispatchEvent(new CustomEvent('plugin-enter', { detail: detail })) + }) +}