I have an extension that operates on pages that formats data, I need to wait for the function to complete to sweep up the data on the page. In using an Event Listener on readystatechange, When "complete". This does not work, the function has not completed. I can sleep for a second and all works fine, but this seems silly. Any help? Thanks.
Asked
Active
Viewed 209 times
0
-
That's because modern pages are built using JavaScript after the initial HTML is parsed. You can use MutationObserver and [other "more proper" methods](https://stackoverflow.com/a/39508954). – wOxxOm Apr 10 '20 at 15:03
-
Thank you, Grate links, Q: I setup a MutationObserver at the interactive state of the DOM, on all script nodes. Once the all the scrips have been fired & seen by the Observer, I can assume there work is done? – user1195021 Apr 10 '20 at 18:10
-
Not universally because it depends on the page so a delay may be needed anyway, depending on what you want to do next. In modern browsers you can use PerformanceObserver to [measure LCP](https://web.dev/lcp/#measure-lcp-in-javascript) instead. – wOxxOm Apr 10 '20 at 18:19
-
Thanks: As you lauded to scripts can load scrips down the line. Are there key thinks I can look for within the scripts, key strings, such as window.view.initialState or document.createElement('script'); I just don't want to sleep if I don't have to. Thanks again – user1195021 Apr 10 '20 at 20:14
-
There are too many other methods to delay execution in modern JavaScript, moreover some sites may use obfuscation or remote scripts (not inlined) so you can't rely on source code analysis. – wOxxOm Apr 11 '20 at 05:27
-
Very helpful... So, how do web crawlers scrap content, (In a timely manner). I can see that Google has the content on there search result page (the content I am trying to get without a sleeping) How are they shaking the DOM tree? They cant rely on the ready state change, as I am doing? Something I am having a hard time finding the answer to. Thanks for any help. – user1195021 Apr 11 '20 at 11:00
-
There is no magic solution. Use MutationObserver + delay and see PerformanceObserver method I linked above. – wOxxOm Apr 11 '20 at 11:04
-
You seem to be a Stack Overflow Rock star... Thanks for your attention. I have the advantage (somewhat) of knowing what I am looking for. What seems to be a compromise to stupid sleeping. I can leverage MutationObserver, and watch scrips as they load. Watch the document.body.innerText.length expand form 0 to > 0. Use an Interval of say (100 mills) and test document.body.innerText.includes(Mytext). & bob's your uncle, not perfect, but much faster... Thanks again – user1195021 Apr 11 '20 at 20:24
-
In case you're looking for a DOM element you can simply observe the entire `document` in MutationObserver, not just the scripts, no other timeouts or sleeps will be necessary. – wOxxOm Apr 12 '20 at 04:08