Handle keystrokes when the popup is shown in a different window
As raised here: https://github.com/birchill/10ten-ja-reader/issues/46#issuecomment-1147114706, this probably affects Thunderbird users the most but can affect browser users too in some cases.
I'm not sure if this is worth doing but it would roughly entail something like:
-
Broadcast the state of the popup showing or not to all content scripts, including those in other windows (or at least the active tab in other windows) -- this is so we know if we can ignore keystrokes or not when we get them
-
If we get a keystroke and we are not the top-most frame or the popup is showing in a different window send the message to the background page like we currently do
-
Have the background page forward the message to not only the top-most frame of the current window, but all windows (e.g. use
browser.tabs.query({ active: true })and then get their top-most frames). This might suggest using a message prefix other thantop:, e.g.active-top:.Alternatively, we could have the background page track the tab in which the popup is showing and only send to that tab. That might be simpler since I believe the messages about popup state already pass through the background page.
Whatever approach we take, we'll need to take care the content script doesn't get stuck in a loop when receiving these messages.
The set of actions we'd likely want this to cover would include:
- Showing the next dictionary (the main use case)
- Hold-to-show keys
- Keys related to copying
- Popup movement keys
- The key to toggle definitions on and off
Then again, if we make it possible to interact with the popup window with the mouse, perhaps this would become less of an issue.
Here's a WIP of the sort of setup: https://gist.github.com/birtles/9ac8673ad55c0f33eeaff25c09336aee