MCPcopy Create free account
hub / github.com/ChromeDevTools/chrome-devtools-mcp / waitForStableDom

Method waitForStableDom

src/WaitForHelper.ts:40–87  ·  view source on GitHub ↗

* A wrapper that executes a action and waits for * a potential navigation, after which it waits * for the DOM to be stable before returning.

()

Source from the content-addressed store, hash-verified

38 * for the DOM to be stable before returning.
39 */
40 async waitForStableDom(): Promise<void> {
41 const stableDomObserver = await this.#page.evaluateHandle(timeout => {
42 let timeoutId: ReturnType<typeof setTimeout>;
43 function callback() {
44 clearTimeout(timeoutId);
45 timeoutId = setTimeout(() => {
46 domObserver.resolver.resolve();
47 domObserver.observer.disconnect();
48 }, timeout);
49 }
50 const domObserver = {
51 resolver: Promise.withResolvers<void>(),
52 observer: new MutationObserver(callback),
53 };
54 // It's possible that the DOM is not gonna change so we
55 // need to start the timeout initially.
56 callback();
57
58 domObserver.observer.observe(document.body, {
59 childList: true,
60 subtree: true,
61 attributes: true,
62 });
63
64 return domObserver;
65 }, this.#stableDomFor);
66
67 this.#abortController.signal.addEventListener('abort', async () => {
68 try {
69 await stableDomObserver.evaluate(observer => {
70 observer.observer.disconnect();
71 observer.resolver.resolve();
72 });
73 await stableDomObserver.dispose();
74 } catch {
75 // Ignored cleanup errors
76 }
77 });
78
79 return Promise.race([
80 stableDomObserver.evaluate(async observer => {
81 return await observer.resolver.promise;
82 }),
83 this.timeout(this.#stableDomTimeout).then(() => {
84 throw new Error('Timeout');
85 }),
86 ]);
87 }
88
89 async waitForNavigationStarted() {
90 // Currently Puppeteer does not have API

Callers 1

Calls 3

timeoutMethod · 0.95
observeMethod · 0.80
disposeMethod · 0.45

Tested by

no test coverage detected