Skip to content

feat: add login command for no-popup + cookies headless mode#40

Open
cklxx wants to merge 3 commits intojackwener:mainfrom
cklxx:feat/headless-mode
Open

feat: add login command for no-popup + cookies headless mode#40
cklxx wants to merge 3 commits intojackwener:mainfrom
cklxx:feat/headless-mode

Conversation

@cklxx
Copy link

@cklxx cklxx commented Mar 17, 2026

Summary

  • Add opencli login command: opens Chrome via extension mode once to save cookies to ~/.opencli/session.json
  • Add --headless flag: loads saved cookies via --storage-state for silent background execution with full login state
  • --isolated is required with --storage-state (works with newContext(), not launchPersistentContext())

Usage

opencli login                    # one-time setup, saves cookies
opencli --headless bilibili me   # silent, no popup, cookies loaded

Test Plan

  • opencli login saves session file
  • opencli --headless bilibili me returns logged-in user info
  • headless works without session file (no --storage-state passed)
  • all unit tests pass (npm test)

@cklxx cklxx force-pushed the feat/headless-mode branch from db7ced5 to eff1ed6 Compare March 17, 2026 10:52
@cklxx cklxx changed the title feat: add --headless mode with persistent cookie session feat: add login command for no-popup + cookies headless mode Mar 17, 2026
@cklxx
Copy link
Author

cklxx commented Mar 17, 2026

@jackwener 佬,搞个静默的打开模式 chrome 不弹窗

chenkailun.c added 3 commits March 18, 2026 18:29
Implement "不抢窗口 + 有 cookie" flow using Playwright storage state:

**Usage:**
1. Run `opencli login` once — opens your Chrome (extension mode),
   saves all cookies to ~/.opencli/session.json
2. Run `opencli --headless <site> <cmd>` — headless browser with your
   saved cookies, no popup window

**How it works:**
- login: connects via extension (has all user cookies), calls
  browser_storage_state (requires --caps storage) to save full cookie
  jar (including HttpOnly) to session file
- --headless: passes --storage-state to @playwright/mcp so the headless
  browser context starts with all saved cookies

**Changes:**
- `src/browser/discover.ts`: add defaultSessionFile() → ~/.opencli/session.json;
  buildMcpArgs now loads --storage-state when session file exists (headless),
  accepts caps[] for capability flags, uses --user-data-dir only in CI
- `src/browser/page.ts` + `src/types.ts`: add saveStorageState(filename)
- `src/browser/mcp.ts` + `src/runtime.ts`: thread caps[] through connect()
- `src/main.ts`: add `opencli login` command
- `src/browser.test.ts`: add headless buildMcpArgs tests
storageState is only applied in browser.newContext() (isolated context),
not in launchPersistentContext() which is the default. Without --isolated,
the storage state file is silently ignored and cookies are never loaded.
Code fixes:
- discover.ts: fix copy-paste comment error on userDataDir param
- main.ts: move headless domain navigation inside runWithTimeout so it
  is bounded by the command timeout (previously unbounded)

README (en + zh-CN):
- Highlight no-popup mode as top feature
- Add "No-popup mode" quick-start section with opencli login + --headless
- Update prerequisites tip to mention login + headless workflow
@cklxx cklxx force-pushed the feat/headless-mode branch from 3d12368 to dca9adc Compare March 18, 2026 10:31
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