Jump to content

CopyToClipboard


Mohammad Hamdan
 Share

Recommended Posts

Hi Mohammad,

 

Setting Clipboard.AsText will modify the clipboard on the server. To modify the clipboard in the browser, you would need to use Javascript (unless UniGui has some built-in support for this). However, web browsers impose certain restrictions on the circumstances under which the developer can read from or write to the clipboard. The following answer on Stackexchange explains it quite well:

 

http://stackoverflow.com/a/27281043/2444493

 

I copied the Javascript code from that post into a UniGui form:
procedure TMainForm.UniFormCreate(Sender: TObject);
begin
  UniSession.AddJS(
    'var isIe = (navigator.userAgent.toLowerCase().indexOf("msie") != -1 ' +
    '           || navigator.userAgent.toLowerCase().indexOf("trident") != -1); ' +
    ' ' +
    'document.addEventListener("copy", function(e) { ' +
    '    var textToPutOnClipboard = "This is some text"; ' +
    '    if (isIe) { ' +
    '        window.clipboardData.setData("Text", textToPutOnClipboard); ' +
    '    } else { ' +
    '        e.clipboardData.setData("text/plain", textToPutOnClipboard); ' +
    '    } ' +
    '    e.preventDefault(); ' +
    '}); '
  );
end;

At runtime, when the user copies some text on this form, "This is some text" gets put into the clipboard instead (as expected).

 

I hope this helps :)

 

Link to comment
Share on other sites

Sorry, but i'am not familiar with Javascript in uniGui

 

but how to Replace the "This is some text"

 

I have used the code, but should i add this code to every element on my forms?

 

 

Could you send a sample? 

 

I used the code as follow:

 

   For those controls and text that i want user to copy, 

       >> OnClick on uniEdit  

                    CopyEvent(edit1.text)

 

procedure TMainForm.CopyEvent(FText: string);

begin
   UniSession.AddJS(
    'var isIe = (navigator.userAgent.toLowerCase().indexOf("msie") != -1 ' +
    '           || navigator.userAgent.toLowerCase().indexOf("trident") != -1); ' +
    ' ' +
    'document.addEventListener("copy", function(e) { ' +
    '    var textToPutOnClipboard = "' + FText + '"; ' +
    '    if (isIe) { ' +
    '        window.clipboardData.setData("Text", textToPutOnClipboard); ' +
    '    } else { ' +
    '        e.clipboardData.setData("text/plain", textToPutOnClipboard); ' +
    '    } ' +
    '    e.preventDefault(); ' +
    '}); '
  );
end;
 
 
In IE, this code is not working.
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...