Skip to content

fix: keep init scripts available to page actions#361

Closed
malsabbagh05 wants to merge 2 commits into
D4Vinci:devfrom
malsabbagh05:fix/init-script-page-action-fallback
Closed

fix: keep init scripts available to page actions#361
malsabbagh05 wants to merge 2 commits into
D4Vinci:devfrom
malsabbagh05:fix/init-script-page-action-fallback

Conversation

@malsabbagh05

Copy link
Copy Markdown

Summary

  • preserve normal context-level init_script registration
  • add a marker to detect when the browser backend did not run the init script
  • apply the configured init script before page_action only when the marker is missing
  • add an async stealth regression test for issue No context persistence for code loaded using init_script #350

Tests

python -m compileall -q scrapling/engines/_browsers tests/fetchers/async/test_stealth_session.py
pytest tests/fetchers/async/test_stealth_session.py tests/fetchers/sync/test_stealth_session.py tests/fetchers/async/test_dynamic_session.py tests/fetchers/sync/test_dynamic.py -q

Result:

19 passed

Also verified the original issue shape against https://example.com, where page_action observed seen [2].

Fixes #350

Register the normal context-level init script as before, then add a marker script to detect whether the browser backend actually ran it. If the marker is missing after navigation, apply the configured init script to the active page before user callbacks run.
@samrusani

Copy link
Copy Markdown

Ran a local review/validation pass against commit 84996b7.

Validation:

python -m compileall -q scrapling/engines/_browsers tests/fetchers/async/test_stealth_session.py
python -m pytest tests/fetchers/async/test_stealth_session.py::TestAsyncStealthySession::test_init_script_is_available_in_page_action -q
python -m pytest tests/fetchers/async/test_stealth_session.py tests/fetchers/sync/test_stealth_session.py tests/fetchers/async/test_dynamic_session.py tests/fetchers/sync/test_dynamic.py -q
python -m ruff check scrapling/engines/_browsers/_base.py scrapling/engines/_browsers/_controllers.py scrapling/engines/_browsers/_stealth.py tests/fetchers/async/test_stealth_session.py
git diff --check origin/dev...HEAD

Results: targeted regression passed (1 passed), the listed browser-session suite passed (19 passed), ruff passed, and diff whitespace checks were clean.

I also checked the control flow around page_setup/navigation/page_action; the fallback stays scoped to the configured init_script path and still preserves the normal context-level registration path. I did not find a blocking issue in this patch.

@yetval

yetval commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Thanks, but this was already settled in #353: the init script does run, it just sits in a different world than page.evaluate, so isolated_context=False is the whole fix. This patch only re-runs the script after load, which masks the symptom. I'd close in favor of #353.

@samrusani

Copy link
Copy Markdown

Thanks for clarifying. That makes sense: my local pass only validated that this patch made the page_action repro observe the global, not that re-running the script after load was the right fix. I agree the isolated_context=False / docs path in #353 is the cleaner resolution.

@malsabbagh05

Copy link
Copy Markdown
Author

Thanks for the clarification. I agree that the #353 approach is the cleaner fix here, since the issue is the script world/visibility behavior rather than needing to re-run the init script after load. Closing this in favor of #353.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants