Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download url chrome extension

I am trying to download a url by writing code for chrome extension. Here is the myscript.js file:

chrome.downloads.download(
    {url: 'http://www.iana.org/_img/iana-logo-pageheader.png',
     saveAs: true
    },
    function(res){alert(res);});

and here is my manifest.json

{
  "name": "My extension",
  "version": "1.0",
  "manifest_version":2,
  "background_page": "background.html",
  "browser_action": {
    "name": "Manipulate DOM",
    "icons": ["icon.png"],
    "default_icon": "icon.png"
  },
  "permissions": ["downloads",
    "tabs", "http://*/*","https://*/*"
  ],
  "content_scripts": [
    {
      "matches": [ "http://*/*", "https://*/*"],
      "js": ["jquery.js","d3.v2.js","myscript.js"],
      "run_at": "document_end"
    }
  ]
}

but the console is showing the error "Cannot call method 'download' of undefined". Please help me.

like image 559
skjindal93 Avatar asked Dec 10 '12 18:12

skjindal93


People also ask

How do I find the URL for a download link?

To do this you need to right-click on the file and select “Copy Public Link“. This will generate a public link for this specific file. This link usually leads to a page where you can preview a file before downloading it.

How do I download multiple urls at once?

As indicated, you can draw a rectangle around the links you want to select. This will highlight the links in yellow. From there you can either hit Enter to open the selected links in the same window, “Shift + Enter” to open in a new window, or “Alt + Enter” to download them.


1 Answers

The documentation for chrome.downloads clearly states that the "API is still under development. It is only available for Chrome users on the dev early release channel." (emphasis mine, currently at Chrome 23).

To use the API, you need to get a dev or canary build of Chrome (see this page for download links).

Another way to solve the problem is by not using the chrome.downloads API. I've been using the following method to create downloads, and it works like a charm (it works anywhere: Content script / background page / popup / whatever):

var a = document.createElement('a');
a.href = 'http://www.iana.org/_img/iana-logo-pageheader.png';
a.download = 'iana-logo-pageheader.png'; // Filename
a.click();                               // Trigger download

a.click() causes Chrome to follow the link.
The download attribute causes Chrome to download the target, and suggest the attribute's value as a file name in the Save As dialog.

This feature is not limited to Chrome extensions, you can also use it in an ordinary web page. Have a look at this demo: http://jsfiddle.net/dEeHF/.

like image 191
Rob W Avatar answered Sep 20 '22 03:09

Rob W