This is WIP userscript for scraping book image links from archive.org
You can try scraping image links of this book https://archive.org/details/eastofsunwestofm00asbj/page/100/mode/2up

The script will work fine as long as you stay on the tab in which the website is opened. If you switch to a different tab though, the script will stop. I am guessing this problem is caused by MutationObserver. What am I doing wrong?

I tried this same script in the form of browser extension, and the same thing happens. Soon as you switch to a different tab, it stops working.

I am using firefox 87.0 (64-bit).

// ==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...");

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);
              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);

The first thing I can think of is that this might be a "power saving" feature of Firefox/Chrome. I know this happens to setInterval/setTimeout. When you put a tab in the background, these two function will no longer work. The idea is that background tabs should not use CPU and drain your battery. I guess the same could happen to MutationObserver.