Tuesday, October 25, 2022

Re: Modern browsers support for Copyclipboard functionality

The  doc.execCommand('copy'), while deprecated, still works.  Eg:

@UiField TextBox myTextBox;

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

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

If you run your site over HTTPS, the user grants permissions, and you jump though lots of hoops, you can also use the $wnd.clipboardData.readText() and $wnd.clipboardData.writeText(myText).

On Saturday, 22 October 2022 at 8:10:57 pm UTC+11 vas...@gmail.com wrote:
Well it's the event "copy" as in my example. You need an eventHandler and then the event is passed as argument to your handler.

Hope that helps.

On Thu, Oct 20, 2022 at 7:47 PM Pramod Patil <patil.p...@gmail.com> wrote:
Thanks again,
In below method - how to get "event " ?  so as to use event.clipboard.setData("text/plain", data), if I can get event handle then I may achieve desired results. 
public static native String setClipboardData(String data)/*-{

if($wnd.clipboardData && clipboardData.setData){
return $wnd.clipboardData.setData('Text',Data);
}
}
}-

On Thu, Oct 20, 2022 at 5:17 PM Vassilis Virvilis <vas...@gmail.com> wrote:
I believe I did

It's this snippet for copy

event.clipboardData.setData("text/plain", data);

On Thu, Oct 20, 2022 at 1:52 PM Pramod Patil <patil.p...@gmail.com> wrote:
Thank you again, I understand security implications but objective is to make it work for Edge, I have control over native methods, getClipBoardata and setClipboardData(String data)  which are native methods in java. The code which I have pasted above is working fine with IE browser, but not with modern browsers. So can you help me with the code/pointers which can replace above code and work for Edge,Chrome. Thanks.

On Thursday, 20 October 2022 at 14:52:22 UTC+5:30 vas...@gmail.com wrote:
Well if I remember correctly it is not supposed to access the clipboard directly because that would be a huge security issue.

Consider the following scenario. You copy / paste your password for your bank somewhere. Then you go to a random webpage. If the javascript of this webpage that runs on your computer/browser could access (getClipboardData()) your clipboard your bank password would be compromised.

So you can only access the clipboard from inside an event handler that handles the "copy" event aka Ctrl+C.

The necessary details to access the data differ from IE and so I have posted examples.

I do not know why your code does not compile. I think native methods are not checked so they tend to give NULL errors during runtime. If that is your case then caniuse is your friend.

    Vassilis



On Thu, Oct 20, 2022 at 11:12 AM Pramod Patil <patil.p...@gmail.com> wrote:
Hi Thanks for your response, 
on both front at GWT and Javascript  I am at beginners level, will it be possible to have modification in below function, which will support copyclipboard functionality with Microsoft Edge. I tried navigator.clipboard.writeText() but code is not getting compiled.
public static native String getClipboardData()/*-{
if($wnd.clipboardData && clipboardData.setData){
return $wnd.clipboardData.getData('Text');
}

On Thu, Oct 20, 2022 at 12:36 PM Vassilis Virvilis <vas...@gmail.com> wrote:
For non IE browsers I have this:

            // attach event listeners
            // copy - cut - paste handlers
            ((Element) Js.cast(getElement())).addEventListener("copy", copy_li);
            ((Element) Js.cast(getElement())).addEventListener("cut", cut_li);
            ((Element) Js.cast(getElement())).addEventListener("paste", paste_li);


where (Element) is elemental2.dom.Element

copy_li is something like this:

        final elemental2.dom.EventListener copy_li = new elemental2.dom.EventListener() {
            @Override
            public void handleEvent(elemental2.dom.Event evt) {
                final ClipboardEvent event = Js.cast(evt);
                copy(event);
            }
        };

where ClipboardEvent is elemental2.dom.ClipboardEvent

I need a copy function because I reuse it in the "Cut" functionality also.

and finally copy is something like this

   private void copy(ClipboardEvent event) {
        if (!hasData()) {  // <-- hasData() is your application specific function
            return;
        }

        // do application stuff get/iterate and finally get the data in a String form somehow...
        final String data = getData();  // application specific

        event.clipboardData.setData("text/plain", data);
        event.preventDefault();
    }

Similarly the paste evenListener:

        final elemental2.dom.EventListener paste_li = new elemental2.dom.EventListener() {
            @Override
            public void handleEvent(elemental2.dom.Event evt) {
                final ClipboardEvent event = Js.cast(evt);
                final String data = event.clipboardData.getData("text/plain")
                event.preventDefault();
                if (data == null)
                    return;

                // now you need somehow to propagate data to the interesting parties (methods, classes etc)
                // I am using the EventBus from GWT
                getEventBus().fireEvent(new PasteEvent(data));
            }
        };

Hope that helps

     Vassilis

On Thu, Oct 20, 2022 at 9:18 AM Pramod Patil <patil.p...@gmail.com> wrote:
Hi All,
In our GWT application we are using Java native method call for Copytoclipboard functionality. Please refer below code

public static native String getClipboardData()/*-{
if($wnd.clipboardData && clipboardData.setData){
return $wnd.clipboardData.getData('Text');
}
}
}-*/ 
public static native String setClipboardData(String data)/*-{
if($wnd.clipboardData && clipboardData.setData){
return $wnd.clipboardData.setData('Text',Data);
}
}
}-*/ 

So above code is working fine with IE browser but not working with modern browser. I know this is not GWT specific question, but I don't have much knowledge with Javascript, so anyone who knows Javascript, please help here.



--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit/c7ebae01-b41f-440a-b4da-b6c1c91231e3n%40googlegroups.com.


--
Vassilis Virvilis

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit/CAKbOjEybr2Kr5hEZ12tKccfMMuR6QZGazTZuZqRHVMd0bKg3ZA%40mail.gmail.com.


--
Thanks and regards,
 Pramod Patil
 Contact +91-8975432800

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.


--
Vassilis Virvilis

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit/f7386ec6-494b-4754-94dd-cbfbb42e8f22n%40googlegroups.com.


--
Vassilis Virvilis

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit/CAKbOjEzshqm%2Bs-SUOZuuqSSH-2AR_T9nmcZ9O%2BK7Lcxoy-_R-A%40mail.gmail.com.


--
Thanks and regards,
 Pramod Patil
 Contact +91-8975432800

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.


--
Vassilis Virvilis

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit/ed7e91b1-8612-4d5a-a096-1a420191802an%40googlegroups.com.

No comments:

Post a Comment