how to detect if cursor text selection is visible on mobile browser?

I’m trying to add update the jQuery Terminal cursor position when text selection cursor is visible. This is my code:

function update_mobile_cursor() {
    if (is_mobile) {
        var selection = document.getSelection();
        console.log(selection);
        if (selection.anchorNode === selection.focusNode) {
            var range = selection.getRangeAt(0);
            console.log(range);
            self.position(range.endOffset);
        }
    }
}

But both selection and range exists even when the selection cursor is not visible. When I type something, the endOffset points into the last typed character.

Is there a way to detect the visibility of the selection cursor on a mobile Browser?

I have a testing webpage: https://terminal.jcubic.pl/android.php when You press backspace it jumps to the beginning.

NOTE: that the function don’t trigger when text selection is changed, I’ve asked about it here: Listen to cursor change on mobile browser

I test this with Chromium based Brave.