How to revoke an object URL

If you use URL.createObjectURL() to download data created in JavaScript and you want to revoke the object URL (with revokeObjectURL) later (as it is strongly recommended), you need to do that after the download has been completed. To do so, listen to the downloads.onChanged event.

The attached test extension

  • Creates a blob with some text and downloads it into the standard download directory
  • In the downloads.onChanged handler:
    If a download has been completed, and it was started by the extension, then erase the download from the history, and free the blob memory

Is there a better way to do this?
If I chain an exception handler to browser.downloads.search(), will it catch all exceptions?

revokeobjecturl.zip (4.3 KB)

Better version:
revokeobjecturl.zip (4.3 KB)

If browser.downloads.erase() causes an exception, it will now be caught.