Hello, I am trying to build a chrome extension. Where I want to download image files. In my background script, I am trying to fetch the image and create a blob URL from the date but I am getting the error. Can anyone explain where is the problem?
fetch(url).then( data => { let dataUrl = URL.createObjectURL( data.blob() ); chrome.downloads.download({ url : dataUrl, filename : "your-file-name" // Optional }); });
URL.createObjectURL is not allowed in a service worker so you can’t use the function there. You can just use a data URL.
const downloadImage = async (url) => { const data = await fetch(url); const blob = await data.blob(); const reader = new FileReader(); reader.readAsDataURL(blob); reader.onloadend = () => { chrome.downloads.download({ url : reader.result, filename : "your-file-name" // Optional }); } }
I didn’t test it but it should work.