Store selected text from HTMLviewer in a variable

All,

I’m trying to build an Windows tool that will help me download a webpage (done!) and when I load the local file in the HTMLviewer, I would like to select text (in Arabic, Japanese, Korean… all languages I don’t read) and put tags around it (to markup terminology that needs to be added to a glossary for translators). Is there a way to do this?

The problem I have is I can’t find a way to store the selected text in a variable.

Thanks for your help,

Gert

Well, you may want to check our MBS Win Plugin:
http://www.monkeybreadsoftware.de/xojo/plugin-win.shtml

We have a couple of methods to extend the Xojo HTMLViewer for Windows:
http://monkeybreadsoftware.net/pluginpart-windowshtmlviewer.shtml

e.g. IEHTMLTextMBS as string function gives you the html text of a webpage.

Christian, does IEHTMLTextMBS store the selected text from a web page or the whole web page?

I only need the selected text.

thanks

Gert

IEHTMLTextMBS is for the whole page.
Same for IETextMBS, but maybe you an programmatically find the text?

What would be the way to find the selected text programmatically, Christian?
If I cannot see the start & stop position of the selected text, I don’t see how I can get it.

thanks

It’s up to you to define that.
Maybe load all the text into a textarea and let the user copy & paste it.
Or maybe in HTML you can find some HTML tags and pick text between.

Check the DecodingFromHTMLMBS(s as string) as string function, too.

Christian, I read all the documention and I’m not sure if you understand the problem I’m trying to find a solution for: I cannot make the user work in the text-view of an HTML file; he needs to select a word in the HTML window and I should be able to find that word preferably by an offset (line & position number) or by DOM info.

I don’t have a problem decoding the text, in fact, it does not need to be decoded. I just need to edit the HTML and add a marker, wrapped around the selected word.

The problem is: I don’t see how I can get any info about the start an stop position of the selected text, or how I can even copy the selected text. It’s like XOJO can’t get inside the HTMLviewer…

thanks

gert

Well, which platforms do you use?
And on Windows is it only native or webkit as engine for HTMLViewer?

Maybe I can add functions to get selected text there.

I’m using Windows now; Not yet sure what my users will work on, but if I have a solution that works only on Windows, it would be great. I can use whatever engine – don’t have a preference.

If you would have a solution, I would definitely go for this. As I need to put tags around the selected text, having the start and stop position would also be most helpful.

thanks, Christian.

gert

[quote=338544:@Gert Van Assche]I’m using Windows now; Not yet sure what my users will work on, but if I have a solution that works only on Windows, it would be great. I can use whatever engine – don’t have a preference.

If you would have a solution, I would definitely go for this. As I need to put tags around the selected text, having the start and stop position would also be most helpful.

thanks, Christian.

gert[/quote]

If you have a string you can use String.Left String.Mid And String.Right

execute a javascript that returns you the selected text ?
I’m not at all a javascript expert, but I think this could be done.

function getSelectionText() { var text = ""; if (window.getSelection) { text = window.getSelection().toString(); } else if (document.selection && document.selection.type != "Control") { text = document.selection.createRange().text; } return text; }

Edit: I don’t know how you can send this javascript selected text back to Xojo !

Edit2 : seems not possible but found this :

[quote]The MonkeyBreadSoftware (MBS) plugins have some methods to do this.

Another way: the Document.title hack. Change document.title in the javascript to return the value, then use the htmlviewer.titleChanged event to capture the result. Not elegant, but does work.[/quote]
so you could store the text selected start and length in the document title, and so get them back into Xojo

Edit 3 : you should fill a feature request !

You can return values to Xojo through Titlechanged.

On macOS you can use the following extends method (in a module)

[code]Public Function EvaluateJavaScript(Extends viewer As HTMLViewer, script As Text) as Text
DIM result As Text

#if TargetCocoa
Declare Function stringByEvaluatingJavaScriptFromString Lib “Cocoa” Selector “stringByEvaluatingJavaScriptFromString:” (id As integer, script As CFStringRef) As CFStringRef
result = stringByEvaluatingJavaScriptFromString(viewer.Handle, script)
#endif

Return result
End Function
[/code]

To all that contributed and help me find a solution: thank you!