Wednesday, 5 January 2022

Where can I properly use the API "chrome.downloads" when I want to download?

Ⅰ. Introduction

I have read quite a lot and also tried various ways of using the chrome.downloads API. But until now, I am still helpless in finding a way to use the chrome.downloads API.

Instructions from the docs didn't seem to help me much. So I decided to come here to ask as well as present to everyone about the problem that I am facing. I hope it helps many people.

My project uses manifest version 3.

Here is the GitHub link of the project: https://github.com/ATU8020/Use-API-chrome.downloads.git

Ⅱ. Presenting the Problem

Some related concepts are used:

  1. Service worker
  2. Content script
  3. Asynchronous function
  4. Synchronous function

Problems

I know that service workers require a synchronous function.

Listeners must be registered synchronously from the start of the page. LINK

Do not register listeners asynchronously, as they will not be properly triggered. LINK

But the chrome.downloads.download API returns a Promise:

API Chrome.downloads

See chrome.downloads docs.

That confused me. I have also tried some other ways, such as with the a content script. But after reading the docs, I know that the API chrome.downloads cannot be used directly. enter image description here

See content script docs.

Comment

That is the crux of my question. Any constructive comments and useful information are always appreciated.

Ⅲ. More Information

If using the API chrome.downloads in file popup.js it works, so what does that mean?

I know that could be an alternative solution.

But what if you want to do something like download when you click on a contextMenu? Is it possible?

enter image description here



from Where can I properly use the API "chrome.downloads" when I want to download?

No comments:

Post a Comment