Skip to content

Instantly share code, notes, and snippets.

@turlockmike
Created March 16, 2026 19:51
Show Gist options
  • Select an option

  • Save turlockmike/b9444a43dff0c81edd361fdb029d7c46 to your computer and use it in GitHub Desktop.

Select an option

Save turlockmike/b9444a43dff0c81edd361fdb029d7c46 to your computer and use it in GitHub Desktop.
X (Twitter) research skill for Claude Code — search, read posts, view profiles via agent-browser

X Research

Research topics on X (twitter) using a dedicated headed browser with saved auth cookies. Separate window from main Chrome.

When to use

  • Real-time AI/LLM discourse, announcements, and opinions from thought leaders
  • Breaking news or emerging topics not yet covered by blogs or docs
  • Checking what specific people are saying about a topic
  • Gathering community sentiment or reactions to launches, papers, or tools

Setup (one-time, or when session expires)

  1. Open x.com in Chrome and confirm you're logged in
  2. Enable Chrome remote debugging: chrome://inspect/#remote-debugging → Allow
  3. Run x-export-cookies.sh to copy auth cookies
  4. Prereq: pip install websockets (needed by x-export-cookies.sh)

Scripts

Script Purpose
x-search.sh "" [top|latest] Search X. Defaults to Top tab.
x-read.sh Read a specific post or thread.
x-profile.sh View a user's recent posts.
x-export-cookies.sh Copy X auth cookies from Chrome.

All scripts launch a separate headed browser window (not your main Chrome). Output is an agent-browser accessibility snapshot. Use agent-browser directly for further interaction (click, scroll, expand).

Tips

  • X search supports operators: from:user, to:user, since:YYYY-MM-DD, until:YYYY-MM-DD, min_faves:100, min_retweets:50, filter:links
  • Truncated posts show a "Show more" button — click its ref to expand
  • To scroll for more results: agent-browser scroll down 800
  • To read a thread: click into a post, then snapshot to see replies
  • High engagement (likes, reposts, views) is a useful signal for relevance
  • If login fails (cookies expired), re-run x-export-cookies.sh
