使用 Javascript 访问剪贴板 - 没有 Flash?

发布于 2024-08-10 18:51:12 字数 293 浏览 4 评论 0原文

有没有可靠的方法使用 Javascript 访问客户端计算机的剪贴板?尝试执行此操作时,我继续遇到权限问题。 Google 文档是如何做到这一点的?他们使用Flash吗?

我的主要目标是 IE8,但也想支持 FF 和 Chrome。

我已经看到了使用 Flash 执行此操作的技术,但我正在寻找纯 js 路线:
使用 Flash 访问剪贴板

Is there a reliable way to access the client machine's clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use Flash?

My primary target is IE8, but would like to support FF and Chrome also.

I have seen the technique to do this using Flash, but am looking for a pure js route:
Clipboard access using Flash

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

赤濁 2024-08-17 18:51:12

由于这是一个很大的安全风险,所以所有关心安全的浏览器都不允许JS访问剪贴板。

主要原因是许多人将密码放入文本文件中,然后使用剪切和粘贴来登录。然后,破解者可以通过破解一个流行网站并安装一些向他们发送剪贴板内容的 JS 来从剪贴板收集密码(以及可能的其他私人信息,例如您刚刚复制的 Word 文档)。

这就是为什么我一直禁用闪光灯的原因。

Since this is a big security risk, all browsers that care about safety don't allow JS to access the clipboard.

The main reason is that many people put their passwords into a text file and then use cut&paste to login. Crackers could then collect the password (and possibly other private information like the word document which you just copied) from the clipboard by cracking a popular site and installing some JS that sends them the content of the clipboard.

Which is why I have flash disabled all the time.

人海汹涌 2024-08-17 18:51:12

不,FF 和 Chrome 中没有。它可以在 IE(不确定 7 和 8,但肯定是 6)和 Flash 中运行。这就是为什么总是使用Flash。

No, not in FF and Chrome. It works in IE (not sure about 7 and 8, but definitively 6), and from Flash. That is why Flash is always used.

说不完的你爱 2024-08-17 18:51:12

忘记纯 JS。

没有用于访问剪贴板的标准 API,并且很少有浏览器实现适当的方法。

Flash 是“标准”方法。

Forget pure JS.

There is no standard API for accessing the clipboard, and few browsers implement a propriety method.

Flash is the 'standard' method.

白云不回头 2024-08-17 18:51:12

您正在寻找 execCommand 函数,至少是我能告诉的最好的函数。以下是一些资源:
在 Javascript contenteditable div 中插入文本
http://www.java2s.com/Code/JavaScriptReference/Javascript- Methods/execCommandisappliedto.htm

不幸的是,这遇到了 Flash 在 Flash 9 中密封的相同安全漏洞。由于人们向剪贴板发送垃圾邮件,因此现在只能通过直接用户交互来访问剪贴板,老实说,最好这样做方式。我敢打赌大多数浏览器都有类似的(如果不是更严格的政策)。

You're looking for the execCommand function, at least the best I can tell. Here are some resources:
Insert text in Javascript contenteditable div
http://www.java2s.com/Code/JavaScriptReference/Javascript-Methods/execCommandisappliedto.htm

Unfortunately, this runs into the same security loophole that Flash sealed in Flash 9. Since people were spamming the clipboard, the clipboard is now only accessible through direct user interaction, and honestly, it is better that way. And I'll wager that most browsers have similar (if not stricter policies).

森末i 2024-08-17 18:51:12

在 IE 中,做到这一点非常轻松。对于 Firefox,您需要更新 users.js 和/或 prefs.js(您可以在 Firefox 中通过 google 搜索剪贴板访问)。对于 Chrome,您需要编写一个扩展。

在您的扩展程序背景页面中,有一个占位符(IFrame)
在您的网页中,有“剪切”、“复制”和“粘贴”等按钮或链接。您的页面上还有一个隐藏的 iframe Paste_holder,用于取回扩展程序的 background_page 读取的文本。在扩展程序的清单文件中,包含如下代码:

manifest.json

"background_page": "mypaste_helper.html",
"content_scripts": [
    {
        "matches": ["<all_urls>"],
        "js": ["mypaste_helper.js"],
        "all_frames": true
    }
],
"permissions": [
    "clipboardRead",
    "clipboardWrite",
    "tabs"  
]

mypaste_helper.js

获取对页面上的剪切、复制和复制按钮的引用

cutButton.addEventListener("click", function() 
{
            get selected content using window.getSelection()
            pass that text to handleCut function in mypaste_helper.html     
}, false);      
copyButton.addEventListener("click", function() 
{
            get selected content using window.getSelection()
            pass that text to handleCopy function in mypaste_helper.html 
}, false);      
pasteButton.addEventListener("click", function() 
{
            get content from handlePaste function in mypaste_helper.html 
}, false);    

在回调函数中

获取background_page函数发送的内容
使用接收到的文本设置paste_holder框架的document.body的innerHTML。

mypaste_helper.htmlhandleCopy

和handleCut是相同的

get reference to your iframe document.body as clipboardholder
set innerHTML of the clipboardholder.contentDocument.body with the data passed by mypaste_helper.js
capture selection through window.getSelection()
selection.selectAllChildren(clipboardholder);
document.execCommand('copy')
read contents of the clipboardholder
pass the text back to callback in mypaste_helper.js

handlePaste

get reference to your iframe document.body as clipboardholder
you may want to clear the contents of clipboardholder.contentDocument.body
capture selection through window.getSelection()
selection.selectAllChildren(clipboardholder);
document.execCommand('paste')
read contents of the clipboardholder
pass the text back to callback in mypaste_helper.js

In IE, to do this is pretty painless. For Firefox, you need to update users.js and/or prefs.js (you can google for Clipboard access in Firefox). For Chrome, you need to write an extension.

In you extension background_page, have a place holder (an IFrame)
in your webpage, have buttons or links like 'cut', 'copy' and 'paste'. also have a hidden iframe paste_holder on your page to get back the text read by the background_page of your extension. In your extension's manifest file, have code like below:

manifest.json

"background_page": "mypaste_helper.html",
"content_scripts": [
    {
        "matches": ["<all_urls>"],
        "js": ["mypaste_helper.js"],
        "all_frames": true
    }
],
"permissions": [
    "clipboardRead",
    "clipboardWrite",
    "tabs"  
]

mypaste_helper.js

get references to your cut, copy and copy buttons on the page

cutButton.addEventListener("click", function() 
{
            get selected content using window.getSelection()
            pass that text to handleCut function in mypaste_helper.html     
}, false);      
copyButton.addEventListener("click", function() 
{
            get selected content using window.getSelection()
            pass that text to handleCopy function in mypaste_helper.html 
}, false);      
pasteButton.addEventListener("click", function() 
{
            get content from handlePaste function in mypaste_helper.html 
}, false);    

in the callback function

get the contents sent by background_page function
set innerHTML of paste_holder frame's document.body with received text.

mypaste_helper.html

handleCopy and handleCut are identical

get reference to your iframe document.body as clipboardholder
set innerHTML of the clipboardholder.contentDocument.body with the data passed by mypaste_helper.js
capture selection through window.getSelection()
selection.selectAllChildren(clipboardholder);
document.execCommand('copy')
read contents of the clipboardholder
pass the text back to callback in mypaste_helper.js

handlePaste

get reference to your iframe document.body as clipboardholder
you may want to clear the contents of clipboardholder.contentDocument.body
capture selection through window.getSelection()
selection.selectAllChildren(clipboardholder);
document.execCommand('paste')
read contents of the clipboardholder
pass the text back to callback in mypaste_helper.js
春庭雪 2024-08-17 18:51:12

http://www.rodsdot.com/ee/cross_browser_clipboard_copy_with_pop_over_message.asp 实现 ZeroClipboard flash 对象正确并且是跨浏览器的。它还讨论了 ZeroClipboard 的潜在问题和可能的解决方法。还兼容 Flash 10+。

http://www.rodsdot.com/ee/cross_browser_clipboard_copy_with_pop_over_message.asp implements the ZeroClipboard flash object correctly and is cross browser. It also discusses the potential problems with ZeroClipboard and possible work-arounds. Also compatible with Flash 10+.

寄离 2024-08-17 18:51:12

这是一个纯 JS 实现,可让您粘贴适用于 Google Chrome 的图像数据: http://strd6.com/2011/09/html5-javascript-pasting-image-data-in-chrome/

Here's a pure JS implementation that lets you paste image data which works in Google Chrome: http://strd6.com/2011/09/html5-javascript-pasting-image-data-in-chrome/

再见回来 2024-08-17 18:51:12

这个问题很清楚,但我仍然有疑问,因为可以选择在 javascript 中执行此操作,另一件事是对于 Windows 窗体,完全可以使用命令

Clipboard.Clear()

Ref: System.Windows.Forms

任何可以发挥作用的恶意软件。

It was clear this issue, but I still have doubts because there is the option to do this in javascript and another thing is for windows forms it is totally possible to do using the command

Clipboard.Clear()

Ref: System.Windows.Forms

Any malicious software that can do well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文