0

Currently in my UWP app I am getting the Text-selection of a webview through

DataPackage package = await webview.CaptureSelectedContentToDataPackageAsync();

and read out the string with

await package.GetView().GetTextAsync();

This works perfectly on the PC but not on the phone.

What would be the best way to go about it on the phone? I tried injecting javascript with

window.getSelection().toString();  or document.selection.createRange().text;

but it didn't work or I used it the wrong way.

Any help would be appreciated.

UPDATE1:

Changed code to the following with the result that it still only works on the PC but not on the phone:

  string texttoget = await mainwebview1.InvokeScriptAsync("eval", new string[] { "document.getSelection().toString();" });
  if (texttoget != null)
  {
      Debug.WriteLine("Text To get is:    " + texttoget.ToString().Trim());   
  }
  else
  {
        MessageDialog msgbox3 = new MessageDialog("Please select or mark the text you would like to get.");
        await msgbox3.ShowAsync();
  }
DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
SunnySonic
  • 1,318
  • 11
  • 37

1 Answers1

0

I would use

string text = webview.InvokeScriptAsync("eval", "document.getSelection()");

Remember you're working in a browser at the point of getting the selection so await package.GetView().GetTextAsync(); is going to be wonky on different devices. The above method leverages javascript which should be equal on each device.

Reference Documentation

DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
  • Actually i did get it working with string string text = await mainwebview1.InvokeScriptAsync("eval", new string[] { "document.getSelection().toString();" }); But this again only works on the pc but not on the phone. Any comments? – SunnySonic Jul 26 '16 at 17:51
  • @SunnySonic honestly I was guessing. Without actually seeing your code, errors etc, I don't know if anyone will be able to answer the question. I have an application that uses a webview extensively both on phone and desktop just fine. – DotNetRussell Jul 26 '16 at 17:55
  • Thanks. Updated my answer with the current code. The behaviour works fine on a computer, but on the phone it is always null. – SunnySonic Jul 26 '16 at 18:25
  • @SunnySonic do you know for sure that document.getSelection() is supported on the phone's browser? Maybe there is a different javascript call you need to make on the phone? – DotNetRussell Jul 26 '16 at 18:31
  • Could it be related to this? http://stackoverflow.com/questions/3538798/javascript-document-getselection – DotNetRussell Jul 26 '16 at 18:31
  • Did you try this instead? https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection – DotNetRussell Jul 26 '16 at 18:33
  • Maybe when you're on the phone it's a system level task and not a browser task. – DotNetRussell Jul 26 '16 at 18:35
  • same behaviour with window.getSelection().toString() Working on pc but not on phone... – SunnySonic Jul 26 '16 at 18:35
  • with contentDocument and also with .createRange() the code breaks – SunnySonic Jul 26 '16 at 18:40
  • Like it physically falls into pieces on your screen or there is an exception? – DotNetRussell Jul 26 '16 at 18:41
  • hehe. it just shows Exception thrown: 'System.Exception' in mscorlib.ni.dll like every time when there is a coding problem in the java code you are trying to inject. – SunnySonic Jul 26 '16 at 18:42
  • Have you attempted to capture system level selected text on the phone? – DotNetRussell Jul 26 '16 at 18:43
  • No. How do it do that? – SunnySonic Jul 26 '16 at 18:43
  • have you seen this post? http://stackoverflow.com/questions/3545018/selected-text-event-trigger-in-javascript – DotNetRussell Jul 26 '16 at 18:44
  • This MSDN article https://social.msdn.microsoft.com/Forums/en-US/f09ea401-15a0-4403-9543-d3d40473ca77/uwpdetect-text-selection-change-in-a-uwp-webview-control?forum=wpdevelop – DotNetRussell Jul 26 '16 at 18:44
  • Not sure i understand. Isn't that all the same? – SunnySonic Jul 26 '16 at 18:55
  • My point being that someone is making it work with that code. I would look at WHY yours might not be working with it instead of a different solution – DotNetRussell Jul 26 '16 at 18:57
  • The guy only checks for selection changed but not actually copies the text. Tried all these solutions give unfortunately already. There is still the difference of things working on the pc and not on the phone. – SunnySonic Jul 26 '16 at 19:04