getSelection does not work in iOS - but in Desktop, AnkiDroid, Web versions

I created a card template with some JavaScript years ago. It checks for selected text and passes it on to a website. It has worked and is still working in a) the desktop version, b) AnkiDroid and c) the Web version. However, I recently got an iPhone (with iOS 18.5) - and the selection cannot be retrieved in the AnkiMobile app.
The relevant code snippet is this:

wort = "";
if (window.getSelection()) {
	if (window.getSelection().toString().length > 0){
		wort = window.getSelection().toString().trim();
	}
}

window.getSelection() is true, but window.getSelection().toString() returns nothing, so wort remains an empty string.
As written above - coding unchanged for years, but not working in the iOS app.

If anyone has an idea (or even a solution) it would be great!
Thanks!

Does anything here help you?

Or in this section of the AnkiMobile manual: URL Schemes - AnkiMobile Manual ?

[And I’m just taking a flyer that they might – I actually have no idea. But it might give you some things to try until someone more knowledgeable is available.]

Thanks for your hint - I’m afraid I have not made myself clear enough.
Calling a URL and passing on values is not a problem.

My problem is retrieving selected text in cards in the iOS app. JavaScript window.getSelection()appears not work in Anki Mobile on iPhones.

It seems to be a known issue with iOS. Maybe try the solutions posted by other people, e.g. see:

  1. IOS 15, javascript functions windo… | Apple Developer Forums
  2. window.getSelection() doesn't work in iOS 15 · Issue #14270 · mdn/browser-compat-data · GitHub
3 Likes

Thank you so much!
The problem appears to be the popup menu (“Copy … Translate … Share …”) that automatically comes up as soon as you select text on the iPhone.
I found a nice solution in the second link (https://github.com/mdn/browser-compat-data/issues/14270) suggested by Anon_0000 that constantly checks if the selection is being changed and saves the currently selected text in a global variable - before the popup comes in.
This has worked for me on all versions (desktop, Web, Android and now also iOS).
:grinning_face:

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.