Remove unconditional setTimeout waits from overlay/consent removal scripts - #2139
Open
SohamKukreti wants to merge 1 commit into
Open
Remove unconditional setTimeout waits from overlay/consent removal scripts#2139SohamKukreti wants to merge 1 commit into
SohamKukreti wants to merge 1 commit into
Conversation
…al scripts In-page timers never fire on CSP-sandboxed pages (GitHub/HuggingFace raw), hanging crawls 30s-to-forever; waits now run Python-side or only after a consent action actually fired.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Crawling pages served with a CSP
sandboxdirective (e.g.raw.githubusercontent.com,huggingface.co/*/raw/*, Jenkins-hosted user content) withremove_overlay_elements=Trueorremove_consent_popups=Truestalled ~30s per page — or hung indefinitely — with the warningFailed to remove overlay elements: Page.evaluate: Execution context was destroyed, most likely because of a navigation.See discussion #2119 for the issue
Root cause: the CSP
sandboxdirective disables script timers, so the unconditionalawait new Promise(r => setTimeout(r, ...))waits inside the injected overlay/consent-removal scripts never resolve. Chromium tears the execution context down after ~30s (GitHub raw), or never (HuggingFace raw), deadlocking the crawl.Fix: remove the unconditional in-page waits. The overlay script's trailing settle wait moves to the existing Python-side
wait_for_timeout(immune to page CSP); the consent script's mid-script wait now runs only when a consent action (button click or CMP API call) actually fired — which cannot happen on sandboxed raw pages. Total settle budgets before HTML capture are unchanged, so scraping output on normal pages is identical.Measured: GitHub raw crawls ~31s → ~1.3s; HuggingFace raw hung >130s → ~1.5s. Verified no regression on live CMP sites (BBC, The Verge, The Guardian) and against a synthetic "watchdog" CMP that re-injects its banner if removed before consent registers (both click and vendor-API consent paths).
List of files changed and why
crawl4ai/js_snippet/remove_overlay_elements.js— removed the trailing 50mssetTimeoutwait that hung forever under CSP sandbox; post-click waits kept.crawl4ai/js_snippet/remove_consent_popups.js— the mid-script 500ms wait is now conditional on a consent action having fired (accepted || apiCalled); added theapiCalledflag to the Didomi/Cookiebot/Osano/Klaro API branches, which previously didn't record that they acted.crawl4ai/async_crawler_strategy.py— overlay settle wait 500→600ms to absorb the removed in-page wait, keeping the pre-capture budget identical.tests/regression/test_reg_browser.py— addedtest_overlay_removal_on_csp_sandbox_page: crawls a commit-pinnedraw.githubusercontent.comURL (live CSPsandboxheader, immutable content) with both flags on and asserts completion well under the old stall time.How Has This Been Tested?
test_remove_overlay_elementsstill passes.Checklist: