代码之家  ›  专栏  ›  技术社区  ›  Chris Ballance

使用Javascript-sans Flash访问剪贴板?

  •  9
  • Chris Ballance  · 技术社区  · 15 年前

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

    我见过使用Flash实现这一点的技术,但我正在寻找一种纯js路线:
    Clipboard access using Flash

    8 回复  |  直到 8 年前
        1
  •  11
  •   Aaron Digulla    15 年前

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

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

    这就是我一直禁用flash的原因。

        2
  •  3
  •   Marius    15 年前

    不,不是在FF和Chrome中。它适用于IE(不确定7和8,但肯定是6)和Flash。这就是为什么总是使用Flash。

        3
  •  2
  •   Quentin    15 年前

    忘记纯JS吧。

        4
  •  2
  •   Community CDub    8 年前
        5
  •  2
  •   Surya Upadhyayula    13 年前

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

    "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);    
    

    在回调函数中

    获取后台页面函数发送的内容 使用接收到的文本设置粘贴支架框架的document.body的innerHTML。

    mypaste_helper.html

    handleCopy和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
    

    手铐

    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
    
        6
  •  1
  •   rdivilbiss    14 年前

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

        7
  •  0
  •   Daniel X Moore mcdon    13 年前

    下面是一个纯JS实现,可以让您粘贴在Google Chrome中工作的图像数据: http://strd6.com/2011/09/html5-javascript-pasting-image-data-in-chrome/

        8
  •  0
  •   Fernando Arce    10 年前

    这个问题很明显,但我仍然有疑问,因为javascript中有这样做的选项,另一件事是windows窗体完全可以使用该命令

    Clipboard.Clear()
    

    裁判: System.Windows.Forms