Hi.
Yes, this is possible with the Montoya API - you can find an example below. In the example, it simply prints the offsets of the selected text. However, you could manipulate this further to retrieve the contents of the text.
import burp.api.montoya.BurpExtension;
import burp.api.montoya.MontoyaApi;
import burp.api.montoya.core.Range;
import burp.api.montoya.ui.contextmenu.ContextMenuEvent;
import burp.api.montoya.ui.contextmenu.ContextMenuItemsProvider;
import javax.swing.*;
import java.awt.*;
import java.util.List;
import static burp.api.montoya.ui.contextmenu.InvocationType.*;
import static java.util.Collections.emptyList;
public class Extension implements BurpExtension
{
@Override
public void initialize(MontoyaApi api)
{
api.extension().setName("Get selection offsets");
api.userInterface().registerContextMenuItemsProvider(new ContextMenuItemsProvider()
{
@Override
public List<Component> provideMenuItems(ContextMenuEvent event)
{
if (event.isFrom(MESSAGE_EDITOR_REQUEST, MESSAGE_EDITOR_RESPONSE, MESSAGE_VIEWER_REQUEST, MESSAGE_VIEWER_RESPONSE))
{
if (event.messageEditorRequestResponse().isPresent() && event.messageEditorRequestResponse().get().selectionOffsets().isPresent())
{
JMenuItem menuItem = new JMenuItem("Get selected text");
menuItem.addActionListener(l -> {
Range selectionOffsets = event.messageEditorRequestResponse().get().selectionOffsets().get();
api.logging().logToOutput(String.format("Start index: %s\r\nEnd index: %s", selectionOffsets.startIndexInclusive(), selectionOffsets.endIndexExclusive()));
});
return List.of(menuItem);
}
}
return emptyList();
}
});
}
}