为什么 document.execCommand("paste") 在 Google Chrome 中不起作用?
我的扩展有问题。我想从剪贴板粘贴数据。
到目前为止,我得到了这样的信息:
function pasteAndGo()
{
document.execCommand('paste')
alert("Pasted")
}
出现了警报,但没有粘贴任何内容。
我感觉 document
部分需要更改,但我不知道该怎么做。有什么想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
出于安全考虑,“合理”的浏览器不支持调用
document.execCommand("paste")
,因为它可能使脚本能够从剪贴板读取敏感数据(如密码)。这是关于剪贴板事件的
document.execCommand("...")
的兼容性矩阵:我的两点意见:
您可以使用功能检测可能的命令>document.queryCommandSupported 方法。
编辑:根据 MDN,
document.queryCommandSupported
现已弃用,不应再使用。Calling
document.execCommand("paste")
is not supported by "reasonable" browsers, because of security concerns as it might enable the script to read sensitive data (like passwords) from the clipboard.This is the compatibility matrix of
document.execCommand("...")
concerning clipboard events:My two cents to this:
You can feature detect the possible commands using the document.queryCommandSupported method.
Edit: According to MDN
document.queryCommandSupported
is now deprecated and should no longer be used.Chrome 中曾经有一个实验性的剪贴板 API,但在 Chrome 13 中被删除。Chrome
已转向更标准的
document.execCommand('paste')
、document.execCommand(' copy')
和document.execCommand('cut')
命令:在 Chrome 中,您需要添加权限到您的清单:“clipboardRead”和“clipboardWrite”。
在 Chrome 38 之前,这些剪贴板权限仅适用于扩展程序页面,例如后台脚本。从 Chrome 39 开始,内容脚本在清单文件中声明剪贴板权限后也可以使用这些剪贴板 API。
clipboardRead/clipboardWrite
权限脚本 - Chromium 问题 (#395376)There used to be an experimental clipboard API in Chrome, but this was removed in Chrome 13.
Chrome has moved towards the more standard
document.execCommand('paste')
,document.execCommand('copy')
anddocument.execCommand('cut')
commands:In Chrome you'll need permissions need to be added to your manifest: "clipboardRead" and "clipboardWrite".
Up until Chrome 38, these clipboard permissions were only available to extension pages such as background scripts. As of Chrome 39, content scripts can also use these clipboard APIs after declaring the clipboard permissions in the manifest file.
clipboardRead/clipboardWrite
permissions are not respected in content scripts - Chromium Issue (#395376)这对我来说在背景页面中效果很好。
当然,您的扩展程序仍然需要“clipboardRead”权限,并且您必须使用消息传递将此信息返回到您的内容脚本:
content.js:
background.js:
This works well for me in a background page.
Of course your extension still needs "clipboardRead" permission and you have to use message passing to get this information back to your content script:
content.js:
background.js:
您不能在常规页面上执行它,只能在后台页面中执行。
You can't execute it on a regular page, only in a background page.
您需要设置
clipboardRead
权限才能使用document.execCommand('paste')
和clipboardWrite
权限才能使用execCommand('复制')
和execCommand('cut')
。否则,权限将被拒绝并且不会发生任何事情。
请查看此链接了解更多详情。
You need to set the
clipboardRead
permission to usedocument.execCommand('paste')
and theclipboardWrite
permission to useexecCommand('copy')
andexecCommand('cut')
.Otherwise, the permissions will be denied and nothing will happen.
Check this link for more details.
您可以通过手动执行相同的操作来模仿粘贴:
首先关注步骤 #2 和 #3,在本例中,我检查活动元素是否是文本输入。如果是这样,我通过替换该文本框的突出显示内容并重新定位光标来模拟粘贴。
对于#1,添加一个侦听器来拦截用户的粘贴尝试:
对于#4,在 el.setSelectionRange(newCursorPos, newCursorPos); 之后添加此内容:
请注意,如果您使用的是操作响应式框架的反应式框架,基于 DOM 代表您的数据绑定,您需要将光标位置更新推迟到下一次 DOM 渲染之后。例如:
You can mimic a paste by doing the same thing yourself by hand:
Focusing on steps #2 and #3 first, in this example, I check whether the active element is a text input. If so, I simulate a paste by replacing that text box's highlighted content and repositioning the cursor.
For #1, add a listener to intercept the user's paste attempts:
For #4, add this after
el.setSelectionRange(newCursorPos, newCursorPos);
:Note that if you are using a reactive framework that manipulates the DOM on your behalf based on data binding, you will need to defer the cursor position update until after the next DOM render. For instance:
您需要一个能够接收内容的焦点控件...
有关 JS 中剪贴板的一些示例,请参见 http://www.geekpedia.com/tutorial126_Clipboard-cut-copy-and-paste-with-JavaScript.html
和 http://help.dottoro.com/ljcvtcaw.php
关于 Chrome 扩展程序参见复制/粘贴在 Chrome 扩展程序中不起作用
you need to a control in focus which is capable of receiving the content...
For some examples regarding clipboard in JS see http://www.geekpedia.com/tutorial126_Clipboard-cut-copy-and-paste-with-JavaScript.html
and http://help.dottoro.com/ljcvtcaw.php
Regarding Chrome extensions see Copy/Paste Not Working in Chrome Extension
由于在manifest v3上无法与Service Worker一起使用,经过长时间的研究,我找到了更好的解决方案:(您仍然需要在manifest中设置clipboardRead权限)
Because on manifest v3 does not work with service worker, I found a better solution after long researches: (You still need to set the clipboardRead permission in manifest)