Breakthrough! These use some experimental features and I have no idea if this will actually work. First, let's find out the path of the downloads directory by downloading a dummy file to it:
chrome.experimental.downloads.download({url:"data:text/plain,",filename:"~dummy.tmp"},
function (downloadId, error) {
if (error) { /* do stuff */ }
// get the DownloadItem object
chrome.experimental.downloads.search({downloadId: downloadId}, function(items) {
// this is the absolute filesystem location of where it was downloaded!
var filename = items[0].filename;
// if we remove dummy.tmp from the end we have the downloads directory!
// also in case it's windows replace backslashes with forward slashes.
var downloads_directory = filename.replace(/~dummy.tmp$/i,"").replace(/\\/g,"/");
});
});
After this, I anticipate two ways for completing your extension:
(slightly harder) Now that we have the downloads directory, let's get access to the file:/// url. Add the "file://*"
permission to your manifest file. You need to tell your user to navigate to chrome://extensions
, find your extension, and tick off "Allow access to file:// URLs" next to it. You can check to make sure the user has done this via isAllowedFileSchemeAccess.
Try it by going to file:///home/
or file:///C:/
, based on your OS. It should show a directory of the files in your computer. I believe it may be possible to use XMLHttpRequest to get the HTML of these files (hint: use xhr.responseType = "document"
), which you can parse to build your own user interface for selecting folders! (For adding folders just download a file to the path you'd like to add.)
You can also simply invoke chrome.experimental.downloads.download
with saveAs: true
to get the user to download a temporary file to the folder they want to save to. Then, remove the filename from the end, and remove the downloads path from the beginning, to get a relative path, which you can save files to in the future!
I'm quite excited to see how your extension turns out. Good luck!