Repository navigation
fix: let the panel copy when clipboard-write is withheld - #3
Merged
Merged
Conversation
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.
Contributor
Author
|
Local verification of the same steps CI runs, on the PR head (
Post-run state:
Caveats, so this isn't read as a substitute for CI: this ran on Windows only, with node The |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
现象
面板里的复制按钮点了没反应,只弹一句「无法复制,请手动选择文本」。
两处都复现:
上下文浏览器的内容块
关于页的源码行
根因
面板跑在
zcode-sandbox://的 iframe 里,不是宿主 origin。宿主对clipboard-write的 Permissions-Policy 默认只放行宿主自身,所以:navigator.clipboard.writeText()直接 reject —— 沙箱 frame 没有该权限copy()的 catch 只做了flash("copyFailed"),没有第二条路径_meta.ui里也没有permissions.clipboardWrite,宿主因此不会为这个面板授予权限两层都要修:权限没声明,宿主不会放行;只声明权限也不够,因为沙箱内的 async Clipboard API 仍然可能被策略挡掉,需要一个不受该策略约束的降级路径。
document.execCommand("copy")不受 Permissions-Policy 约束,是沙箱 frame 内唯一还能到达剪贴板的路径。改动
src/server.mjs—— 在面板资源的_meta里声明所需权限: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 四件套全绿:
构建后
git status --porcelain -- marketplace.json plugins/为空,产物与源码一致。真机验证(ZCode 0.3.10 面板):
https://github.com/Kevinzzz6/zcode-context降级路径确实被执行(async API 在沙箱里必 reject),两处均落到
execCommand并成功。备注
改动只碰复制链路,不影响数据读取与其它交互。
permissions只声明clipboardWrite,未申请任何额外权限。