browser.tabs.captureVisibleTab not working in firefox for mv3

browser.tabs.captureVisibleTab is undefined in firefox manifest v3, even after adding “<all_urls>” under host_permissions and “activeTab” under permissions.
Any help would be appreciated if I am missing something.

It’s a permission problem:
Firefox MV3 treats all host permissions as optional.

You need to jump through a few hoops to use tabs.captureVisibleTab()

  1. Install the test extension:
    captureVisibleTab.zip (1.5 KB)

  2. Click the action icon to download a screenshot of the active tab.
    Because the extension doesn’t have the “<all_urls>” permissions yet, the extension console displays:
    TypeError: browser.tabs.captureVisibleTab is not a function
    Enable the “<all_urls>” permission in the Add-ons Manager:
    image

  3. Click the action icon again.
    Again, the extension console displays:
    TypeError: browser.tabs.captureVisibleTab is not a function
    Reload the extension:
    captureVisibleTab_Firefox_MV3_b

  4. Click the action icon again.
    The extension now creates the file “captureVisibleTab.png” in the Downloads directory.

Notes:

  • For the sake of simplicity, the test extension forces the user to enable the “<all_urls>” permissions manually, and reload the extension manually. A finished extension could do this with browser.permissions.request() and browser.runtime.reload()
  • I don’t know why you have to reload the extension after enabling the “<all_urls>” permissions.
    If you think this is a bug, please consider reporting it on https://bugzilla.mozilla.org/
  • I don’t know if Firefox will distinguish between required and optional host permissions once the “optional_host_permissions” manifest key has been implemented.
    See manifest.json > optional_permissions > Host permissions
  • browser.tabs.captureVisibleTab() returns a data URL.
    Firefox 118.0 can’t download data URLs, so the test extension converts the data URL to a blob, then converts the blob to an object URL, and downloads the object URL.
2 Likes

This may be a good time to mention that there is also an issue to allow screenshots with “activeTab” only permission (same as in Chrome and Safari).

1 Like

thanks Hans, it worked, and I was able to automate these permissions using browser.permissions.request({origins: [’<all_urls>’]})