How to use the download API in chrome extension

4,813 views
Skip to first unread message

jaaf

unread,
Jun 26, 2013, 8:59:26 AM6/26/13
to chromium-...@chromium.org
Hi
I am writting an extension that uses a data file (dictionary like file).
I would like to allow the user to update this data file using a button whether in the popup or the options page to trigger the download from a given server.
I mentionned this in the manifest the following way :

"permissions": [
    
"downloads",
    
"http://sousdomain.domain.com/HorsDrupal/data/"],

I placed a button in the popup to call an update function in the javascript of the popup.


function
 updateDict(){


chrome
.downloads.download( {url: "http://sousdomain.domain.com/HorsDrupal/data/BasqueFr.txt", filename: "chrome.extension://@@extension_id/"}) 
}
  I got the following answer in the console of the popup 


Error
 during downloads.download: I'm afraid I can'do that.                       sendRequest:22
chromeHidden
.handleResponse                                                        sendRequest:22


The problem is the same if I place the button and the javascript in the options page.

I also tried an alternative method indicated in the samples


function
 updateDict(){

chrome
.downloads.onDeterminingFilename.addListener(function(item, suggest) {
  suggest
({filename: item.filename,
           conflict_action
: 'overwrite'});
 
});
}

This time the answer is

Uncaught TypeError: Cannot call method 'addListener' of undefined                       popup.js:41
updateDict                                                                              
popup.js:41


I guess this example is not complete. 
I must confess I have trouble understanding this API as the examples are very laconic. 
I need some help. Thanks in advance.

benjh...@chromium.org

unread,
Jun 27, 2013, 12:42:14 PM6/27/13
to chromium-...@chromium.org
On Wednesday, June 26, 2013 5:59:26 AM UTC-7, jaaf wrote:
> Hi
> I am writting an extension that uses a data file (dictionary like file).
> I would like to allow the user to update this data file using a button whether in the popup or the options page to trigger the download from a given server.
> I mentionned this in the manifest the following way :
>
>
>
> "permissions": [
>     "downloads",
>     "http://sousdomain.domain.com/HorsDrupal/data/"],
>
>
> I placed a button in the popup to call an update function in the javascript of the popup.
>
>
>
>
> function updateDict(){
>
>
> chrome.downloads.download( {url: "http://sousdomain.domain.com/HorsDrupal/data/BasqueFr.txt", filename: "chrome.extension://@@extension_id/"}) 

The characters ':' and '/' are invalid. Try this instead:
chrome.downloads.download({url:"http://sousdomain.domain.com/HorsDrupal/data/BasqueFr.txt", filename: chrome.i18n.getMessage("@@extension_id")});

I'll make the error messages more helpful.

> }
>   I got the following answer in the console of the popup 
>
>
>
>
> Error during downloads.download: I'm afraid I can't do that.                       sendRequest:22
> chromeHidden.handleResponse                                                        sendRequest:22
>
>
>
>
> The problem is the same if I place the button and the javascript in the options page.
>
>
> I also tried an alternative method indicated in the samples
>
>
>
>
> function updateDict(){
>
> chrome.downloads.onDeterminingFilename.addListener(function(item, suggest) {
>   suggest({filename: item.filename,
>            conflict_action: 'overwrite'});
>  });
> }
>
>
> This time the answer is
>
>
>
> Uncaught TypeError: Cannot call method 'addListener' of undefined                       popup.js:41
> updateDict                                                                              popup.js:41

That looks like a bug. What version of chrome are you using?
Reply all
Reply to author
Forward
0 new messages