user3851593
user3851593

Reputation: 27

Chrome Extension: Download

My last and final question. I cannot seem to use the .downloads function in the extension. I get the error

Cannot read property 'download' of undefined

Beware I removed some of the code so it can fit here My code is

var contentInput = document.createElement("div")
contentInput.innerHTML = '<div style="position:relative; width: 145px;height: 30px;right: 0px;left: 14px;padding-top: 0px;top: 0px;"><div class="btn-primary btn-medium" style="position: absolute;left: 0px;" draggable="true">Download .OBJ</div><p style="position:relative; top: 33px; font-size:15px">Click to download the OBJ version of this asset.</p> </div>'
contentInput = contentInput.children[0].children[0]

localStorage.setItem("OBJURL", jsonObject.Url); //It's saved!
});

var objurl = localStorage.getItem("OBJURL");
SendRequest(objurl, function (objfinal) {

});
    chrome.downloads.download({url:objurl,filename:"wat23333.obj",conflictAction:"overwrite"})
    chrome.extension.sendRequest({
        action: "EditContent",
        type: assetType,
        name: assetName, 
        content: contentData
    })

}

Manifest file

Permissions

"permissions": [
    "http://*.roblox.com/*",
    "http://*.rbxcdn.com/*",
    "downloads",
    "downloads.open"
],

Upvotes: 1

Views: 2406

Answers (3)

Simon Baars
Simon Baars

Reputation: 2319

For me Xan's solution also didn't work. What worked for me was to go to chrome://extensions and remove the extension from chrome. Afterwards, I re-added the extension. Then it worked.

Upvotes: 0

Xan
Xan

Reputation: 77571

Devlin's guess was almost correct.

It happens when either of those conditions hold:

  1. You don't have permissions
  2. The API you're accessing is called from a content script when it's not available for content scripts.

You have the second case, apparently. You'll need to pass a message to the background page and handle the action from there.

Upvotes: 5

Devlin
Devlin

Reputation: 21

The error "Cannot read property 'download' of undefined" means that the chrome object doesn't have the downloads property on it. This is usually indicative of not having permission to access the API - do you request the "downloads" permission in your manifest?

Upvotes: 2

Related Questions