How to constantly highlight text on an Android website?

I am reading an Epub and showing the book in an Android web browser.

I can currently select text using below javascript

public static String Highlightscript = " <script language=\"javascript\">" +

        "function highlightSelection(){" +
        "var userSelection = window.getSelection();" + 
        "for(var i = 0; i < userSelection.rangeCount; i++)"
        + "  highlightRange(userSelection.getRangeAt(i));" +
         "}" +
        "function highlightRange(range){"+
        "span = document.createElement(\"span\");"+
        "span.appendChild(range.extractContents());"+
        "span.setAttribute(\"style\",\"display:block;background:#ffc570;\");"+
        "range.insertNode(span);}"+
        "</script> ";

and when the user clicks on the selection, I do

webView.loadUrl("javascript:highlightSelection()");

This will highlight the text, and I will also save the selected text in a local database

but when the user opens the book again, I want to show the previous highlighted text. How can I achieve this?

I try to find text when the user enters a page (which highlights the default text)

webView.findAllAsync(highlightedText);

but if the selected text is too small, like the word "The", many words will be highlighted.

I already searched on SO, but all questions remained unanswered Question 1 and Question 2

So is there any other way to achieve this?

+3
2

, - . , ( ). - .

, .

highlightRange(userSelection.getRangeAt(i));

Range. . Highlightscript, .

(https://developer.mozilla.org/en-US/docs/Web/API/Range).

, node node . . ( , , .)

. Range document.createRange() Range(). , .

highlightRange() Range.

0

, 2019 , . -, ? , , , . loadURL() evaluateJavascript(). .

0

Source: https://habr.com/ru/post/1666871/


All Articles