Skip to content

fix: let the panel copy when clipboard-write is withheld - #3

Merged
Kevinzzz6 merged 1 commit into
Kevinzzz6:mainfrom
bluechonk:fix/clipboard-copy-fallback
Nov 12, 2026
Merged

Kevinzzz6 merged 1 commit into
Kevinzzz6:mainfrom
bluechonk:fix/clipboard-copy-fallback

Conversation

@bluechonk

Copy link
Copy Markdown
Contributor

现象

面板里的复制按钮点了没反应,只弹一句「无法复制,请手动选择文本」。

两处都复现:

上下文浏览器的内容块

内容块复制失败

关于页的源码行

关于页复制失败

根因

面板跑在 zcode-sandbox:// 的 iframe 里,不是宿主 origin。宿主对 clipboard-write 的 Permissions-Policy 默认只放行宿主自身,所以:

  1. navigator.clipboard.writeText() 直接 reject —— 沙箱 frame 没有该权限
  2. copy() 的 catch 只做了 flash("copyFailed"),没有第二条路径
  3. 资源声明的 _meta.ui 里也没有 permissions.clipboardWrite,宿主因此不会为这个面板授予权限

两层都要修:权限没声明,宿主不会放行;只声明权限也不够,因为沙箱内的 async Clipboard API 仍然可能被策略挡掉,需要一个不受该策略约束的降级路径。

document.execCommand("copy") 不受 Permissions-Policy 约束,是沙箱 frame 内唯一还能到达剪贴板的路径。

改动

src/server.mjs —— 在面板资源的 _meta 里声明所需权限:

-    _meta:{ui:{csp:{connectDomains:[],resourceDomains:[]}}} },
+    _meta:{ui:{csp:{connectDomains:[],resourceDomains:[]},permissions:{clipboardWrite:{}}}} },

ui/app.mjs —— copy() 在 async API 失败后降级到离屏 <textarea> + execCommand:

 async function copy(text) {
-  try{await navigator.clipboard.writeText(text);flash("copied");}catch{flash("copyFailed");}
+  try{await navigator.clipboard.writeText(text);flash("copied");return;}catch{}
+  const scratch=document.createElement("textarea");
+  scratch.value=text;scratch.setAttribute("readonly","");
+  scratch.style.position="fixed";scratch.style.top="-1000px";scratch.style.left="-1000px";scratch.style.opacity="0";
+  document.body.append(scratch);scratch.select();scratch.setSelectionRange(0,text.length);
+  let ok=false;
+  try{ok=document.execCommand("copy");}catch{ok=false;}
+  scratch.remove();
+  flash(ok?"copied":"copyFailed");
 }

离屏 textarea 用完即移除,不改动页面布局;setSelectionRange 兜住部分浏览器 select() 只选首行的问题。

dist/ 下的 context.html 与 server.mjs 是 npm run build 的产物,一并提交。

验证

CI 四件套全绿:

npm ci      ✓
npm run check   Syntax and manifest checks passed.
npm test        122 pass / 0 fail
npm run build   ✓

构建后 git status --porcelain -- marketplace.json plugins/ 为空,产物与源码一致。

真机验证(ZCode 0.3.10 面板):

操作 剪贴板结果
上下文浏览器内容块「复制」 拿到该块完整原文
关于页源码行「复制」 https://github.com/Kevinzzz6/zcode-context

降级路径确实被执行(async API 在沙箱里必 reject),两处均落到 execCommand 并成功。

备注

改动只碰复制链路,不影响数据读取与其它交互。permissions 只声明 clipboardWrite,未申请任何额外权限。

The panel runs in a sandboxed frame served from zcode-sandbox://, and the
host's Permissions-Policy grants clipboard-write only to its own origin, so
navigator.clipboard.writeText rejected and every copy button answered
"Copy unavailable; select the text instead".

Declare clipboardWrite on the UI resource so the host can enable the feature
for the frame, and fall back to execCommand("copy") — which Permissions-Policy
does not gate — for hosts that still withhold it.
@bluechonk

Copy link
Copy Markdown
Contributor Author

Local verification of the same steps CI runs, on the PR head (bd537e6):

Step Result
npm ci pass
npm run check Syntax and manifest checks passed.
npm test 122 pass / 0 fail / 0 skipped
npm run build pass
committed marketplace matches a fresh build git status --porcelain -- marketplace.json plugins/ empty
npm run smoke stdio smoke passed: isolated sessions, resources, empty state and missing binding.

Post-run state:

  • working tree clean (git status --porcelain empty)
  • HEAD == origin/fix/clipboard-copy-fallback == bd537e6, nothing unpushed
  • the three committed artifacts match the fresh build byte for byte:
    • dist/context.html e18bc12e…
    • dist/server.mjs d3087cd0…
    • dist/worker.mjs 4f31bbda…

Caveats, so this isn't read as a substitute for CI: this ran on Windows only, with node 24.21.0 against the pinned 24.14.0 (same major line). The ubuntu/macos matrix legs still need the workflow to run.

The Context plugin run currently sits at action_required — GitHub is holding it for maintainer approval of a first-time fork contributor, so the merge button stays blocked until it is approved. Nothing has failed.

@Kevinzzz6
Kevinzzz6 merged commit d154342 into Kevinzzz6:main Nov 12, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants