No, in a Chrome extension/Firefox WebExtension, there is no way to have your JavaScript make dynamic changes to the context menu at the time the context menu opens. The only thing that dynamically changes at the time the context menu opens is: if you have added a context menu item that has the selection
context for its ContextType, then you can specify %s
in the title
, which will be replaced with the current selection.
Change the context menu before it begins to open
Mouse events
To do what you desire, you need to make the changes to the context menu prior to the beginning of the context menu being opened. This can be accomplished by having a content script that listens to the mouseup
event. You may have to end up using the mousedown
event, or one or more of the mouseenter
/mouseleave
/mouseover
/mouseout
events. If you do need to use mousedown
, once that event fires you should then start listening to some of the mouseenter
/mouseleave
/mouseover
/mouseout
events to tell when the element the mouse is over changes, or just assume the user releases the button on the same element on which they pressed it down.
If the mouseup
/mousedown
event is for button=2
, then you will need to message your background script to change the context menu with contextMenus.update()
. There are multiple asynchronous portions of this process. This may make for various race conditions, and may necessitate using events which give you earlier notification than mouseup
(e.g. mousedown
, mouseenter
/mouseleave
/mouseover
/mouseout
).
What events you need to watch for may be operating system/platform/windowing system dependent. You will need to test to determine what is needed in each environment you plan to support.
Keyboard events
The context menu can also be opened using a few keyboard sequences. You will need to listen for and perform the same messaging as is needed for mouse events. You will need to determine what these events are in all operating systems/platforms/windowing systems which you are supporting.
Linux
On Linux it appears to vary at least based on platform.
OSX
On OSX, it appears that a keyboard shortcut is only available if enabled.
Windows
On Windows, the keyboard shortcuts to open the context menu are:
Context Menu key
You will need to detect the following event sequence:
keydown { target: <body>, key: "ContextMenu", charCode: 0, keyCode: 93 }
keypress { target: <body>, key: "ContextMenu", charCode: 0, keyCode: 93 }
keyup { target: <body>, key: "ContextMenu", charCode: 0, keyCode: 93 }
The context menu does not open until around the same time the keyup
event fires. I did not test to see if the keyup
happens early enough for you to make changes to the context menu prior to it opening.
Keyboard combination: Shift-F10
You will need to detect the following event sequence:
keydown Shift { target: <body>, key: "Shift", charCode: 0, keyCode: 16, shiftKey: true }
keydown Shift { target: <body>, key: "F10", charCode: 0, keyCode: 121, shiftKey: true }
keypress Shift { target: <body>, key: "F10", charCode: 0, keyCode: 121, shiftKey: true }
In this case, the context menu opens at around the same time as the shifted F10 keydown
/keypress
events fire. No keyup
event fires for this keyboard sequence.