#!/usr/bin/env bash
# Export X.com auth cookies from Chrome (via CDP) to agent-browser state file.
# Run this when X session expires or after logging into X in Chrome.
#
# Prerequisites: Chrome remote debugging enabled at chrome://inspect/#remote-debugging
# An x.com tab open in Chrome
#
# Usage:
# x-export-cookies.sh
set -euo pipefail
STATE_DIR="$HOME/.agent-browser"
STATE_FILE="$STATE_DIR/x-state.json"
PORT_FILE="$HOME/Library/Application Support/Google/Chrome/DevToolsActivePort"
if [[ ! -f "$PORT_FILE" ]]; then
echo '{"error":"Chrome remote debugging not enabled. Go to chrome://inspect/#remote-debugging"}' >&2
exit 1
fi
mkdir -p "$STATE_DIR"
python3 - "$PORT_FILE" "$STATE_FILE" << 'PYEOF'
import asyncio, json, sys, websockets
async def export(port_file, state_file):
with open(port_file) as f:
lines = f.read().strip().split("\n")
port, ws_path = lines[0], lines[1]
uri = f"ws://127.0.0.1:{port}{ws_path}"
async with websockets.connect(uri, max_size=20_000_000) as ws:
await ws.send(json.dumps({"id": 1, "method": "Target.getTargets"}))
resp = json.loads(await asyncio.wait_for(ws.recv(), timeout=10))
targets = resp.get("result", {}).get("targetInfos", [])
x_target = None
for t in targets:
url = t.get("url", "")
if "x.com" in url and t.get("type") == "page" and not url.startswith("blob:"):
x_target = t
break
if not x_target:
print('{"error":"No x.com tab open in Chrome"}', file=sys.stderr)
raise SystemExit(1)
await ws.send(json.dumps({
"id": 2,
"method": "Target.attachToTarget",
"params": {"targetId": x_target["targetId"], "flatten": True}
}))
session_id = None
for _ in range(10):
msg = json.loads(await asyncio.wait_for(ws.recv(), timeout=5))
if msg.get("id") == 2:
session_id = msg.get("result", {}).get("sessionId")
break
if not session_id:
print('{"error":"Failed to attach to x.com tab"}', file=sys.stderr)
raise SystemExit(1)
await ws.send(json.dumps({"id": 3, "method": "Network.enable", "sessionId": session_id}))
for _ in range(10):
msg = json.loads(await asyncio.wait_for(ws.recv(), timeout=5))
if msg.get("id") == 3:
break
await ws.send(json.dumps({"id": 4, "method": "Network.getCookies", "sessionId": session_id}))
cookies = []
for _ in range(10):
msg = json.loads(await asyncio.wait_for(ws.recv(), timeout=5))
if msg.get("id") == 4:
cookies = msg.get("result", {}).get("cookies", [])
break
pw_cookies = []
for c in cookies:
pw_cookies.append({
"name": c["name"],
"value": c["value"],
"domain": c.get("domain", ".x.com"),
"path": c.get("path", "/"),
"httpOnly": c.get("httpOnly", False),
"secure": c.get("secure", False),
"sameSite": c.get("sameSite", "None"),
"expires": c.get("expires", -1)
})
state = {"cookies": pw_cookies, "origins": []}
with open(state_file, "w") as f:
json.dump(state, f, indent=2)
http_only = len([c for c in pw_cookies if c["httpOnly"]])
print(json.dumps({"cookies": len(pw_cookies), "httpOnly": http_only, "file": state_file}))
asyncio.run(export(sys.argv[1], sys.argv[2]))
PYEOF
#!/usr/bin/env bash
# View an X user's recent posts.
# Uses a dedicated headed browser with saved auth cookies (separate from main Chrome).
#
# Usage:
# x-profile.sh AnthropicAI
# x-profile.sh kaboroevich
#
# Setup: Run x-export-cookies.sh once to copy X session from Chrome.
set -euo pipefail
HANDLE="${1:?Usage: x-profile.sh <handle>}"
HANDLE="${HANDLE#@}"
STATE_FILE="$HOME/.agent-browser/x-state.json"
agent-browser --headed --state "$STATE_FILE" open "https://x.com/$HANDLE" 2>&1
sleep 3
agent-browser snapshot -i -c --selector "main" 2>&1
#!/usr/bin/env bash
# Read a specific X post/thread by URL.
# Uses a dedicated headed browser with saved auth cookies (separate from main Chrome).
#
# Usage:
# x-read.sh https://x.com/user/status/123456
#
# Setup: Run x-export-cookies.sh once to copy X session from Chrome.
set -euo pipefail
URL="${1:?Usage: x-read.sh <post-url>}"
STATE_FILE="$HOME/.agent-browser/x-state.json"
agent-browser --headed --state "$STATE_FILE" open "$URL" 2>&1
sleep 3
agent-browser snapshot -c --selector "main" 2>&1
#!/usr/bin/env bash
# Search X (twitter) and return a snapshot of results.
# Uses a dedicated headed browser with saved auth cookies (separate from main Chrome).
#
# Usage:
# x-search.sh "AI agents 2026" # Top results (default)
# x-search.sh "AI agents 2026" latest # Latest results
# x-search.sh "from:AnthropicAI Claude" # Search specific account
#
# Setup: Run x-export-cookies.sh once to copy X session from Chrome.
set -euo pipefail
QUERY="${1:?Usage: x-search.sh <query> [top|latest]}"
TAB="${2:-top}"
STATE_FILE="$HOME/.agent-browser/x-state.json"
AB="agent-browser --headed --state $STATE_FILE"
# Navigate to X search
ENCODED=$(python3 -c "import urllib.parse; print(urllib.parse.quote('$QUERY'))")
$AB open "https://x.com/search?q=${ENCODED}&src=typed_query" 2>&1
sleep 3
# Switch to Latest tab if requested
if [[ "$TAB" == "latest" ]]; then
agent-browser click "text=Latest" 2>&1
sleep 2
fi
# Return interactive snapshot of main content
agent-browser snapshot -i -c --selector "main" 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment