0

I am writing a Chrome extension that scrapes data from Twitter, specifically from twitter.com/following, using a content script. I want to run this in a tab/window that is not active or visible to the user, so as to not be disruptive.

Now, Twitter loads much of its content using scripts, so I am using MutationObserver and window.scrollBy to force the content to load. This works perfectly fine when the tab is visible. However, when the tab is not visible (inacive tab or window that is minimized/offscreen), it does not work. Firstly, the setInterval timers get throttled to once a second. I did some research and I think I can fix this using requestAnimationFrame, but have not tested yet. The more pressing problem is that the content that I am looking for, ie. the list of followed accounts, no longer gets loaded by Twitter's scripts.

I have tried to spoof the tab as always visible using this code snippet, but it did not fix the issue.

Object.defineProperty(document, "visibilityState", {
    get() {
        return "visible";
    }
});

document.addEventListener("visibilitychange", function(e) {
    e.preventDefault();
    e.stopPropagation();
    e.stopImmediatePropagation();
});

Is there any way to give non-visible tabs the functionality of visible tabs, if temporarily?

PS: I know that Twitter has an API, but I would prefer to not have to deal with hosting an application if I can avoid it.

  • This code should be placed in [page context](/a/9517879). – wOxxOm Sep 09 '21 at 05:23
  • I made a extension a long time ago https://github.com/zoutepopcorn/scroll. I didn't face the problem then. Maybe add some dummy audio or video to keep the page active? – Johan Hoeksma Sep 09 '21 at 07:05
  • Or maybe using a chrome.runtime.sendmessage() with the setInterval() in the background.js. activating the twitter tab to execute a function ? – Johan Hoeksma Sep 09 '21 at 08:37

0 Answers0