Can a click event caught in a content script be used to trigger a sidebar action

(Olivier De Broqueville) #1

I’ve read the following:

To follow the principle of “no surprises”, APIs like this can only be called from inside the handler for a user action. User actions include the following:

  • Clicking the extension's browser action or page action.
    
  • Selecting a context menu item defined by the extension.
    
  • Activating a keyboard shortcut defined by the extension (this only treated as a user action from Firefox 63 onwards).
    
  • Clicking a button in a page bundled with the extension.
    

from MDN and I understand it means that I can’t use

browser.sidebarAction.open()

from a content script, so how can I trigger the sidebar to launch if something gets clicked that is handled in a content script?

Any ideas how this can be solved?

(Olivier De Broqueville) #2

Could you please help, Martin?

My extension is currently in ‘stuck’ mode, because I use a grid of favicons generated by a content script. When one of the favicons, representing a search engine, gets clicked, then I should display the search results in the sidebar.

The only way I could think of to handle this was to send a message from the content script to the background script, but I can’t use browser.sidebarAction.open() in the bg script because the click (user action) event handler is in the content script!

(Martin Giger) #3

Pretty sure there’s nothing you can do. I think in theory sendMessage carries over the “user generated” flag, but I don’t think that flag applies for content script generated actions, but you could still try…

Generally you’d trigger it with a context menu item or a browser action or a page action etc. - in short, native UI that can’t leak to web content.

(Olivier De Broqueville) #4

That’s a real pity because it means that I can’t use the grid of favicons shown below to make an XHR request for a mobile friendly page of search results that will look good in the sidebar. I don’t think it’s a good decision by the Firefox developers because the sidebar has a very limited width, so it’s preferable to set the header request for a mobile device. You can’t do that with browser.sidebarAction.setPanel by setting a targetUrl for the panel. The only way around this that I can think of is using a remote API to make the XHR request, but that means increased cloud hosting fees and there’s a privacy issue as well because some users are not happy to have their searches transferred to a remote server.

190225

So the option I’m left with is to give users a somewhat ‘shitty’ experience with Firefox if you’ll excuse my saying!