如何使用GWT复制到剪贴板?

18

用谷歌搜索找不到任何相关结果。

有人知道如何通过GWT Java代码将一些文本复制到剪贴板吗?我想避免使用原始JavaScript注入解决方法。

感谢任何帮助或提示。


GWT 实际上将您的 Java 代码编译成 JavaScript,因此当它运行时,实际上是 JavaScript 在运行。 - helloandre
我知道,但如果我不写它,我就不必担心它能在所有浏览器上正常工作。 - JohnIdol
我指的是从GWT代码中进行原始JavaScript注入。 - JohnIdol
2
我知道这篇文章很老,但是在网上找到做这件事的好例子仍然非常困难。Cedric在下面的回答中提供了最接近解决这个问题的东西。如果有人有好的链接,我会很乐意接受!-=干杯 - Jonathan
@Jonathan很不幸地从未弄清楚 - JohnIdol
6个回答

8

只需使用提供的答案https://dev59.com/fHRC5IYBdhLWcg3wJNcN#30810322即可。

因此,您可以将任何文本传递给JavaScript本地函数/方法,js函数会创建一个新元素并将其复制到剪贴板中,在复制后删除该元素。

在新版浏览器中不需要使用任何库。

所以:

public static native void copyTextToClipboard(String text) /*-{
        var textArea = document.createElement("textarea");
        //
        // *** This styling is an extra step which is likely not required. ***
        //
        // Why is it here? To ensure:
        // 1. the element is able to have focus and selection.
        // 2. if element was to flash render it has minimal visual impact.
        // 3. less flakyness with selection and copying which **might** occur if
        //    the textarea element is not visible.
        //
        // The likelihood is the element won't even render, not even a flash,
        // so some of these are just precautions. However in IE the element
        // is visible whilst the popup box asking the user for permission for
        // the web page to copy to the clipboard.
        //

        // Place in top-left corner of screen regardless of scroll position.
        textArea.style.position = 'fixed';
        textArea.style.top = 0;
        textArea.style.left = 0;

        // Ensure it has a small width and height. Setting to 1px / 1em
        // doesn't work as this gives a negative w/h on some browsers.
        textArea.style.width = '2em';
        textArea.style.height = '2em';

        // We don't need padding, reducing the size if it does flash render.
        textArea.style.padding = 0;

        // Clean up any borders.
        textArea.style.border = 'none';
        textArea.style.outline = 'none';
        textArea.style.boxShadow = 'none';

        // Avoid flash of white box if rendered for any reason.
        textArea.style.background = 'transparent';


        textArea.value = text;

        document.body.appendChild(textArea);

        textArea.select();

        try {
            var successful = document.execCommand('copy');
        } catch (err) {
            console.log('Unable to copy');
        }
        document.body.removeChild(textArea);
    }-*/;

5

ZeroClipboard 的官方网站是 http://zeroclipboard.org/,源代码仓库位于 https://github.com/zeroclipboard/zeroclipboard。 - Stéphane B.

5
以下代码在Chrome中对我很有效: ```html

以下代码在Chrome中对我很有效:

```
public static native void copyToClipboard() /*-{
    var selection = $wnd.getSelection();
    var text =  $doc.getElementById("myElement");
    var range = $doc.createRange();
    range.selectNodeContents(text);
    selection.removeAllRanges();
    selection.addRange(range);
    $doc.execCommand('copy');
    selection.removeAllRanges();
}-*/;

3

GWT并不直接支持$doc.execCommand('copy');命令,但是解决这个问题还是很容易的。

首先将焦点放在该项上,选择文本,然后复制即可。

myTextBox.setFocus(true);
myTextBox.selectAll();
boolean success = copyToClipboard();

private static native boolean copyToClipboard() /*-{
    return $doc.execCommand('copy');
}-*/;

你这个太牛了,真是个大脑高手。谢谢 :) - Micheal C Wallas

3

目前似乎没有任何GWT库提供此功能。无论如何,由于需要使用Flash,因此不可能在所有浏览器中支持此功能。一个非常好的库可以包装这个功能是ZeroClipboard


已过时,请参见下文。 - ArcTanH

1

我这里提供一种无需使用本地JS,而是使用gwt elemental的解决方案,灵感来自@SushmithaShenoy,将其留在此处以备将来参考。

前提条件:

import elemental.client.Browser;
import elemental.html.Selection;
import elemental.ranges.Range;

Label.getElement().setAttribute("id","your_element_id"); //unique ID!

现在是“真正”的代码,可能放置在点击处理程序中:

final Selection selection = Browser.getWindow().getSelection();
final Range range = Browser.getDocument().createRange();
range.selectNodeContents(Browser.getDocument().getElementById(""you_elements_id"));
selection.removeAllRanges();
selection.addRange(range);
Browser.getWindow().getDocument().execCommand("copy", false, "");
selection.removeAllRanges();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接