Using Chrome.downloads.download within a Chrome application

My app is running into an issue where Chrome is giving me the error message that chrome.downloads is undefined.

In my attempt to download an image, here is a simple example provided...

Here is the manifest:

{
  "manifest_version": 2,
  "name": "Downloader",
  "minimum_chrome_version": "38",
  "permissions": ["downloads", "<all_urls>"],
  "app": {
    "background": {
      "scripts": ["background.js"]
    }
  }
}

The HTML code snippet looks like this:

<body>
  <button id="download">Download</button>
  <script src="main.js"></script>
</body>

For background.js:

chrome.app.runtime.onLaunched.addListener(function(launchData) {
  chrome.app.window.create(
    'index.html',
    {
      id: 'mainWindow',
      bounds: {width: 800, height: 600}
    }
  );
});

And for main.js:

window.onload = function() {
  document.querySelector("#download").addEventListener("click",
    function () {
      chrome.downloads.download({
        url: "http://upload.wikimedia.org/wikipedia/commons/6/6e/Moonbeam_UFO.JPG",
        filename: "ufo.jpg"
      });
    }
  );
};

I have searched for similar issues when developing extensions, but nothing specific to using it in an app. Can anyone provide guidance on how to resolve this?

Thank you

Answer №1

The API for downloading files using chrome.downloads is not currently supported in Chrome apps (crbug.com/274673).

However, you can still initiate a file download using the standard web platform APIs. In fact, the functionality described in your question can be achieved with basic HTML:

<a download="ufo.jpg" href="http://upload.wikimedia.org/wikipedia/commons/6/6e/Moonbeam_UFO.JPG">Download</a>

(You can style the anchor as a button on your own)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Need to know how to show a DIV for a specific time frame using Javascript?

One of the features I am looking to implement in my Django application is a display that notifies the user when one of their posts is about to start. The idea is to show a DIV element with the message: <div>“Will start soon”</div>, 15 minut ...

Changing the state variable upon clicking to display varying components

Being new to React, I am exploring how to load different components based on state variables. I am looking for a way for my handler to dynamically determine which state variable I am updating without explicitly passing the state name as a string. List of ...

Keeping JSP current with changes made to files on the client side

Currently, I am tackling a project that consists of two main components: a) A Java Application that runs on a client machine. b) A Web Application that is hosted on a web server. The Java Application generates results at random intervals. These results n ...

Key to Perform Right Click

Hey, I could use a little advice window.addEventListener('keyup', function (event) { if (document.activeElement && document.activeElement.tagName === 'INPUT') { return; } switch (String.fromCharCode(event.keyCode ...

Loop through an object with only one array using JavaScript

I have a specific object structure that I am trying to iterate through in order to find a particular value within the array. For example, I want to check if the user name is equal to user2. While I was able to accomplish this with two separate objects (cre ...

Is it necessary to mark all checkboxes in the initial column of the gridview?

I have encountered an issue with my code that checks all checkboxes in the first column of a gridview. It seems to work only for Internet Explorer versions 5.0 to 8.0, but gives a Javascript error when running on IE 9 and above stating "Function Expected ...

I'm having trouble getting my window resize event listener to function properly. I'm using a combination of Three.js and Vuetify.js

My goal is to incorporate a three.js renderer into a div element using the vuetify.js framework. I want my code to dynamically adjust the dimensions of the div element whenever the window is resized. I have excluded unnecessary code blocks from this excer ...

Quirks and nuances when using p5's pixel fill function

I am currently utilizing the p5.js library to work on a program that is designed to automatically fill closed spaces. However, I am encountering an issue where the program sometimes fills half-closed loops for reasons unknown to me. If anyone could provide ...

What is the best way to populate empty dates within an array of objects using TypeScript or JavaScript?

I am trying to populate this object with dates from today until the next 7 days. Below is my initial object: let obj = { "sessions": [{ "date": "15-05-2021" }, { "date": "16-05-2021" }, { "date": "18-05-2021" }] } The desired ...

"Transforming a simple object into an instance of a different type in JavaScript: A step-by-step guide

Having an issue storing a session on disk for local development. The application is asking for an instance of Session to be returned, not an Object function storeCallback(session) { console.log("storeCallback ", session); fs.writeFileSync(&qu ...

Handling 401 Status Codes with Access and Refresh Tokens in a React and Node Application

Dealing with a 401 status request from my server when the access token is expired is proving to be a challenge. I have implemented accessTokenVerify on the server side: require('dotenv').config(); const jwt = require("jsonwebtoken") ...

Updating certain fields in AngularJS with fresh data

I am facing a challenge with the following code snippet: <div ng-controller="postsController"> <div id = "post_id_{{post.postid}}" class = "post" ng-repeat="post in posts"> ...... <div class="comments"> < ...

A div containing a form, with the form being visually integrated within the div

Hi everyone, this is my first post here and I've tried to search extensively before asking this question. I've attempted to use the following examples without success: jquery submit form and then show results in an existing div Form submit ...

Why is it necessary in JavaScript to reset the function's prototype after resetting the function prototype constructor as well?

Code is often written in the following manner: function G() {}; var item = {...} G.prototype = item; G.prototype.constructor = G // What is the purpose of this line? Why do we need to include G.prototype = item before resetting the prototype? What exact ...

The ExpressJS app generator seems to be struggling to identify and interpret the flags

I seem to be having trouble running the express app generator with flags. For example, when I run express --version, it interprets the --version part as a target directory and creates the app there. This is happening on Windows XP SP3. Could I be doi ...

What is the best way to incorporate options using jQuery?

Currently, I am incorporating a jQuery plugin for modals into my website. The plugin/code offers the functionality to customize the background color of the modal. For more details on how to do this, you can visit the plugin's official website. Althou ...

What is the best way to utilize AJAX for uploading .mp4, .mp3, and .doc files together with additional FormData?

Hello everyone, I have been successfully uploading files (documents and images) using AJAX. However, I am facing some challenges when it comes to uploading videos through AJAX. I have searched extensively on this site for solutions but have not found exact ...

Can you display names in capital letters from the randomUser.me API?

Apologies if this is not the appropriate platform for my query. Just to provide context, I am a designer with minimal experience in APIs and Javascript. I am currently utilizing the randomUser API to create a JSON file or URL that can be integrated into I ...

When a panorama is entered in ThreeJS/Panolens, what is the orientation of the camera?

I want to use an equirectangular image with Panolens and achieve the same panorama viewer effect as seen in the links below: Even though Panolens.js is based on Three.js, I encounter a different result when I input my image compared to the first link abov ...

When reopening the modal in Bootstrap V5 with jQuery, the close event may not trigger as expected

When trying to implement special functionality in my modal close event using sweetalert2, I encountered an issue where the close event does not trigger again after closing the modal for the first time. Check out a live example here: https://codepen.io/th ...