Options.html (as popup) & file picker

I’m working on an addon that I would like to support export and import of some rules that would be stored. Currently I have an import button on options.html which opens a file picker to choose the input JSON file and will parse and store the information into the addon.

This solution works fine if options.html is in a separate tab, but does not work when it is just being used as a popup from clicking on the addon icon. It looks to me that the popup closes once the file picker comes up. This makes sense to me from a popup behavior perspective.

Is there a way to keep the popup open even during the file picker process? Or do I need to push the import/export process only when options.html is in a separate tab?

1 Like

Sadly, it’s a very old Firefox bug:

The best you can do is:

  1. vote for that bug in bugzilla :slight_smile:

  2. detect that your popup.html is opened in the popup, and in that case, instead of “input” show something like this:
    image

NOTE:
I’m pretty sure this can happen also with exports!
Some users have “Always ask where to save file” option enabled in Firefox, so for them Firefox will again show a target download folder picker and the export will then fail.

1 Like

I haven’t tested this, but I think it would work. Your background script runs in the background page, so in the background you could

  1. use document.createElement() to create an anchor tag
  2. give it the download attribute
  3. set the URL to the file that you want the user to download
  4. the append it to the background document
  5. use JavaScript to call .click() on the element

I think that would allow you to download the file unless the user has the “Always ask where to save file” setting enabled as @juraj.masiar described.

1 Like

Yeah, this is what I will end up doing. I’ll probably just create a link/button on the options page to open a new tab with the import/export options. (I did vote for the bug and I will watch it)

Thank you.

1 Like