0

I am downloading a file (in JavaScript), decrypting it with a library, and then I am creating a blob URL. This happens as a result of a user interaction where a user clicks a link. So I am not trying to fool anyone. To download the file after it has been decrypted, I am using the following code:

var a = document.createElement('a');
a.href = 'blob:foo';
a.target = '_blank';
a.download = 'foo.jpg';
a.click();

However, this is not always successful.

When is it always successful?

  • When the user does not use an ad blocker.

When is it not successful?

  • If the user uses an ad blocker that blocks blob URLs (e.g. Adblock Plus through EasyList), and does not support the download attribute.

So I want to show an alert to the user if the download is not successful. Is there any way to detect that?

My current idea is to:

  • Detect if there's an ad blocker
  • Check if the download attribute is supported (!window.externalHost && 'download' in createElement('a'))
  • Notify the user of potential problems
HelloWorld
  • 3,381
  • 5
  • 32
  • 58
  • You forgot an other vector for failure: user clicks "Cancel" in the "save as" prompt. There is unfortunately nothing letting us know what happens after the user clicked on an ``, so while you may know when it will never work, you won't be able to know when it will always work. – Kaiido Jun 12 '18 at 00:44
  • You can detect if they're using AdBlocker and display a message telling them they need to disable it in order for the download to work, but JavaScript does not have any capabilities to detect whether or not the file successfully started downloading. – Andrew Jun 12 '18 at 00:45
  • Maybe this helps you. https://stackoverflow.com/questions/1106377/detect-when-browser-receives-file-download – Terry Wei Jun 12 '18 at 00:46
  • But it doesn't says how to detect downloaded and I think that is actually cannot be detected. – Terry Wei Jun 12 '18 at 00:47
  • And as to catch adblockers, there are so many out there, with so many implementations that catching them all is almost impossible. For instance, one might purely and simply block blob URIs, returning a fake string from createObjectURL, another could purely block the `download` attribute if the URI starts with `blob:`, these two cases alone would require different tests. And there might be numerous other way to implement this *feature* – Kaiido Jun 12 '18 at 00:47
  • @TerryWei this is for a download originating from a server, not from the browser's memory. Andrew all ad-blockers don't block downloading from blobURIs – Kaiido Jun 12 '18 at 00:47

1 Answers1

-1

ad blockers do not block downloads, unless the requested URL matches a filter in the filterlists the user selected.

What I suggest you to do is to find that filter and work around it, or communicate with the easylist community so your URL is no longer blocked.

Here is their forum: https://forums.lanik.us/

mundo03
  • 1
  • 2
  • In this case it the scheme blob:// which is blocked. Sure every user could allow it in their prefs, but I guess OP is willing for a way around asking their user to change it manually. – Kaiido Jul 28 '18 at 16:04