views:

85

answers:

0

I'm trying to dynamically create context menu items in a Chrome extension; this involves sending a request to the main extension process to modify the menu, and blocking the menu from being displayed until a response is sent. I've been experimenting with different solutions all day, and I have yet to come up with one that works. For example,

node.oncontextmenu = initContext({...});

initContext = function(menuParams) {
  return function(event) {
    if (event.showContext) // Menu has been created and is ready for display
      return true;

    event.preventDefault(); // Block original menu event
    chrome.extension.sendRequest(menuParams, function() { // Send request to modify menu,
      event.showContext = true; // generate new event when menu is ready
      event.target.dispatchEvent(event);
    });
  };
};

The menu is blocked with preventDefault(), and the callback function to chrome.extension.sendRequest() is executed when the menu is ready to be displayed. With this solution (and all the others I've tried), the menu is never displayed. I'd appreciate any help.