Jump to content

CopyToClipboard


Mohammad Hamdan

Recommended Posts

How can i copy text to clipboard in Web mode

 

I  have used these two lines

 

Clipboard.AsText := 'Some Text';

 

Clipboard.setTextBuf('Some Text');

 

 

works while running in the IDE, but when i deploy, this not works

 

Please any help

 

 

Delphi XE5

uniGUI 0.95.0.1046

Link to comment
Share on other sites

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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...