Access the selected text within macro once user clicks on the macro icon

Hi Team ,

I want develop a plugin which works as mentioned below .

  1. User will select the text on confluence page under edit page mode .
  2. User will click on the plugin icon from the header…
  3. I want to fetch the selected text by the user and use in macro context to process it . Here I want to auto fetch the selected text .

Could you please help to suggest do we have any confluence methods on how to fetch it ?

Thanks
Nishant