I have a HTMLViewer with some contents in it.
If I right click on the HTMLViewer and Select All and copy its contents I can CTRL-V (paste) in any place.
Not a text, but a real web page (I am using Windows).
I’d like to know if I can do this by code and how.
Thanks for any help.
I did this as two extension methods just to make it a bit more apparent what’s happening. These could be combined in to a SelectAllAndCopy method, though.
In a module, add the following methods:
Public Sub SelectAll(extends h as DesktopHTMLViewer)
var exec() as string
exec.Add( "var selection = window.getSelection()," )
exec.Add( " range = document.createRange();" )
exec.Add( "range.selectNodeContents(document.body);" )
exec.Add( "selection.removeAllRanges();" )
exec.Add( "selection.addRange(range);" )
h.ExecuteJavaScript( String.FromArray( exec, "" ) )
End Sub
Public Sub Copy(extends h as DesktopHTMLViewer)
h.ExecuteJavaScript( "document.execCommand('copy');" )
End Sub
Call as:
HTMLViewer1.SelectAll
HTMLViewer1.Copy
This does use the old document.execCommand to perform the copy operation, which is in the process of being deprecated but still works everywhere we care about (Xojo HTMLViewer renderers). There’s a newer JavaScript Clipboard API that may wish to implement in order to be future proof.
1 Like
Thank you Anthony.
It looks promising.
I’ll do it as you show and I will tell you the results.
It works great! Thanks!
Now I will try to develop it with the Clipboard API and I will tell you how it goes.
1 Like
Happy to help, and I look forward to hearing about your adventures in JavaScript!