1

Edit: Thanks to @Dogoku, I was able to find a solution (here). It turns out that Chrome doesn't allow/handle the download attribute anymore, so we have to force a workaround.


I'm trying to create a javascript bookmarklet that downloads images (code shown below). It's accessing the url correctly, but instead of downloading the image it just opens the image in a new tab.

Please help!

r.addEventListener('click', function(ev) {
    r.href = h;
    r.download = c;
}, false);

document.body.appendChild(r), r.click(), document.body.removeChild(r);

the variable r is created using document.createElement("a") which creates an element like so:

<a href="url" download="filename"></a>

Before, when using a bookmarklet you could basically just "click" on the image and it would download.

a_lovelace
  • 490
  • 4
  • 11
  • what is c that is set to the download property? can you try setting a string (filename)? – Dogoku May 07 '18 at 14:11
  • c is the filename :) The image is part of a set that is dynamically grabbed and looped through. Then, the image url is parsed to get the file name. – a_lovelace May 07 '18 at 14:17
  • I don't see anything wrong. is the first image of the loop downloading correctly, but failing for others? perhaps trying putting a delay between the downloads? – Dogoku May 07 '18 at 14:34
  • I can log out the entire process and it's looping through all of them - so nothing is failing; however, instead of downloading the image it's opening it (all of them) in a new tab and doing nothing. – a_lovelace May 07 '18 at 14:36
  • could this be related (if img urls are considered cross origin) https://stackoverflow.com/questions/49474775/chrome-65-blocks-cross-origin-a-download-client-side-workaround-to-force-down? – Dogoku May 07 '18 at 14:53
  • @Dogoku I'm looking into that! It would make a lot of sense if that's the issue I'm running into – a_lovelace May 07 '18 at 15:32

0 Answers0