Remember the highlighted text on the html page (add annotation to the html page)

I have an HTML file, I open it with webkit, I want to develop an application, so that after I open it, I have to select some text and make it highlighted (for example, by clicking on some “select text” button). And he should remember the selected text, so when I open it next time, he should automatically select the same text ... what information should I store so that I can select the same next time? is any library available that makes my work simple?

+3
source share
3 answers

If you want something to be constant in the browser navigation, you can save the information in a hidden INPUT field:

<input type="hidden" id="persistentValue" />

Then use some jQuery code to test it and take action:

$(document).ready(function() { 
    var persistentValue = $("#persistentValue").val();
    if (persistentValue && persistentValue != "") {
         //Highlight the text again
    }
}); 

And to keep the text selected (once you have defined it), use:

$("#persistentValue").val("your value here");

Thus, if you go from your page and go back, the INPUT values ​​will be restored and your text will be highlighted again.

+1
source

If you want to “remember” some text / highlight even after the user has closed his browser and then returned later, you should use cookies (save all the necessary values ​​there). Some examples: http://www.quirksmode.org/js/cookies.html

+1
source

Regarding text selection, you can use the answer to this question: Use javascript to extend the DOM range to cover partially selected nodes . You need to figure out a way to serialize / deserialize the information Range, perhaps by storing the selected text in a cookie, and then, when you reload the page, by searching for it in the document and re-creating it Rangemanually.

+1
source

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


All Articles