chrome.downloads.download on MS Edge: windows update this week added a bug. Work-around?

55 views Asked by At

I'm working on an extension for MS Edge (Version 122.0.2365.80) running on Win10 pro. The extension has been working properly, but not any more. I'm using chrome.downloads.download in javascript and what it used to do is invoke the SaveAs dialog box pointing to the last saved-to folder. Now it points to the browser's default directory. Is anyone else seeing this? Is there an option in chrome.downloads.download to point the SaveAs in the right direction? Here's the code for the background.js:

chrome.runtime.onInstalled.addListener(function() {
  console.log('Extension installed / updated');
});

chrome.runtime.onMessage.addListener(function (request, sender) {
  if (request.type === 'popupMessage') {
    // Access parameters from the content script
  var receivedParams = request.data;

  // Do something with the parameters
    console.log(receivedParams);
chrome.downloads.download({
       url: receivedParams,
       filename: 'desired_filename.ext',
       saveAs: true
    }, function(downloadId) {
       console.log('Download initiated with ID:', downloadId);
    });
   }
 });

Thanks in advance for any pointers you can provide.

When I call the background.js, I expect it to bring up the SaveAs dialog box pointing at the last-saved-to-directory. Instead, the code (unchanged) now brings up the SaveAs dialog box pointing to the browser's default directory.

1

There are 1 answers

0
Dav0Dav0 On

Sooooo, it turns out that for an extension there's more than one way to get things done. And in my case, it's utilizing a totally different API. You no longer use the chrome.downloads.download call, which has to be buried in a backround.js "worker" file. Instead, you can use an asynch function from within your main javascript, using this code:

async function saveImage(currentURL) {try{

// Fetch the image file
const response = await fetch(currentURL);
const blob = await response.blob();

const destination = currentURL.split("/").reverse()[0];

// Request the user to choose where to save the file
const handle = await window.showSaveFilePicker({suggestedName: destination});

// Create a writable stream to the file
const writableStream = await handle.createWritable();

// Write the image data to the file
await writableStream.write(blob);

// Close the file and save changes
await writableStream.close();
} catch(err){alert(err);}}