Skip to content

fix(main): navigate to domain before cookie/header strategy commands#70

Closed
backtime1993 wants to merge 1 commit intojackwener:mainfrom
backtime1993:fix/cookie-strategy-domain-navigation
Closed

fix(main): navigate to domain before cookie/header strategy commands#70
backtime1993 wants to merge 1 commit intojackwener:mainfrom
backtime1993:fix/cookie-strategy-domain-navigation

Conversation

@backtime1993
Copy link
Contributor

Summary

  • Cookie/header strategy commands (e.g. bilibili search, twitter timeline, zhihu hot) fail with Failed to fetch when the active Chrome tab is on an unrelated domain
  • Root cause: fetch() with credentials: 'include' is executed in-page, but cross-origin credentialed requests are blocked by the browser's same-origin policy
  • Fix: navigate to cmd.domain before executing cookie/header strategy commands, mirroring the pre-navigation already done in the cascade command

Repro

  1. Have YouTube (or any non-bilibili page) as the active Chrome tab
  2. Run opencli bilibili search --keyword "test"
  3. Error: Failed to fetch

After fix: opencli automatically navigates to bilibili.com first, then runs the API fetch in same-origin context.

Changes

  • src/main.ts: Add domain pre-navigation for Strategy.COOKIE and Strategy.HEADER commands before executing the command function
  • Import Strategy enum from registry

Test plan

  • bilibili search works regardless of active Chrome tab
  • bilibili hot (public strategy, no cookie) still works
  • youtube search (cookie strategy) still works
  • TypeScript type check passes (tsc --noEmit)

🤖 Generated with Claude Code

… commands

When a cookie/header strategy command runs, the browser page may be on
an unrelated domain (e.g. YouTube tab active while running `bilibili search`).
The in-page `fetch()` with `credentials: 'include'` then fails because
the browser blocks cross-origin credentialed requests.

Fix: before executing cookie/header strategy commands, navigate to the
command's declared `domain` so the fetch runs in same-origin context.
This mirrors the pre-navigation already done in `cascade` command.

Fixes intermittent "Failed to fetch" errors for all cookie-strategy
adapters (bilibili, twitter, zhihu, xueqiu, etc.) when the active
Chrome tab is on a different site.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@backtime1993
Copy link
Contributor Author

Closing this — after further investigation, this issue only occurs with CDP auto-discovery mode (not yet upstream). The fix belongs in the chrome-auto-discovery feature branch instead. Sorry for the noise!

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.

1 participant