|
| 1 | +--- |
| 2 | +tags: [Web, JavaScript, Browser] |
| 3 | +references: ["https://developer.mozilla.org/es/docs/Web/API/Window/setTimeout", "https://stackoverflow.com/questions/5525071/how-to-wait-until-an-element-exists"] |
| 4 | +thumbnail: "/assets/images/other/calamars_amazon_photos.jpg" |
| 5 | +--- |
| 6 | + |
| 7 | +The pieces of shit from Amazon do not allow to download at once all the images from Amazon Photos. Below I will show a little js code i wrote to automatize the process. |
| 8 | + |
| 9 | +<!--more--> |
| 10 | + |
| 11 | +Open Amazon Photos in a browser and select the image or video you want to start from. Then, press F12 to open the development tools, go to the console tab and paste the js code: |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +```js |
| 16 | +const MS_TRANSITION_DELAY = 600; |
| 17 | +const MS_BETWEEN_DOWNLOADS = 100; |
| 18 | + |
| 19 | +var nextArrow; |
| 20 | +downloadImageAndNext(); |
| 21 | + |
| 22 | + |
| 23 | +function next() { |
| 24 | + if (!next) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + nextArrow.click(); |
| 29 | + |
| 30 | + setTimeout(downloadImageAndNext, MS_TRANSITION_DELAY); |
| 31 | +} |
| 32 | + |
| 33 | +function downloadImageAndNext() { |
| 34 | + waitForElm(".download").then((downloadButton) => { |
| 35 | + downloadButton.click(); |
| 36 | + |
| 37 | + nextArrow = getNextArrow(); |
| 38 | + |
| 39 | + setTimeout(next, MS_BETWEEN_DOWNLOADS); |
| 40 | + }); |
| 41 | + |
| 42 | + var toggle = document.getElementsByClassName("toggle")[4]; |
| 43 | + toggle.click(); |
| 44 | +} |
| 45 | + |
| 46 | +function getNextArrow() { |
| 47 | + return document.getElementsByClassName("next")[0]; |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +// From https://stackoverflow.com/questions/5525071/how-to-wait-until-an-element-exists |
| 52 | +function waitForElm(selector) { |
| 53 | + return new Promise(resolve => { |
| 54 | + if (document.querySelector(selector)) { |
| 55 | + return resolve(document.querySelector(selector)); |
| 56 | + } |
| 57 | + |
| 58 | + const observer = new MutationObserver(mutations => { |
| 59 | + if (document.querySelector(selector)) { |
| 60 | + observer.disconnect(); |
| 61 | + resolve(document.querySelector(selector)); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + // If you get "parameter 1 is not of type 'Node'" error, see https://stackoverflow.com/a/77855838/492336 |
| 66 | + observer.observe(document.body, { |
| 67 | + childList: true, |
| 68 | + subtree: true |
| 69 | + }); |
| 70 | + }); |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +Press enter to execute the code. It will download simulate a click to the top-right toggle, click the download button and then the next arrow to transition to the next image. This process will continue until it reaches the last image or fails for some reason[^1]. If it does not start moving automatically, press the top-right toggle manually and after that it should continue on its own. |
| 75 | + |
| 76 | +## Constants |
| 77 | + |
| 78 | +There are 2 constants you should adapt to your needs. |
| 79 | + |
| 80 | +### MS_TRANSITION_DELAY |
| 81 | + |
| 82 | +The milliseconds to wait between the next arrow press and the start of the download of the next image. The lower the better, but beware not to make it to small, it should be enough for the transition to take place. |
| 83 | + |
| 84 | +### MS_BETWEEN_DOWNLOADS |
| 85 | + |
| 86 | +The milliseconds to wait between the start of the download and the transition to the next image. The purpose of this delay is to avoid accumulating a large number of simultaneous downloads. You should adjust it according to your internet speed and the size of the media. |
| 87 | + |
| 88 | +I recommend downloading the images and the videos separately because of their size difference. There is a filter in the left that allows this. |
| 89 | + |
| 90 | +- You can download all the images with a `MS_BETWEEN_DOWNLOADS` of approximately 0. |
| 91 | + |
| 92 | +- In the case of the videos, since they take a lot more space, you can set `MS_BETWEEN_DOWNLOADS` to 2000, 5000, 20000... or whatever milliseconds are necessary to avoid ending with >5 videos downloading at the same time. As I said, it depends on your internet speed. |
| 93 | + |
| 94 | + |
| 95 | +## Footnotes |
| 96 | + |
| 97 | +[^1]: Don't worry if it fails after some images, the process can be started over again from any image. |
0 commit comments