ENX Mark Known: 从等待接口改为乐观更新

背景 enX 的 Chrome 扩展会给查过的生词画下划线。主窗口点一个词会弹出浮层,浮层里有 Mark Known(标为已认识)按钮,点了之后这个词的下划线消失。 后来侧边栏的整句翻译也加上了同样的下划线(ADR-053),于是有了一个新问题:在主窗口标为已认识之后,侧边栏里这个词的下划线也应该一起消失。 原来的做法 content script → background → POST /api/mark → enx-api 写库 ↓ 成功 content script 收到回复 → 去掉主窗口下划线 主窗口要等接口成功才去掉下划线,侧边栏完全不知道这件事。 改成什么样 content script:先去掉主窗口下划线 └─ runtime.sendMessage({ type: 'markAcquainted', word }) ├─ 侧边栏:收到后立即去掉下划线,不回复 └─ background:调 enx-api,完成后回复 content script 两点变化: 主窗口先改界面,再让 background 在后台调接口,也就是常说的乐观更新(optimistic update),和它相对的是原来那种等服务端确认再改界面的做法。 侧边栏不需要新的通道。chrome.runtime.sendMessage 从 content script 发出时,会投递给扩展里所有监听 runtime.onMessage 的页面,background 和侧边栏都能收到。侧边栏只需要多注册一个监听。 为什么可以先改界面 标为已认识失败的代价很低:服务端没记上,用户下次读别的文章时这个词还会带下划线,再标一次就行。为了这个小概率的失败让用户每次都等一个网络往返,不划算。 如果改成「background 调接口成功后再通知侧边栏」,状态会更严格一致,但侧边栏要多等一个接口的时间。对这个操作来说,一致性没有那么重要。 多个接收方只能有一个回复 runtime.sendMessage 允许多个页面监听,但发送方只会收到第一个调用 sendResponse 的回复,其余的被忽略。侧边栏如果也回复,会抢在 background 前面,content script 就看不到接口的真实结果了。 ...

Created 12126-10-12 · Updated 12126-10-12 · 1 min · 95 words · -

enX

enX GitHub 仓库:wiloon/enx 开发环境 vscode 本地测试 在 Chrome 里打开 Manage Extensions: chrome://extensions/ Reload enX 在 Chrome 里打开 InfoQ 里任意一篇文章 点击扩展栏里的 enX enX 会标记文章正文部分的英文词 unit test cd /Users/wiloon/workspace/projects/enx/chrome-enx npm run test -- infoq.test.js deploy api to local vm commit and push enx to GitHub go to jenkins run deploy env DNS: 192.168.50.1:53, dnsmasq Nginx: 192.168.50.130 api: 192.168.50.36:8080 https://github.com/wiloon/enx/blob/main/enx-api/deploy.sh 维护记录 时间 修改内容 原因 2026-07-01 顶部加 GitHub 仓库链接 供 open-source-dictionaries.md 站内链接跳转到本文后,能继续跳到 GitHub 仓库

Created 17176-10-17 · Updated 1016-07-01 · 1 min · 70 words · -

Pomodoro — A Minimal Timer PWA for Always-On Phone Display

Live: https://pomodoro.wiloon.com · Source: github.com/wiloon/pomodoro-vue The Problem Sitting for long periods is hard on your back and neck. I wanted a simple reminder to stand up and move every 30 minutes. Most Pomodoro apps I tried were overly complex — they required accounts, had task lists, synced to the cloud, or sent too many notifications. I didn’t need any of that. I just needed a countdown on a screen I could glance at. ...

Created 18186-05-18 · Updated 18186-05-18 · 3 min · 547 words · -