0

I'm working on a browser extension that scrapes data from a webpage. I've added a method to scrape data, which is invoked via a load listener: window.addEventListener('load', scrapeSomething) However, when I refresh the page and put a breakpoint in the Chrome Developer Tools, I notice that the HTML is still not fully loaded when the method is invoked. For instance, the HTML content at the time of invocation is just: <div id="hiddenFrame"></div>

To mitigate this issue temporarily, the previous code had a 5-second delay before running the scraping logic:

let callDebouncer = () => {
setTimeout(() => {
//scraping part
},5000);  };
window.addEventListener('load', callDebouncer);

I want to avoid using a fixed delay and instead rely on a listener to ensure the page is fully loaded before scraping. Is there an alternative way to achieve this?

10
  • 1
    dynamic sites require expertise to borrow from Commented Jul 26, 2024 at 11:35
  • window.load fires when everything is loaded into window. Most likely there's no other content to load on the page you're navigating to. Commented Jul 26, 2024 at 11:36
  • So why does a 5 second delay allow the site to be "borrowed" from if there's no other content @Teemu Commented Jul 26, 2024 at 11:37
  • 1
    @JaromandaX I guess there is, a script in the head section, maybe using a timeout to create more content, or even a meta tag re-directing the page, Commented Jul 26, 2024 at 11:38
  • 3
    "to ensure the page is fully loaded" - there's no way to know this. If the page uses javascript to build html, it could do it at any time. If the page uses ajax to build the html, it could load some, then load some more. eg a "chat" app would never be "fully loaded". If it's for a specific page, you would need to reverse-engineer what that page is doing. Commented Jul 26, 2024 at 11:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.