// ==UserScript==
// @name        archive.org ripper
// @namespace   Violentmonkey Scripts
// @include     https://archive.org/*
// @include     https://www.archive.org/*
// @grant       GM_download
// @run-at      document-idle
// @version     1.0
// @author      -
// @description archive.org ripper
// ==/UserScript==


console.log("archive.org ripper...");

document.onreadystatechange = function () {
  if (document.readyState === 'complete') {
    var img = document.getElementsByClassName("BRpageimage");
    if(img) {
      console.log("Found");
      console.log(img);
      console.log(img.length);
      console.log(img.type);
      console.log(img[0].src);
    } else {
      console.log("Not Found");
    }
  }
}

Sometimes this script works as expected. You can try it on https://archive.org/details/eastofsunwestofm00asbj/page/n19/mode/2up

But sometimes the console throws these errors:

archive.org ripper...
Found
HTMLCollection { length: 0 }
0
Uncaught TypeError: img[0] is undefined
    onreadystatechange moz-extension://68e50867-b917-4486-9109-bb3547a1b15f/ archive.org ripper.user.js#3:24
    VMin0bjzz1xm9 moz-extension://68e50867-b917-4486-9109-bb3547a1b15f/ archive.org ripper.user.js#3:16
    VMin0bjzz1xm9 moz-extension://68e50867-b917-4486-9109-bb3547a1b15f/ archive.org ripper.user.js#3:88
    a moz-extension://68e50867-b917-4486-9109-bb3547a1b15f/sandbox/injected-web.js:1
    v moz-extension://68e50867-b917-4486-9109-bb3547a1b15f/sandbox/injected-web.js:1
    set moz-extension://68e50867-b917-4486-9109-bb3547a1b15f/sandbox/injected-web.js:1
    <anonymous> moz-extension://68e50867-b917-4486-9109-bb3547a1b15f/ archive.org ripper.user.js#3:1
    c moz-extension://68e50867-b917-4486-9109-bb3547a1b15f/sandbox/injected-web.js:1
    ScriptData moz-extension://68e50867-b917-4486-9109-bb3547a1b15f/sandbox/injected-web.js:1
    onHandle moz-extension://68e50867-b917-4486-9109-bb3547a1b15f/sandbox/injected-web.js:1
    c moz-extension://68e50867-b917-4486-9109-bb3547a1b15f/sandbox/injected-web.js:1

That HTMLCollection section when expanded shows two image tags but the line console.log(img.length) prints "0". Also, img[0].src is throwing error. Why is that? and how can this be resolved?

Re: @Just_Drawing_2035:

Also, img[0].src is throwing error. Why is that? ...

If length is 0 you are trying to access a property that doesn't exist in img[0] i.e. console.log(img[0].src);

... and how can this be resolved?

Don't call console.log(img[0].src); when .length is 0 and probably use a conditional.

That HTMLCollection section when expanded shows two image tags but the line console.log(img.length) prints "0"

How are you listing this HTMLCollection? i.e. what browser methodology and .user.js engine? This sentence doesn't seem to make much sense here.

There's a heavy amount of JavaScript happening on the sample page you gave and a sub-section of SVG text so you might need to try using a MutationObserver to get more accurate handles on what you are going for.

Re: @Martii:

I tried MutationObserver, and it worked well. Thanks. Although I now have a new problem.
Here is the new code:

// ==UserScript==
// @name        archive.org ripper
// @namespace   Violentmonkey Scripts
// @include     https://archive.org/*
// @include     https://www.archive.org/*
// @grant       GM_download
// @run-at      document-idle
// @version     1.0
// @author      -
// @description Rips books from archive.org
// ==/UserScript==

console.log("archive.org ripper...");

const targetNode = document.getElementById('IABookReaderWrapper');
const config = { attributes: true, childList: true, subtree: true };
var img_links = [];

const callback = function(mutationsList, observer) {
    for(const mutation of mutationsList) {
        if (mutation.type === 'childList' && mutation.target.classList[0] === 'BRpagecontainer' && mutation.target.classList[1] === 'BRmode2up') {
              var images = mutation.target.getElementsByClassName('BRpageimage');
              img_links.push(images[0].src);
              current_url = window.location.href;
              if(img_links.length % 2 == 0) {
                  var btn_next = document.getElementsByClassName("book_flip_next")[0];
                  btn_next.click();
                  console.log(img_links.length);  
                  console.log(img_links);
              }
        }
    }
};

const observer = new MutationObserver(callback);
observer.observe(targetNode, config);

Now all this works quite well. So I open https://archive.org/details/eastofsunwestofm00asbj/page/n19/mode/2up and the code works by storing the url of current images into the array img_links and then the btn_next is clicked, and the process goes on.

The problem is that when I switch to browser-tab-B while this script is in action in browser-tab-A. The script stops working. Why is that? and how can this be solved?

Re: @Just_Drawing_2035:

The problem is that when I switch to browser-tab-B while this script is in action in browser-tab-A. The script stops working. Why is that? and how can this be solved?

That's an odd one. Usually the browser controls if a tab is suspended. If it's Firefox (Fx) then you might search for a preference on that. Chromium based (Chrome/Edge/Opera, etc.) is a bit more vague and you might not be able to get at that setting... but of course I haven't been in there much lately so can't say that for sure.

You might also want to look into trying a different // @run-at document-idle value to see if it works better. Usually in Moz based browsers and GM directly there are earlier triggers compared to that one. VM .user.js engine might have an issue too with it's script injection but less likely.

Now all this works quite well.

Glad that's getting your handles for you. :)

Re: @Just_Drawing_2035:

Have you tried the script in your browser?

I have not... because I'm working on the sites back-end atm. That has higher priority, but maybe in a day or so I can try it... just don't have the time atm... going off memory here on past things I've done and passing on a suggestion or so for you to try.

If Fx is having some issues resuming a tab then that could be a bug to search for on bugzilla... but just be sure. In the observer just do the equivalent of a "ping" console message... might fill up the log quickly until idle so be warned. ;)