Hello, I am facing issues when I try to download a file in a safari browser extension.
I tried multiple methods, I followed also this thread https://developer.apple.com/forums/thread/119017. But they did not work for me.
For a safari browser extension, it seems that the download href attribute does not work. browser.downloads api is not available, also the window.saveAs is not available.
In a safari browser extension, the download attribute doesn't do anything, it just opens the file in the same tab, and the user is loosing the current tab of the extension. Using window.open as suggested in the topic above helps, but the UX is terrible, because the user has to allow the popup to be opened.
So is there a way to perform file downloads from safari extension?
Here are the methods that I tried:
- Creating an element from javascript. The issue here is that the download attribute does not work, it moves the user to the file in the same tab. And the user can't go back.
const element = document.createElement('a'); element.setAttribute('href', uri); element.setAttribute('download', filename); element.style.display = 'none'; document.body.appendChild(element); element.click(); document.body.removeChild(element); }
- Using window.open. Here the user needs to approve the popup, and it's a hard/confusing thing to do for a non-experienced user.
function downloadPWA(content) { var file = new Blob([content], { type: 'text/xml;charset=UTF-8' }); var reader = new FileReader(); reader.onload = function() { var popup = window.open(); var link = document.createElement('a'); link.setAttribute('href', reader.result); link.setAttribute('download', 'filename.txt'); popup.document.body.appendChild(link); link.click(); } reader.readAsDataURL(file); }
Here is a GitHub repo of a small dummy test extension where I tested the download functions https://github.com/mihainsto/Safari-extension-download-file .