代码之家  ›  专栏  ›  技术社区  ›  Nick Sergeant

自Flash 10以来,是否有任何新的解决方案可以通过JavaScript/Flash将多组文本复制到剪贴板?

  •  4
  • Nick Sergeant  · 技术社区  · 17 年前

    自从Flash 10推出以来,由于新的安全限制,许多流行的“复制到剪贴板”脚本已经停止工作。这里有一个仅支持Flash的解决方案:

    http://cfruss.blogspot.com/2009/01/copy-to-clipboard-swf-button-cross.html

    …尽管我正在寻找通过JS触发复制功能的能力,而不是依赖用户点击Flash对象来触发。

    有关我们目前使用的示例,请参阅:

    http://snipt.net/public

    任何“复制”链接都使用jQuery的复制插件:

    http://plugins.jquery.com/project/copy

    更新:好的,所以我尝试了ZeroClipboard。乍一看,它看起来很棒。然而 redundant code needed to enable multiple clipboard bindings 这是不可接受的。在某些情况下,将有40多个文本实例,每个实例都有自己的“复制”链接。仍在寻找更好的解决方案。..

    2 回复  |  直到 17 年前
        1
  •  4
  •   DavGarcia    17 年前

    这是一个可怕的消息,我甚至没有注意到。我也广泛使用Flash技巧。据我所知,由于浏览器安全问题,这是在不安装其他插件(除了无处不在的Flash)的情况下使复制工作的唯一方法。

    更新:经过多次恐慌和几次谷歌搜索,我偶然发现 http://code.google.com/p/zeroclipboard/ 这提供了一种与Flash 10兼容的技巧,以使副本再次工作。现在来更新一下网站。..

        2
  •  0
  •   schwerwolf    17 年前

    此解决方案仅适用于调用所需操作的按键。它的工作原理是在用户完成相关按键之前将用户的光标移动到textarea元素中。它仅适用于文本输入。我已经在firefox和chrome中实现了这一点。IE可以使用clipboardData对象(这比这种破解更可取)。

    在您的html中,您应该在某个地方创建一个具有任意大的行和列属性的textarea元素。' 剪贴板文本区域 '元素将是粘贴和复制数据的保存区域。我使用一些样式属性隐藏元素。

    脚本:

    var desiredClipboardContents = 'It works';
    
    function onCopyKeyPressed() {
       // The trick here is to populate the textarea with
       // the text you want copied before the user releases
       // the copy keystroke.
       var textarea = document.getElementById('clipboard-textarea');
       textarea.value = desiredClipboardContents;
       textarea.focus();
       textarea.select();
    }
    
    function onPasteKeyPressed() {
       var textarea = document.getElementById('clipboard-textarea');
       textarea.value = '';
       textarea.focus();
       // The trick here is to delay slurping the content
       // that arrives in the textarea element until after
       // the paste keystroke is completed. The 750 ms timeout
       // provides the necessary delay.
       setTimeout("finishedPasting", 750);
    }
    
    function finishedPasting() {
       var textarea = document.getElementById('clipboard-textarea');
       alert("Received from clipboard-paste: " + textarea.value);
    }