browser.tabs.captureVisibleTab not working in firefox for mv3

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:

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

  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