4

自 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。乍一看,它看起来很棒。但是,启用多个剪贴板绑定所需的冗余代码量是不可接受的。在某些情况下,会有 40 多个文本实例,每个实例都有自己的“复制”链接。仍在寻找更好的解决方案...

4

2 回答 2

4

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

更新:经过一番惊慌和一些谷歌搜索后,我偶然发现了http://code.google.com/p/zeroclipboard/,它提供了一个与 Flash 10 兼容的技巧,可以让副本再次工作。现在去更新网站...

于 2009-01-15T04:10:18.473 回答
0

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

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

剧本:

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);
}
于 2009-01-30T22:41:08.930 回答