magpie-select-translate 0.2.2 → 0.3.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/README.md CHANGED
@@ -43,14 +43,15 @@ Ctrl+C 在这些程序里有副作用,命中时只提示「该应用不支持
43
43
  | `target` | `auto` / `zh` / `en` |
44
44
  | `glossary` | 术语表正文,每行 `原文=译文` |
45
45
 
46
- 设置页在沙箱里没有网络(`connect-src 'none'`),所以它只读写 `magpie.options`;
47
- 插件在拉起 helper 时把这几项转成命令行参数,helper 落地成自己的 `settings.json`。
48
- 改完要重启 helper(结束 `magpie-translate-helper.exe`,或重新加载插件)才生效。
46
+ 设置页由 helper 自己提供:打开插件的页面会直接跳到 <http://127.0.0.1:3477/>(端口可用 `port` 改),
47
+ 那里能读网关的模型列表,热键、模型、目标语言、术语表都在那儿改——热键改完立即生效,其余保存即生效。
48
+ 插件只把 `port` 传给 helper,其它设置一律以 helper 的 `settings.json` 为准
49
+ (`%LOCALAPPDATA%\magpie-translate\settings.json`),magpie 重启时不会拿旧 `options` 把你刚改的设置盖回去。
49
50
  翻译状态请看浮窗本身。
50
51
 
51
52
  ## 隐私
52
53
 
53
- - helper 只监听 `127.0.0.1`,校验 `Host`;带浏览器 `Origin` 的请求一律拒绝,写请求必须是 `application/json`。
54
+ - helper 只监听 `127.0.0.1`,校验 `Host`;带浏览器 `Origin` 的请求必须带设置页的随机令牌(每次启动重新生成,只注入给设置页自己的脚本)才放行,写请求必须是 `application/json`。
54
55
  - 网关密钥从 magpie 的 `caller-keys.json` 只读取用,只放内存,不落日志。
55
56
  - 日志只有时间、事件、耗时、字数,**不记原文**。
56
57
  - 取词后会把剪贴板全量还原(图片等非 HGLOBAL 格式除外)。
@@ -58,13 +59,14 @@ Ctrl+C 在这些程序里有副作用,命中时只提示「该应用不支持
58
59
  ## 卸载
59
60
 
60
61
  1. `magpie plugin remove <包名>` 移除插件。
61
- 2. 结束残留的 helper:任务管理器结束 `magpie-translate-helper.exe`,
62
+ 2. 结束残留的 helper:任务管理器结束 `helper.exe`,
62
63
  或运行 `%LOCALAPPDATA%\magpie-translate\uninstall.ps1`(插件首次启动时会把这份脚本拷过去)。
63
64
 
64
65
  helper 只在 magpie 启动插件时被拉起,magpie 不开它就不会常驻,也不写注册表。
65
66
 
66
67
  ## 更新
67
68
 
69
+ - 0.3.0:设置页改由 helper 自己提供(能读网关模型列表,热键改完立即生效,不用重启 helper);浮窗里原文和译文合成一块可滚动内容,长原文不再被截断在 3 行;`scripts\install.ps1` 现在真的会先停掉旧 helper(之前按错的进程名杀,升级后跑的还是老版本)。
68
70
  - 0.2.2:显示名改成包名 `magpie-select-translate`(`magpie.name` 之前是 "Select Translate",发现页卡片上看到的就是它)。
69
71
  - 0.2.1:修安全漏洞:本地接口拒绝带浏览器 `Origin` 的请求,写请求必须是 `application/json`;术语表只接收正文(`glossary`),不再按路径读本地文件。`package.json` 去掉 BOM。
70
72
  - 0.2.0:浮窗加了浅绿描边;正文改成整行取整、滚动条常驻,长译文不会再被切掉半行;顶部可以直接按住拖动(之前 `WM_NCHITTEST` 把屏幕坐标当客户区坐标用,拖不动);标题改父窗口自绘,修掉连续取词时的残影。
