This is WIP userscript for scraping book image links from
You can try scraping image links of this book

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 ripper
// @namespace   Violentmonkey Scripts
// @include*
// @include*
// @grant       GM_download
// @run-at      document-idle
// @version     1.0
// @author      -
// @description Rips books from
// ==/UserScript==


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' &&[0] === 'BRpagecontainer' &&[1] === 'BRmode2up') {
              var images ='BRpageimage');
              if(img_links.length % 2 == 0) {
                  var btn_next = document.getElementsByClassName("book_flip_next")[0];

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.