I have some code that updates a context menu when a selection is made. My content code is:
document.addEventListener('selectionchange', function() {
var selection = window.getSelection().toString().trim();
chrome.runtime.sendMessage({
request: 'selectionChanged',
selection: selection
});
});
and the background code catches the message and updates the context menu.
This works fine if I select text by using the left-mouse button (drag to highlight, double click, etc) and then bringing up the context menu, but does not work if I right-click on a word that is not already highlighted. From what I can tell the problem is that the context menu is brought up before the selection change message is handled and the menu updated.
How can I update the context menu with the right-click selection before the menu is displayed?