Open In App

How to detect when browser receives download in web extension ?

In this article, we will see how to detect download events in your browser. The browser supports something called extension API which allows you to do more than manipulate a web page. There are many APIs that can be used to make browser extensions and one among them is the downloads API. It enables us to interact with the browser’s download manager. You can use this API module to download files, cancel, pause, resume downloads, and show downloaded files in the file manager.

To use this API you need to have the “downloads” API permission specified in your manifest.json file. This file is basically used to build progressive web apps that manifest your web app. downloads API is accessed through browser API.



Example: The downloads API can only be accessed in the background context and so you need to add a field background in the below-mentioned manifest file. A background field is an object with a script field where you run your background js file.

{
   "name":"name of the extension",
   "version":"version",
   "permissions":["downloads", "downloads.open"],
   "manifest_version": 2,
   "background":{
         "scripts":["index.js"]
     },
}

Download API consists of three different events, which are described below:



We can use any of the above events to detect the download event. Now, let us understand with a simple example of how to detect the download event

Here is the following code we are using to print the info about the downloaded item:

function handleCreated(downloadItem) {
    console.log(downloadItem);
}
browser.downloads.onCreated.addListener(handleCreated);

The onCreated event returns a downloadItem object that represents the downloaded file.

Here, the following code we are using to print the downloadedItem once it completes:

function handleChanged(DownloadDelta) {
    if (DownloadDelta.state && 
        DownloadDelta.state.current === "complete") {
           console.log(delta.id);
     }
}
browser.downloads.onChanged.addListener(handleChanged);

DownloadItem: This represents the downloaded file and is passed as an argument to the onCreated event.

There are various properties that are provided by the DownloadItem, which are described below:

Example 1: In this example, we will detect the download and print the downloadItem

Approach:

manifest.json:

{
   "name":"my-extension",
   "version":"1.0.4",
   "description":"This Extension helps you detect download",
   "manifest_version": 2,
   "permissions":["downloads"],
     "background": 
       {
         "scripts": ["app.js"]
       }     
}

base.html:




<!DOCTYPE html>
<html>
 
<head>
    <title>Download</title>
</head>
 
<body>
    <h1 style="color:green">GeekForGeeks</h1>
    <h3>
        How to detect when browser
        receives download in web extension ?
    </h3>
    <a href="http://127.0.0.1:5501/base.html" download>
      Download
      </a>
</body>
 
</html>

app.js:




// App.js
function handleCreated(downloadItem) {
   console.log(downloadItem);
}
browser.downloads.onCreated.addListener(handleCreated);

Output: Here, I am running the base.html using a live server at URL http://127.0.0.1/base.html and the download href that is downloaded is also at the same origin.

 

The downloadItem object consoled by the background script in the above output is:

Object { 
    id: 7,
    url: "http://127.0.0.1:5501/base.html", 
    referrer: "http://127.0.0.1:5501/base.html", 
    filename: "C:\\Users\\NIKHIL\\Downloads\\base.html", 
    incognito: false, cookieStoreId: "firefox-default", 
    danger: "safe", 
    mime: "text/html", 
    startTime: "2022-10-06T08:10:17.599Z", 
    endTime: null, ...
}

Example 2: In this example, we will print the download URL with startTime on the console.

Approach: 

manifest.json:

{
      "name":"my-extension",
      "version":"1.0.4",
      "description":"This Extension helps you detect download",
      "manifest_version": 2,
      "permissions":["downloads"],
    "background": {
        "scripts": ["app.js"]
    }     
}

base.html:




<!DOCTYPE html>
<html>
 
<head>
    <title>Download</title>
</head>
 
<body>
    <h1 style="color:green">GeekForGeeks</h1>
    <h3>
        How to detect when browser receives
        download in web extension ?
    </h3>
    <a href="http://127.0.0.1:5501/base.html" download>
        Download
    </a>
</body>
 
</html>

app.js:




function handleCreated(downloadItem) {
   console.log(
   `Download from ${downloadItem.url} +''
   +at starttime ${downloadItem.startTime}`);
}
browser.downloads.onCreated.addListener(handleCreated);

Output:

 


Article Tags :