2

I have a link to the file. How do I know that the user clicked on the link and download the file? I need to get downloadId? Google Chrome API has a method chrome.downloads.open (integer downloadId). But how do I know downloadId, to open this file? Help me please.

Xan
  • 74,770
  • 16
  • 179
  • 206

1 Answers1

3

You can try this example code below tested in Google Chrome Version 38.0.2125.111 m (64-bit)

chrome.downloads.onChanged.addListener(function (detail){
console.log("Detail",detail); //Save log for debugging

    //if file download finished
   if(detail.state.current == "complete"){

       var downloadId = detail.id; //Download ID

       console.log("Download ID",downloadId); //Save log for debugging
       /*
       Do Something
        */

   }
});
Touhid
  • 659
  • 8
  • 27
  • 1
    var downloadId = detail.id; chrome.downloads.open(downloadId); Error in event handler for downloads.onChanged: Cannot read property 'current' of undefined Stack trace: TypeError: Cannot read property 'current' of undefined...Unchecked runtime.lastError while running downloads.open: User gesture required at chrome-extension://gpilmplhmghnbjlbcahpncjihglmmjjb/background.js:6:20 – Дмитрий Захаров Nov 06 '14 at 12:20
  • 1
    These errors are occuring when state.current is not present it will not make problem. and Find user gesture permission here -> [chrome.permissions](https://developer.chrome.com/extensions/permissions) – Touhid Nov 06 '14 at 12:44
  • I do not understand what you mean. "permissions": ["downloads","downloads.open","tabs"]? So, too, does not work. I have problems with English. Translator translates not clear) – Дмитрий Захаров Nov 06 '14 at 13:02
  • You need user permission by User gesture. You can find about user gesture here -> https://developer.chrome.com/extensions/permissions – Touhid Nov 06 '14 at 13:04
  • I am also not familiar with user gesture. You can find the solution of that problem anywhere or ask that problem again. – Touhid Nov 06 '14 at 13:06
  • 3
    "User gesture is required" means you can't do what you want to do. **This can only run in a code initiated by a user action, like a click on a button. It cannot be executed from non-user events.** Sad. I'm removing my answer then, it does not apply. – Xan Nov 06 '14 at 13:26
  • 3
    Basically, you can't automatically open downloads. You can only present a custom button to open a download. – Xan Nov 06 '14 at 13:27