I am currently working on a project that involves allowing users to upload .docx files and read them directly within the app, alongside files uploaded by other users. To achieve this, I have extracted text from the docx files and displayed it in a separate view and HTML page within a div. My goal now is to provide users with the option to select specific text from this div and display a button that hovers over the selected text, enabling them to easily add it to their notes. This functionality is similar to what MS Edge does when text is selected, where it automatically adds three dots to open a menu for actions such as copying. Another example is the 'Save Note' button on Coursera's website, which allows users to save selected text to their notes with just one click.
Example of Coursera's 'Save Note' button
Although I have an idea of using window.getSelection to capture the selection and send it via fetch to my server (built with Django) to add it to the Notes Model, I am unsure of how to implement this feature effectively. Specifically, I am looking for guidance on how to make a button appear over the selected text only when a selection is made. Any assistance or suggestions on implementing this behavior are greatly appreciated! Please note that I am aiming to accomplish this using Vanilla JavaScript to avoid the need for additional libraries or frameworks like React.