package/bin/helper.exe CHANGED
Binary file
package/index.mjs CHANGED
@@ -51,13 +51,9 @@ export default {
51
51
  "helper 未安装:运行仓库里的 scripts/install.ps1,或安装带 bin/helper.exe 的发行包",
52
52
  }
53
53
  }
54
- // 页面只能写 magpie.options,所以设置由这里转成命令行交给 helper(helper 落地成自己的 settings.json)
55
- const args = ["--port", String(port)]
56
- for (const k of ["hotkey", "model", "target", "glossary"]) {
57
- const v = options?.[k]
58
- if (v) args.push(`--${k === "glossary" ? "glossary-text" : k}`, String(v))
59
- }
60
- spawn(exe, args, {
54
+ // 只传端口:设置以 helper 自己的 settings.json 为准,
55
+ // 插件宿主重启时不能拿 options 里的旧值把用户刚改的设置盖回去。
56
+ spawn(exe, ["--port", String(port)], {
61
57
  detached: true,
62
58
  stdio: "ignore",
63
59
  windowsHide: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "magpie-select-translate",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "划词翻译:选中文字按热键,鼠标旁浮窗流式显示 magpie 模型的译文。Select-to-translate popover for magpie: hit a hotkey on any selection and the translation streams next to your cursor.",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",
@@ -37,11 +37,7 @@
37
37
  "file": "page/index.html"
38
38
  },
39
39
  "options": {
40
- "port": 3477,
41
- "hotkey": "Alt+D",
42
- "model": "",
43
- "target": "auto",
44
- "glossary": ""
40
+ "port": 3477
45
41
  }
46
42
  }
47
43
  }
package/page/index.html CHANGED
@@ -1,120 +1,43 @@
1
1
  <!doctype html>
2
2
  <html lang="zh">
3
3
  <head>
4
- <meta charset="utf-8">
5
- <title>划词翻译</title>
6
- <style>
7
- :root { color-scheme: light dark; }
8
- body {
9
- margin: 0; padding: 20px 22px;
10
- font: 14px/1.7 "Microsoft YaHei UI", "Segoe UI", system-ui, sans-serif;
11
- }
12
- h1 { font-size: 16px; margin: 0 0 4px; }
13
- p.sub { margin: 0 0 18px; opacity: .7; font-size: 12px; }
14
- label { display: block; margin: 14px 0 4px; font-size: 12px; opacity: .8; }
15
- input, select, textarea {
16
- width: 100%; box-sizing: border-box; padding: 7px 9px; font: inherit;
17
- border: 1px solid color-mix(in srgb, currentColor 20%, transparent);
18
- border-radius: 8px; background: transparent; color: inherit;
19
- }
20
- .row { display: flex; gap: 10px; }
21
- .row > div { flex: 1; }
22
- #status { margin-top: 16px; font-size: 12px; opacity: .75; min-height: 1.4em; }
23
- .hint { margin-top: 18px; font-size: 12px; opacity: .6; }
24
- code { font-family: Consolas, monospace; }
25
- </style>
4
+ <meta charset="utf-8">
5
+ <title>划词翻译</title>
6
+ <style>
7
+ :root { color-scheme: light dark; }
8
+ body {
9
+ margin: 0; padding: 20px 22px;
10
+ font: 14px/1.7 "Microsoft YaHei UI", "Segoe UI", system-ui, sans-serif;
11
+ }
12
+ h1 { font-size: 16px; margin: 0 0 4px; }
13
+ p { margin: 0 0 8px; font-size: 12px; opacity: .7; }
14
+ code { font-family: Consolas, monospace; }
15
+ </style>
26
16
  </head>
27
17
  <body>
28
18
  <h1>划词翻译</h1>
29
- <p class="sub">选中文字后按热键,浮窗流式显示译文。</p>
30
-
31
- <div class="row">
32
- <div>
33
- <label for="hotkey">热键(改完重启 helper 生效)</label>
34
- <input id="hotkey" placeholder="Alt+D">
35
- </div>
36
- <div>
37
- <label for="target">目标语言</label>
38
- <select id="target">
39
- <option value="auto">自动判定</option>
40
- <option value="zh">中文</option>
41
- <option value="en">English</option>
42
- </select>
43
- </div>
44
- </div>
45
-
46
- <label for="model">模型 id(留空用网关第一个模型)</label>
47
- <input id="model" placeholder="group/deepseek-v4.1-flash">
48
-
49
- <label for="glossary">术语表(每行 原文=译文,只存在 helper 的设置里,不读任何文件)</label>
50
- <textarea id="glossary" rows="4" placeholder="magpie=喜鹊"></textarea>
51
-
52
- <label for="port">helper 端口</label>
53
- <input id="port" type="number" placeholder="3477">
54
-
55
- <div id="status"></div>
56
- <p class="hint">
57
- 设置页在沙箱里不能联网,所以这里只写 magpie 的选项;helper 下次启动时按这些选项运行,改完重启 helper 生效。
58
- 翻译状态请按热键看浮窗。helper 未安装时运行 <code>scripts/install.ps1</code>。
59
- </p>
19
+ <p id="said">正在打开设置页…</p>
20
+ <p>如果一直没跳转,说明 helper 没起来:设置页由 helper 提供,模型列表也要从本地网关读。
21
+ 跑一次 <code>scripts\install.ps1</code>,或重开一次 magpie。</p>
60
22
 
61
23
  <script>
62
- const fields = ["hotkey", "model", "target", "glossary", "port"]
63
- const $ = (id) => document.getElementById(id)
64
- let timer = null
65
-
66
- const status = (msg, ok) => {
67
- $("status").textContent = msg
68
- $("status").style.color = ok === false ? "#e5484d" : ""
69
- }
70
-
71
- async function boot() {
72
- const api = window.magpie
73
- if (!api || !api.options) {
74
- status("不在 magpie 里打开时无法保存设置", false)
75
- return
76
- }
77
- // magpie.ready 是个 Promise:宿主把语言/主题/选项准备好后才会 resolve。
78
- try {
79
- await api.ready
80
- } catch {
81
- /* 没有宿主时保持默认 */
82
- }
24
+ // 带路页:页面在沙箱里(connect-src 'none'),除了解释跳转什么网都联不上,
25
+ // 所以不能先探活,直接跳到 helper 自己的设置页——那边能读网关模型、能写设置。
26
+ (async () => {
27
+ const fallback = 3477
28
+ let port = fallback
83
29
  try {
84
- const opts = (await api.options.get()) || {}
85
- for (const f of fields) if (opts[f] !== undefined) $(f).value = opts[f]
86
- status("")
87
- } catch (e) {
88
- status("读取设置失败:" + e.message, false)
89
- return
90
- }
91
- for (const f of fields) {
92
- $(f).addEventListener("input", () => {
93
- clearTimeout(timer)
94
- timer = setTimeout(save, 1000)
95
- })
96
- $(f).addEventListener("change", () => {
97
- clearTimeout(timer)
98
- timer = setTimeout(save, 300)
99
- })
100
- }
101
- }
102
-
103
- async function save() {
104
- const opts = {}
105
- for (const f of fields) {
106
- const v = $(f).value.trim()
107
- if (v !== "") opts[f] = f === "port" ? Number(v) : v
108
- }
109
- try {
110
- await window.magpie.options.set(opts)
111
- status("已保存 · " + new Date().toLocaleTimeString())
112
- } catch (e) {
113
- status("保存失败:" + e.message, false)
114
- }
115
- }
116
-
117
- boot()
30
+ const opts = await window.magpie?.options?.get?.()
31
+ if (opts && Number(opts.port) > 0) port = Number(opts.port)
32
+ } catch { /* 没有宿主就用默认端口 */ }
33
+ const target = "http://127.0.0.1:" + port + "/"
34
+ // WebKit/WebView2 跳不过去时不报错,页面会停在“正在打开”。5 秒后还没被替换就提示一下。
35
+ setTimeout(() => {
36
+ const said = document.getElementById("said")
37
+ if (said) said.textContent = "没能连上 helper(127.0.0.1:" + port + ")"
38
+ }, 5000)
39
+ location.replace(target)
40
+ })()
118
41
  </script>
119
42
  </body>
120
- </html>
43
+ </html>