Htmlviewer Select All & Copy with Javascript

I am displaying a web page with the desktop HTMLViewer that is nothing but text, no HTML. I researched what Javascript commands should work. Then I am using a toolbar button to execute the following

htmlviewer.ExecuteJavaScript (“document.execCommand(‘selectall’,null,false);”)
htmlviewer.ExecuteJavaScript (“document.execCommand(‘copy’);”)

The first command does select everything ( all the text ), but nothing gets placed on the clipboard. Is this a security thing or me not doing the right Javascript function?

JavaScript cannot interact with the clipboard unless initiated by a user (i.e. a button on the webpage was clicked) as a security measure.

A toolbar button executing JavaScript from outside the WebView likely does not satisfy this requirement.

Security

Here’s some old code that I was using way back… The Mac code still works, but not able to test the Windows and Linux code to see if they still work…

All of this gets added to a module

[code]Public Sub Copy(Extends viewer As HTMLViewer)
’ Copies the current selection inside the HTMLViewer to the clipboard.

#if TargetCocoa
Declare Sub copy Lib “Cocoa” Selector “copy:” (id As Integer, sender As Integer)
copy viewer.Handle, viewer.Handle

#elseif TargetLinux
Soft Declare Function gtk_bin_get_child Lib “libgtk” (bin As Integer) as Integer
Soft Declare Sub webkit_web_view_copy_clipboard Lib “libwebkitgtk-1.0.so” (web_view As Integer)
webkit_web_view_copy_clipboard(gtk_bin_get_child(viewer.Handle))

#elseif TargetWin32
DIM t_cef_browser As MemoryBlock = Ptr(viewer.Handle)
DIM t_get_focused_frame As NEW CefGetFocusedFrameDelegate(t_cef_browser.Ptr(80))
DIM t_cef_frame As MemoryBlock = Ptr(t_get_focused_frame.Invoke(t_cef_browser))
DIM t_cef_frame_copy As NEW CefFrameCopyDelegate(t_cef_frame.Ptr(28))
t_cef_frame_copy.Invoke(t_cef_frame)
#endif
End Sub
[/code]

Private Sub CefFrameCopyDelegate(p_cef_frame_t As Ptr)
Private Function CefGetFocusedFrameDelegate(p_cef_browser_t As Ptr) as Integer

Please check MBS Plugin classes for IE

http://www.monkeybreadsoftware.net/pluginpart-windowshtmlviewer.shtml

And Chromium

http://www.monkeybreadsoftware.net/pluginpart-chromium.shtml

Thanks Shao for the code, I’ll test it hopefully next time I need to copy text I had to create an entirely different setup to work around this issue due to the responses saying it can not be done due to security.

[quote=339139:@shao sean]Here’s some old code that I was using way back… The Mac code still works, but not able to test the Windows and Linux code to see if they still work…

All of this gets added to a module

[code]Public Sub Copy(Extends viewer As HTMLViewer)
’ Copies the current selection inside the HTMLViewer to the clipboard.

#if TargetCocoa
Declare Sub copy Lib “Cocoa” Selector “copy:” (id As Integer, sender As Integer)
copy viewer.Handle, viewer.Handle

#elseif TargetLinux
Soft Declare Function gtk_bin_get_child Lib “libgtk” (bin As Integer) as Integer
Soft Declare Sub webkit_web_view_copy_clipboard Lib “libwebkitgtk-1.0.so” (web_view As Integer)
webkit_web_view_copy_clipboard(gtk_bin_get_child(viewer.Handle))

#elseif TargetWin32
DIM t_cef_browser As MemoryBlock = Ptr(viewer.Handle)
DIM t_get_focused_frame As NEW CefGetFocusedFrameDelegate(t_cef_browser.Ptr(80))
DIM t_cef_frame As MemoryBlock = Ptr(t_get_focused_frame.Invoke(t_cef_browser))
DIM t_cef_frame_copy As NEW CefFrameCopyDelegate(t_cef_frame.Ptr(28))
t_cef_frame_copy.Invoke(t_cef_frame)
#endif
End Sub
[/code]

Private Sub CefFrameCopyDelegate(p_cef_frame_t As Ptr)
Private Function CefGetFocusedFrameDelegate(p_cef_browser_t As Ptr) as Integer