Mohammad Hamdan Posted April 5, 2015 Posted April 5, 2015 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 Quote
Tim Posted April 6, 2015 Posted April 6, 2015 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 Quote
Mohammad Hamdan Posted April 9, 2015 Author Posted April 9, 2015 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. Quote
Tim Posted April 9, 2015 Posted April 9, 2015 One approach would be to declare a global variable in javascript which you then set to the value you want to appear in the clipboard. I have attached an example. Maybe this helps? UniClipboardDemo09apr2015.zip Quote
Abaksoft Posted April 10, 2015 Posted April 10, 2015 Hi محمد, For all extra request, Sencha Ext-JS can gives solution : Just put your keyword on search field : clipboard for exemple. https://www.sencha.com/forum/forumdisplay.php?87-Ext-Q-amp-A Regards... Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.