mirror of
https://github.com/garrytan/gstack.git
synced 2026-05-02 03:35:09 +02:00
3f080de1b7
* feat: CDP inspector module — persistent sessions, CSS cascade, style modification New browse/src/cdp-inspector.ts with full CDP inspection engine: - inspectElement() via CSS.getMatchedStylesForNode + DOM.getBoxModel - modifyStyle() via CSS.setStyleTexts with headless page.evaluate fallback - Persistent CDP session lifecycle (create, reuse, detach on nav, re-create) - Specificity sorting, overridden property detection, UA rule filtering - Modification history with undo support - formatInspectorResult() for CLI output Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: browse server inspector endpoints + inspect/style/cleanup/prettyscreenshot CLI Server endpoints: POST /inspector/pick, GET /inspector, POST /inspector/apply, POST /inspector/reset, GET /inspector/history, GET /inspector/events (SSE). CLI commands: inspect (CDP cascade), style (live CSS mod), cleanup (page clutter removal), prettyscreenshot (clean screenshot pipeline). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: sidebar CSS inspector — element picker, box model, rule cascade, quick edit Extension changes for the visual CSS inspector: - inspector.js: element picker with hover highlight, CSS selector generation, basic mode fallback (getComputedStyle + CSSOM), page alteration handlers - inspector.css: picker overlay styles (blue highlight + tooltip) - background.js: inspector message routing (picker <-> server <-> sidepanel) - sidepanel: Inspector tab with box model viz (gstack palette), matched rules with specificity badges, computed styles, click-to-edit quick edit, Send to Agent/Code button, empty/loading/error states Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * docs: document inspect, style, cleanup, prettyscreenshot browse commands Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: auto-track user-created tabs and handle tab close browser-manager.ts changes: - context.on('page') listener: automatically tracks tabs opened by the user (Cmd+T, right-click open in new tab, window.open). Previously only programmatic newTab() was tracked, so user tabs were invisible. - page.on('close') handler in wirePageEvents: removes closed tabs from the pages map and switches activeTabId to the last remaining tab. - syncActiveTabByUrl: match Chrome extension's active tab URL to the correct Playwright page for accurate tab identity. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: per-tab agent isolation via BROWSE_TAB environment variable Prevents parallel sidebar agents from interfering with each other's tab context. Three-layer fix: - sidebar-agent.ts: passes BROWSE_TAB=<tabId> env var to each claude process, per-tab processing set allows concurrent agents across tabs - cli.ts: reads process.env.BROWSE_TAB and includes tabId in command request body - server.ts: handleCommand() temporarily switches activeTabId when tabId is present, restores after command completes (safe: Bun event loop is single-threaded) Also: per-tab agent state (TabAgentState map), per-tab message queuing, per-tab chat buffers, verbose streaming narration, stop button endpoint. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: sidebar per-tab chat context, tab bar sync, stop button, UX polish Extension changes: - sidepanel.js: per-tab chat history (tabChatHistories map), switchChatTab() swaps entire chat view, browserTabActivated handler for instant tab sync, stop button wired to /sidebar-agent/stop, pollTabs renders tab bar - sidepanel.html: updated banner text ("Browser co-pilot"), stop button markup, input placeholder "Ask about this page..." - sidepanel.css: tab bar styles, stop button styles, loading state fixes - background.js: chrome.tabs.onActivated sends browserTabActivated to sidepanel with tab URL for instant tab switch detection Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * test: per-tab isolation, BROWSE_TAB pinning, tab tracking, sidebar UX sidebar-agent.test.ts (new tests): - BROWSE_TAB env var passed to claude process - CLI reads BROWSE_TAB and sends tabId in body - handleCommand accepts tabId, saves/restores activeTabId - Tab pinning only activates when tabId provided - Per-tab agent state, queue, concurrency - processingTabs set for parallel agents sidebar-ux.test.ts (new tests): - context.on('page') tracks user-created tabs - page.on('close') removes tabs from pages map - Tab isolation uses BROWSE_TAB not system prompt hack - Per-tab chat context in sidepanel - Tab bar rendering, stop button, banner text Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: resolve merge conflicts — keep security defenses + per-tab isolation Merged main's security improvements (XML escaping, prompt injection defense, allowed commands whitelist, --model opus, Write tool, stderr capture) with our branch's per-tab isolation (BROWSE_TAB env var, processingTabs set, no --resume). Updated test expectations for expanded system prompt. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * chore: bump version and changelog (v0.13.9.0) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add inspector message types to background.js allowlist Pre-existing bug found by Codex: ALLOWED_TYPES in background.js was missing all inspector message types (startInspector, stopInspector, elementPicked, pickerCancelled, applyStyle, toggleClass, injectCSS, resetAll, inspectResult). Messages were silently rejected, making the inspector broken on ALL pages. Also: separate executeScript and insertCSS into individual try blocks in injectInspector(), store inspectorMode for routing, and add content.js fallback when script injection fails (CSP, chrome:// pages). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: basic element picker in content.js for CSP-restricted pages When inspector.js can't be injected (CSP, chrome:// pages), content.js provides a basic picker using getComputedStyle + CSSOM: - startBasicPicker/stopBasicPicker message handlers - captureBasicData() with ~30 key CSS properties, box model, matched rules - Hover highlight with outline save/restore (never leaves artifacts) - Click uses e.target directly (no re-querying by selector) - Sends inspectResult with mode:'basic' for sidebar rendering - Escape key cancels picker and restores outlines Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: cleanup + screenshot buttons in sidebar inspector toolbar Two action buttons in the inspector toolbar: - Cleanup (🧹): POSTs cleanup --all to server, shows spinner, chat notification on success, resets inspector state (element may be removed) - Screenshot (📸): POSTs screenshot to server, shows spinner, chat notification with saved file path Shared infrastructure: - .inspector-action-btn CSS with loading spinner via ::after pseudo-element - chat-notification type in addChatEntry() for system messages - package.json version bump to 0.13.9.0 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * test: inspector allowlist, CSP fallback, cleanup/screenshot buttons 16 new tests in sidebar-ux.test.ts: - Inspector message allowlist includes all inspector types - content.js basic picker (startBasicPicker, captureBasicData, CSSOM, outline save/restore, inspectResult with mode basic, Escape cleanup) - background.js CSP fallback (separate try blocks, inspectorMode, fallback) - Cleanup button (POST /command, inspector reset after success) - Screenshot button (POST /command, notification rendering) - Chat notification type and CSS styles Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * docs: update project documentation for v0.13.9.0 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: cleanup + screenshot buttons in chat toolbar (not just inspector) Quick actions toolbar (🧹 Cleanup, 📸 Screenshot) now appears above the chat input, always visible. Both inspector and chat buttons share runCleanup() and runScreenshot() helper functions. Clicking either set shows loading state on both simultaneously. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * test: chat toolbar buttons, shared helpers, quick-action-btn styles Tests that chat toolbar exists (chat-cleanup-btn, chat-screenshot-btn, quick-actions container), CSS styles (.quick-action-btn, .quick-action-btn.loading), shared runCleanup/runScreenshot helper functions, and cleanup inspector reset. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: aggressive cleanup heuristics — overlays, scroll unlock, blur removal Massively expanded CLEANUP_SELECTORS with patterns from uBlock Origin and Readability.js research: - ads: 30+ selectors (Google, Amazon, Outbrain, Taboola, Criteo, etc.) - cookies: OneTrust, Cookiebot, TrustArc, Quantcast + generic patterns - overlays (NEW): paywalls, newsletter popups, interstitials, push prompts, app download banners, survey modals - social: follow prompts, share tools - Cleanup now defaults to --all when no args (sidebar button fix) - Uses !important on all display:none (overrides inline styles) - Unlocks body/html scroll (overflow:hidden from modal lockout) - Removes blur/filter effects (paywall content blur) - Removes max-height truncation (article teaser truncation) - Collapses empty ad placeholder whitespace (empty divs after ad removal) - Skips gstack-ctrl indicator in sticky removal Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: disable action buttons when disconnected, no error spam - setActionButtonsEnabled() toggles .disabled class on all cleanup/screenshot buttons (both chat toolbar and inspector toolbar) - Called with false in updateConnection when server URL is null - Called with true when connection established - runCleanup/runScreenshot silently return when disconnected instead of showing 'Not connected' error notifications - CSS .disabled style: pointer-events:none, opacity:0.3, cursor:not-allowed Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * test: cleanup heuristics, button disabled state, overlay selectors 17 new tests: - cleanup defaults to --all on empty args - CLEANUP_SELECTORS overlays category (paywall, newsletter, interstitial) - Major ad networks in selectors (doubleclick, taboola, criteo, etc.) - Major consent frameworks (OneTrust, Cookiebot, TrustArc, Quantcast) - !important override for inline styles - Scroll unlock (body overflow:hidden) - Blur removal (paywall content blur) - Article truncation removal (max-height) - Empty placeholder collapse - gstack-ctrl indicator skip in sticky cleanup - setActionButtonsEnabled function - Buttons disabled when disconnected - No error spam from cleanup/screenshot when disconnected - CSS disabled styles for action buttons Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: LLM-based page cleanup — agent analyzes page semantically Instead of brittle CSS selectors, the cleanup button now sends a prompt to the sidebar agent (which IS an LLM). The agent: 1. Runs deterministic $B cleanup --all as a quick first pass 2. Takes a snapshot to see what's left 3. Analyzes the page semantically to identify remaining clutter 4. Removes elements intelligently, preserving site branding This means cleanup works correctly on any site without site-specific selectors. The LLM understands that "Your Daily Puzzles" is clutter, "ADVERTISEMENT" is junk, but the SF Chronicle masthead should stay. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: aggressive cleanup heuristics + preserve top nav bar Deterministic cleanup improvements (used as first pass before LLM analysis): - New 'clutter' category: audio players, podcast widgets, sidebar puzzles/games, recirculation widgets (taboola, outbrain, nativo), cross-promotion banners - Text-content detection: removes "ADVERTISEMENT", "Article continues below", "Sponsored", "Paid content" labels and their parent wrappers - Sticky fix: preserves the topmost full-width element near viewport top (site nav bar) instead of hiding all sticky/fixed elements. Sorts by vertical position, preserves the first one that spans >80% viewport width. Tests: clutter category, ad label removal, nav bar preservation logic. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * test: LLM-based cleanup architecture, deterministic heuristics, sticky nav 22 new tests covering: - Cleanup button uses /sidebar-command (agent) not /command (deterministic) - Cleanup prompt includes deterministic first pass + agent snapshot analysis - Cleanup prompt lists specific clutter categories for agent guidance - Cleanup prompt preserves site identity (masthead, headline, body, byline) - Cleanup prompt instructs scroll unlock and $B eval removal - Loading state management (async agent, setTimeout) - Deterministic clutter: audio/podcast, games/puzzles, recirculation - Ad label text patterns (ADVERTISEMENT, Sponsored, Article continues) - Ad label parent wrapper hiding for small containers - Sticky nav preservation (sort by position, first full-width near top) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: GStack Browser stealth + branding — anti-bot patches, custom UA, rebrand - Add GSTACK_CHROMIUM_PATH env var for custom Chromium binary - Add BROWSE_EXTENSIONS_DIR env var for extension path override - Move auth token to /health endpoint (fixes read-only .app bundles) - Anti-bot stealth: disable navigator.webdriver, fake plugins, languages - Custom user agent: Chrome/<version> GStackBrowser (auto-detects version) - Rebrand Chromium plist to "GStack Browser" at launch time - Update security test to match new token-via-health approach * feat: GStack Browser .app bundle — launcher script + build system - scripts/app/gstack-browser: dual-mode launcher (dev + .app bundle) - scripts/build-app.sh: compiles binary, bundles Chromium + extension, creates DMG - Rebrands Chromium plist during build for "GStack Browser" in menu bar - 389MB .app, 189MB compressed DMG, launches in ~5s * docs: GStack Browser V0 master plan — AI-native development browser vision 5-phase roadmap from .app wrapper through Chromium fork, 9 capability visions, competitive landscape, architecture diagrams, design system. * fix: restore package.json and sync version to 0.14.3.0 * chore: bump version and changelog (v0.14.4.0) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * chore: gitignore top-level dist/ (GStack Browser build output) * feat: GStack Browser icon — custom .icns replaces Chromium's Dock icon - Generated 1024px icon: dark terminal window with amber prompt cursor - Converted to .icns with all macOS sizes (16-1024px, 1x and 2x) - build-app.sh copies icon into both the outer .app and bundled Chromium's Resources (Chromium's process owns the Dock icon, not the launcher) - browser-manager.ts patches Chromium's icon at runtime for dev mode too - Both the Dock and Cmd+Tab now show the GStack icon * feat: rename /connect-chrome → /open-gstack-browser - Rename skill directory + update frontmatter name and description - Update SKILL.md.tmpl to reference GStack Browser branding/stealth - Create connect-chrome symlink for backwards compatibility - Setup script creates /connect-chrome alias in .claude/skills/ - Fix package.json version sync (0.14.5.0 → 0.14.6.0) * feat: rename /connect-chrome → /open-gstack-browser across all references Update README skill lists, docs/skills.md deep dive, extension sidepanel banner copy button, and reconnect clipboard text. * feat: left-align sidebar UI + extension-ready event for welcome page - Left-align all sidebar text (chat welcome, loading, empty states, notifications, inspector empty, session placeholder) - Dispatch 'gstack-extension-ready' CustomEvent from content.js so the welcome page can detect when the sidebar is active * chore: add GStack Browser TODOs — CDP stealth patches + Chromium fork P1: rebrowser-style postinstall patcher for Playwright 1.58.2 (suppress Runtime.enable, addBinding context discovery, 6 files, ~200 lines). P2: long-term Chromium fork for permanent stealth + native sidebar. * chore: regenerate open-gstack-browser/SKILL.md from template Fix timeline skill name (connect-chrome → open-gstack-browser) and preamble formatting from merge with main's updated template. * feat: welcome page served from browse server on headed launch - Add /welcome endpoint to server.ts, serves welcome.html - Navigate to /welcome after server starts (not during launchHeaded, which runs before the server is listening) - welcome.html bundled in browse/src/ for portability * feat: auto-open sidebar on every browser launch, not just first install - Add top-level setTimeout in background.js that fires on every service worker startup (onInstalled only fires on install/update) - Remove misaligned arrow from welcome page, replace with text fallback that hides when extension content script fires gstack-extension-ready * fix: sidebar auto-open retry with backoff + welcome page tests - Replace single-attempt sidePanel.open() with autoOpenSidePanel() that retries up to 5 times with 500ms-5000ms backoff - Fire on both onInstalled AND every service worker startup - Remove misaligned arrow from welcome page, replace with text fallback - Add 12 tests: welcome page structure, /welcome endpoint, headed launch navigation timing, sidebar auto-open retry logic, extension-ready event * feat: reload button in sidebar footer Adds a "reload" button next to "debug" and "clear" in the sidebar footer. Calls location.reload() to fully refresh the side panel, re-run connection logic, and clear stale state. * feat: right-pointing arrow hint for sidebar on welcome page Replace invisible text fallback with visible amber bubble + animated right arrow (→) pointing toward where the sidebar opens. Always correct regardless of window size (unlike the old up arrow at toolbar chrome). * fix: sidebar auth race — pass token in getPort response The sidebar called tryConnect() → getPort → got {port, connected} but NO token. All subsequent requests (SSE, chat poll) failed with 401. The token only arrived later via the health broadcast, but by then the SSE connection was already broken. Fix: include authToken in the getPort response so the sidebar has the token from its very first connection attempt. * feat: sidebar debug visibility + auth race tests - Show attempt count in loading screen ("Connecting... attempt 3") - After 5 failed attempts, show debug details (port, connected, token) so stuck users can see exactly what's failing - Add 4 tests: getPort includes token, tryConnect uses token, dead state exists with MAX_RECONNECT_ATTEMPTS, reconnectAttempts visible * fix: startup health check retries every 1s instead of 10s Root cause: extension service worker starts before Bun.serve() is listening. First checkHealth() fails, next attempt is 10 seconds later. User stares at "Connecting..." for 10 seconds. Fix: retry every 1s for up to 15 attempts on startup, then switch to 10s polling once connected (or after 15s gives up). Sidebar should connect within 1-2 seconds of server becoming available. 3 new tests verify the fast-retry → slow-poll transition. * feat: detailed step-by-step status in sidebar loading screen Replace useless "Connecting..." with real-time debug info: - "Looking for browse server... (attempt N)" - Shows port, server responding status, token status - Shows chrome.runtime errors if extension messaging fails - Tells user to run /open-gstack-browser if server not found * fix: sidebar connects directly to /health instead of waiting for background Root cause: sidepanel asked background "are you connected?" but background's health check hadn't succeeded yet (1-10s gap). Sidepanel waited forever. Fix: when background says not connected, sidepanel hits /health directly with fetch(). Gets the token from the response. Bypasses background entirely for initial connection. Shows step-by-step debug info: "Checking server directly... port: 34567 / Trying GET /health..." * fix: suppress fake "session ended" and timeout errors in sidebar Two issues making the sidebar look broken when it's actually working: 1. "Timed out after 300s" error displayed after agent_done — this is a cleanup timer, not a real error. Now suppressed when no active session. 2. "(session ended)" text appended on every idle poll — removed entirely. The thinking spinner is cleaned up silently instead. * fix: sidebar agent passes BROWSE_PORT to child claude Ensures the child claude process connects to the existing headed browse server (port 34567) instead of spawning a new headless one. Without this, sidebar chat commands run in an invisible browser. * feat: BROWSE_NO_AUTOSTART prevents sidebar from spawning headless browser When set, the browse CLI refuses to start a new server and exits with a clear error: "Server not available, run /open-gstack-browser to restart." The sidebar agent sets this so users never get an invisible headless browser when the headed one is closed. * test: BROWSE_NO_AUTOSTART guard in CLI + sidebar-agent env vars 5 tests: CLI checks env var before starting server, shows actionable error, sidebar-agent sets the flag + BROWSE_PORT, guard runs before lock acquisition to prevent stale lock files. * fix: stale auth token causes Unauthorized + invisible error text background.js checkHealth() never refreshed authToken from /health responses, so when the browse server restarted with a new token, all sidebar-command requests got 401 Unauthorized forever. Also: error placeholder text was #3f3f46 on #0C0C0C (nearly invisible). Now shows in red to match the error border. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: replace 40+ silent catch blocks with debug logging Every empty catch {} in sidepanel.js, sidebar-agent.ts now logs with [gstack sidebar] or [sidebar-agent] prefix. Chat poll 401s, stop agent, tab poll, clear chat, SSE parse, refs fetch, stream JSON parse, queue read/parse, process kill — all now visible in console. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: noisy debug logging + auto model routing in browse server Server-side silent catch blocks (22 instances) now log with [browse] prefix: chat persistence, session save/load, agent kill, tab pin/restore, welcome page, buffer flush, worktree cleanup, lock files, SSE streams. Also adds pickSidebarModel() — routes sidebar messages to sonnet for navigation/interaction (click, goto, fill, screenshot) and opus for analysis/comprehension (summarize, describe, find bugs). Sonnet is ~4x faster for action commands with zero quality difference. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * test: update sidebar tests for model router + longer stopAgent slice - stopAgent slice 800→1000 to accommodate added error logging lines - Replace hardcoded opus assertion with model router assertions Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: sidebar arrow hint stays visible until sidebar actually opens Previously the welcome page arrow hid immediately when the extension's content script loaded — but extension loaded ≠ sidebar open. Now the signal flow is: sidepanel connects → tells background.js → relays to content script → dispatches gstack-extension-ready → arrow hides. Adds welcome-page.test.ts: 14 tests verifying arrow, branding, feature cards, dark theme, and auto-hide behavior via real HTTP server. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * test: arrow hide signal chain (4-step) + stale session-ended assertion 8 new tests verify the sidebarOpened → background → content → welcome signal chain. Updates stale "(session ended)" test that checked for text removed in a prior commit. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: preserve optimistic UI during tab switch on first message When the user sends a message and the server assigns it to a new tab (because Chrome's active tab changed), switchChatTab() was blowing away the optimistic user bubble and thinking dots with a welcome screen. Now preserves the current DOM if we're mid-send with a thinking indicator. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * docs: sidebar message flow architecture doc + CLAUDE.md pointer SIDEBAR_MESSAGE_FLOW.md documents the full init timeline, message flow (user types → claude responds), auth token chain, arrow hint signal chain, model routing, tab concurrency, and known failure modes. CLAUDE.md now tells you to read it before touching sidebar files. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: sidebar chat resets idle timer + shutdown kills sidebar-agent Two fixes for the "browser died while chatting" problem: 1. /sidebar-command now calls resetIdleTimer(). Previously only CLI commands reset it, so the server would shut down after 30 min even while the user was actively chatting in the sidebar. 2. shutdown() now pkills the sidebar-agent daemon. Previously the agent survived server shutdown, kept polling a dead server, and spawned confused claude processes that auto-started headless browsers. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: disable idle timeout in headed mode — browser lives until closed The 30-minute idle timeout only applies to headless mode now. In headed mode the user is looking at the Chrome window, so auto-shutdown is wrong. The browser stays alive until explicit disconnect or window close. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: cookies button in sidebar footer opens cookie picker One-click cookie import from the sidebar. Navigates the headed browser to /cookie-picker where you can select which domains to import from your real Chrome profile. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * docs: update project documentation for GStack Browser improvements README.md: updated Real browser mode and sidebar agent sections with model routing, cookie import button, no idle timeout in headed mode. Updated skill table entries for /browse and /open-gstack-browser. docs/skills.md: updated /open-gstack-browser deep dive with model routing and cookie import details. GSTACK_BROWSER_V0.md: added 6 new SHIPPED items to implementation status table (model routing, debug logging, idle timeout, cookie button, arrow hint, architecture doc). TODOS.md: marked "Sidebar agent Write tool + error visibility" as SHIPPED. Added new P2 TODO for direct API calls to eliminate claude -p startup tax. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add Claude Code terminal example to welcome page TRY IT NOW Fifth example shows the parent agent workflow: navigate, extract CSS, write to file. The other four are all sidebar-only. This one shows co-presence — the Claude Code session that launched the browser can also control it directly. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: hide internal tool-result file reads from sidebar activity Claude reads its own ~/.claude/projects/.../tool-results/ files as internal plumbing. These showed up as long unreadable paths in the sidebar. Now: describeToolCall returns empty for tool-result reads, and the sidebar skips rendering tool_use entries with no description. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: collapse tool calls into "See reasoning" disclosure on completion While the agent is working, tool calls stream live so you can watch progress. When the agent finishes, all tool calls collapse into a "See reasoning (N steps)" disclosure. Click to expand and see what the agent did. The final text answer stays visible. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * test: 17 new tests for recent sidebar fixes Covers: tool-result file filtering, empty tool_use skip, reasoning disclosure collapse, idle timeout headed mode bypass, sidebar-command idle reset, shutdown sidebar-agent kill, cookie button, and model routing analysis-before-action priority. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: move cookies button to quick actions toolbar Cookies now sits next to Cleanup and Screenshot as a primary action button (🍪 Cookies) instead of buried in the footer. Same behavior, more discoverable. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add instructional text to cookie picker page "Select the domains of cookies you want to import to GStack Browser. You'll be able to browse those sites with the same login as your other browser." Also fixes stale test that expected hardcoded '--model', 'opus'. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: 6-card welcome page with cookie import + dual-agent cards 3x2 grid layout (was 2x2). New cards: "Import your cookies" (click 🍪 Cookies to import login sessions from Chrome/Arc/Brave) and "Or use your main agent" (your Claude Code terminal also controls this browser). Responsive: 3 cols > 2 cols > 1 col. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: move sidebar arrow hint to top-right instead of vertically centered The arrow was centered vertically which put it behind the feature cards. Now positioned at top: 80px where there's open space and it's more visible. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1653 lines
61 KiB
JavaScript
1653 lines
61 KiB
JavaScript
/**
|
|
* gstack browse — Side Panel
|
|
*
|
|
* Chat tab: two-way messaging with Claude Code via file queue.
|
|
* Debug tabs: activity feed (SSE) + refs (REST).
|
|
* Polls /sidebar-chat for new messages every 1s.
|
|
*/
|
|
|
|
const NAV_COMMANDS = new Set(['goto', 'back', 'forward', 'reload']);
|
|
const INTERACTION_COMMANDS = new Set(['click', 'fill', 'select', 'hover', 'type', 'press', 'scroll', 'wait', 'upload']);
|
|
const OBSERVE_COMMANDS = new Set(['snapshot', 'screenshot', 'diff', 'console', 'network', 'text', 'html', 'links', 'forms', 'accessibility', 'cookies', 'storage', 'perf']);
|
|
|
|
let lastId = 0;
|
|
let eventSource = null;
|
|
let serverUrl = null;
|
|
let serverToken = null;
|
|
let chatLineCount = 0;
|
|
let chatPollInterval = null;
|
|
let connState = 'disconnected'; // disconnected | connected | reconnecting | dead
|
|
let lastOptimisticMsg = null; // track optimistically rendered user msg to avoid dupes
|
|
let sidebarActiveTabId = null; // which browser tab's chat we're showing
|
|
const chatLineCountByTab = {}; // tabId -> last seen chatLineCount
|
|
const chatDomByTab = {}; // tabId -> saved innerHTML
|
|
let reconnectAttempts = 0;
|
|
let reconnectTimer = null;
|
|
const MAX_RECONNECT_ATTEMPTS = 30; // 30 * 2s = 60s before showing "dead"
|
|
|
|
// Auth headers for sidebar endpoints
|
|
function authHeaders() {
|
|
const h = { 'Content-Type': 'application/json' };
|
|
if (serverToken) h['Authorization'] = `Bearer ${serverToken}`;
|
|
return h;
|
|
}
|
|
|
|
// ─── Connection State Machine ─────────────────────────────────────
|
|
|
|
function setConnState(state) {
|
|
const prev = connState;
|
|
connState = state;
|
|
const banner = document.getElementById('conn-banner');
|
|
const bannerText = document.getElementById('conn-banner-text');
|
|
const bannerActions = document.getElementById('conn-banner-actions');
|
|
|
|
if (state === 'connected') {
|
|
if (prev === 'reconnecting' || prev === 'dead') {
|
|
// Show "reconnected" toast that fades
|
|
banner.style.display = '';
|
|
banner.className = 'conn-banner reconnected';
|
|
bannerText.textContent = 'Reconnected';
|
|
bannerActions.style.display = 'none';
|
|
setTimeout(() => { banner.style.display = 'none'; }, 5000);
|
|
} else {
|
|
banner.style.display = 'none';
|
|
}
|
|
reconnectAttempts = 0;
|
|
if (reconnectTimer) { clearInterval(reconnectTimer); reconnectTimer = null; }
|
|
} else if (state === 'reconnecting') {
|
|
banner.style.display = '';
|
|
banner.className = 'conn-banner reconnecting';
|
|
bannerText.textContent = `Reconnecting... (${reconnectAttempts}/${MAX_RECONNECT_ATTEMPTS})`;
|
|
bannerActions.style.display = 'none';
|
|
} else if (state === 'dead') {
|
|
banner.style.display = '';
|
|
banner.className = 'conn-banner dead';
|
|
bannerText.textContent = 'Server offline';
|
|
bannerActions.style.display = '';
|
|
if (reconnectTimer) { clearInterval(reconnectTimer); reconnectTimer = null; }
|
|
} else {
|
|
banner.style.display = 'none';
|
|
}
|
|
}
|
|
|
|
function startReconnect() {
|
|
if (reconnectTimer) return;
|
|
setConnState('reconnecting');
|
|
reconnectTimer = setInterval(() => {
|
|
reconnectAttempts++;
|
|
if (reconnectAttempts > MAX_RECONNECT_ATTEMPTS) {
|
|
setConnState('dead');
|
|
return;
|
|
}
|
|
setConnState('reconnecting');
|
|
tryConnect();
|
|
}, 2000);
|
|
}
|
|
|
|
// ─── Chat ───────────────────────────────────────────────────────
|
|
|
|
const chatMessages = document.getElementById('chat-messages');
|
|
const commandInput = document.getElementById('command-input');
|
|
const sendBtn = document.getElementById('send-btn');
|
|
const commandHistory = [];
|
|
let historyIndex = -1;
|
|
|
|
function formatChatTime(ts) {
|
|
const d = new Date(ts);
|
|
return d.toLocaleTimeString('en-US', { hour12: false, hour: '2-digit', minute: '2-digit' });
|
|
}
|
|
|
|
// Current streaming state
|
|
let agentContainer = null; // The container for the current agent response
|
|
let agentTextEl = null; // The text accumulator element
|
|
let agentText = ''; // Accumulated text
|
|
|
|
// Dedup: track which entry IDs have already been rendered to prevent
|
|
// repeat rendering on reconnect or tab switch (server replays from disk)
|
|
const renderedEntryIds = new Set();
|
|
|
|
function addChatEntry(entry) {
|
|
// Dedup by entry ID — prevent repeat rendering on reconnect/replay
|
|
if (entry.id !== undefined) {
|
|
if (renderedEntryIds.has(entry.id)) return;
|
|
renderedEntryIds.add(entry.id);
|
|
}
|
|
|
|
// Remove welcome message on first real message
|
|
const welcome = chatMessages.querySelector('.chat-welcome');
|
|
if (welcome) welcome.remove();
|
|
|
|
// User messages → chat bubble (skip if we already rendered it optimistically)
|
|
if (entry.role === 'user') {
|
|
if (lastOptimisticMsg === entry.message) {
|
|
lastOptimisticMsg = null; // consumed — don't skip next identical msg
|
|
return;
|
|
}
|
|
const bubble = document.createElement('div');
|
|
bubble.className = 'chat-bubble user';
|
|
bubble.innerHTML = `${escapeHtml(entry.message)}<span class="chat-time">${formatChatTime(entry.ts)}</span>`;
|
|
chatMessages.appendChild(bubble);
|
|
bubble.scrollIntoView({ behavior: 'smooth', block: 'end' });
|
|
return;
|
|
}
|
|
|
|
// Legacy assistant messages (from /sidebar-response)
|
|
if (entry.role === 'assistant') {
|
|
const bubble = document.createElement('div');
|
|
bubble.className = 'chat-bubble assistant';
|
|
let content = escapeHtml(entry.message);
|
|
content = content.replace(/```([\s\S]*?)```/g, '<pre>$1</pre>');
|
|
content = content.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
|
|
content = content.replace(/\n/g, '<br>');
|
|
bubble.innerHTML = `${content}<span class="chat-time">${formatChatTime(entry.ts)}</span>`;
|
|
chatMessages.appendChild(bubble);
|
|
bubble.scrollIntoView({ behavior: 'smooth', block: 'end' });
|
|
return;
|
|
}
|
|
|
|
// System notifications (cleanup, screenshot, errors)
|
|
if (entry.type === 'notification') {
|
|
const note = document.createElement('div');
|
|
note.className = 'chat-notification';
|
|
note.textContent = entry.message;
|
|
chatMessages.appendChild(note);
|
|
note.scrollIntoView({ behavior: 'smooth', block: 'end' });
|
|
return;
|
|
}
|
|
|
|
// Agent streaming events
|
|
if (entry.role === 'agent') {
|
|
handleAgentEvent(entry);
|
|
return;
|
|
}
|
|
}
|
|
|
|
function handleAgentEvent(entry) {
|
|
if (entry.type === 'agent_start') {
|
|
// If we already showed thinking dots optimistically in sendMessage(),
|
|
// don't duplicate. Just ensure fast polling is on.
|
|
if (agentContainer && document.getElementById('agent-thinking')) {
|
|
startFastPoll();
|
|
updateStopButton(true);
|
|
return;
|
|
}
|
|
// Create a new agent response container
|
|
agentText = '';
|
|
agentContainer = document.createElement('div');
|
|
agentContainer.className = 'agent-response';
|
|
agentTextEl = null;
|
|
chatMessages.appendChild(agentContainer);
|
|
|
|
// Add thinking indicator
|
|
const thinking = document.createElement('div');
|
|
thinking.className = 'agent-thinking';
|
|
thinking.id = 'agent-thinking';
|
|
thinking.innerHTML = '<span class="thinking-dot"></span><span class="thinking-dot"></span><span class="thinking-dot"></span>';
|
|
agentContainer.appendChild(thinking);
|
|
agentContainer.scrollIntoView({ behavior: 'smooth', block: 'end' });
|
|
startFastPoll();
|
|
updateStopButton(true);
|
|
return;
|
|
}
|
|
|
|
if (entry.type === 'agent_done') {
|
|
// Remove thinking indicator
|
|
const thinking = document.getElementById('agent-thinking');
|
|
if (thinking) thinking.remove();
|
|
updateStopButton(false);
|
|
stopFastPoll();
|
|
// Collapse tool calls into a "See reasoning" disclosure
|
|
if (agentContainer) {
|
|
const tools = agentContainer.querySelectorAll('.agent-tool');
|
|
if (tools.length > 0) {
|
|
const details = document.createElement('details');
|
|
details.className = 'agent-reasoning';
|
|
const summary = document.createElement('summary');
|
|
summary.textContent = `See reasoning (${tools.length} step${tools.length > 1 ? 's' : ''})`;
|
|
details.appendChild(summary);
|
|
for (const tool of tools) {
|
|
details.appendChild(tool);
|
|
}
|
|
// Insert the disclosure before the text response (if any)
|
|
const textEl = agentContainer.querySelector('.agent-text');
|
|
if (textEl) {
|
|
agentContainer.insertBefore(details, textEl);
|
|
} else {
|
|
agentContainer.appendChild(details);
|
|
}
|
|
}
|
|
// Add timestamp
|
|
const ts = document.createElement('span');
|
|
ts.className = 'chat-time';
|
|
ts.textContent = formatChatTime(entry.ts);
|
|
agentContainer.appendChild(ts);
|
|
}
|
|
agentContainer = null;
|
|
agentTextEl = null;
|
|
return;
|
|
}
|
|
|
|
if (entry.type === 'agent_error') {
|
|
// Suppress timeout errors that fire after agent_done (cleanup noise)
|
|
if (entry.error && entry.error.includes('Timed out') && !agentContainer) {
|
|
return;
|
|
}
|
|
const thinking = document.getElementById('agent-thinking');
|
|
if (thinking) thinking.remove();
|
|
updateStopButton(false);
|
|
stopFastPoll();
|
|
if (!agentContainer) {
|
|
agentContainer = document.createElement('div');
|
|
agentContainer.className = 'agent-response';
|
|
chatMessages.appendChild(agentContainer);
|
|
}
|
|
const err = document.createElement('div');
|
|
err.className = 'agent-error';
|
|
err.textContent = entry.error || 'Unknown error';
|
|
agentContainer.appendChild(err);
|
|
agentContainer = null;
|
|
return;
|
|
}
|
|
|
|
if (!agentContainer) {
|
|
agentContainer = document.createElement('div');
|
|
agentContainer.className = 'agent-response';
|
|
chatMessages.appendChild(agentContainer);
|
|
}
|
|
|
|
// Remove thinking indicator on first real content
|
|
const thinking = document.getElementById('agent-thinking');
|
|
if (thinking) thinking.remove();
|
|
|
|
if (entry.type === 'tool_use') {
|
|
const toolName = entry.tool || 'Tool';
|
|
const toolInput = entry.input || '';
|
|
|
|
// Skip tool uses with no description (e.g. internal tool-result file reads)
|
|
if (!toolInput) return;
|
|
|
|
const toolEl = document.createElement('div');
|
|
toolEl.className = 'agent-tool';
|
|
|
|
// Use the verbose description as the primary text
|
|
// The tool name becomes a subtle badge
|
|
const toolIcon = toolName === 'Bash' ? '▸' : toolName === 'Read' ? '📄' : toolName === 'Grep' ? '🔍' : toolName === 'Glob' ? '📁' : '⚡';
|
|
toolEl.innerHTML = `<span class="tool-icon">${toolIcon}</span> <span class="tool-description">${escapeHtml(toolInput)}</span>`;
|
|
agentContainer.appendChild(toolEl);
|
|
agentContainer.scrollIntoView({ behavior: 'smooth', block: 'end' });
|
|
return;
|
|
}
|
|
|
|
if (entry.type === 'text' || entry.type === 'result') {
|
|
// Full text replacement
|
|
agentText = entry.text || '';
|
|
if (!agentTextEl) {
|
|
agentTextEl = document.createElement('div');
|
|
agentTextEl.className = 'agent-text';
|
|
agentContainer.appendChild(agentTextEl);
|
|
}
|
|
let content = escapeHtml(agentText);
|
|
content = content.replace(/```([\s\S]*?)```/g, '<pre>$1</pre>');
|
|
content = content.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
|
|
content = content.replace(/\n/g, '<br>');
|
|
agentTextEl.innerHTML = content;
|
|
agentContainer.scrollIntoView({ behavior: 'smooth', block: 'end' });
|
|
return;
|
|
}
|
|
|
|
if (entry.type === 'text_delta') {
|
|
// Incremental text append
|
|
agentText += entry.text || '';
|
|
if (!agentTextEl) {
|
|
agentTextEl = document.createElement('div');
|
|
agentTextEl.className = 'agent-text';
|
|
agentContainer.appendChild(agentTextEl);
|
|
}
|
|
let content = escapeHtml(agentText);
|
|
content = content.replace(/```([\s\S]*?)```/g, '<pre>$1</pre>');
|
|
content = content.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
|
|
content = content.replace(/\n/g, '<br>');
|
|
agentTextEl.innerHTML = content;
|
|
agentContainer.scrollIntoView({ behavior: 'smooth', block: 'end' });
|
|
return;
|
|
}
|
|
}
|
|
|
|
async function sendMessage() {
|
|
const msg = commandInput.value.trim();
|
|
if (!msg) return;
|
|
|
|
commandHistory.push(msg);
|
|
historyIndex = commandHistory.length;
|
|
commandInput.value = '';
|
|
commandInput.disabled = true;
|
|
sendBtn.disabled = true;
|
|
|
|
// Show user bubble + thinking dots IMMEDIATELY — don't wait for poll.
|
|
// This eliminates up to 1000ms of perceived latency.
|
|
lastOptimisticMsg = msg;
|
|
const welcome = chatMessages.querySelector('.chat-welcome');
|
|
if (welcome) welcome.remove();
|
|
const userBubble = document.createElement('div');
|
|
userBubble.className = 'chat-bubble user';
|
|
userBubble.innerHTML = `${escapeHtml(msg)}<span class="chat-time">${formatChatTime(new Date().toISOString())}</span>`;
|
|
chatMessages.appendChild(userBubble);
|
|
|
|
agentText = '';
|
|
agentContainer = document.createElement('div');
|
|
agentContainer.className = 'agent-response';
|
|
agentTextEl = null;
|
|
chatMessages.appendChild(agentContainer);
|
|
const thinking = document.createElement('div');
|
|
thinking.className = 'agent-thinking';
|
|
thinking.id = 'agent-thinking';
|
|
thinking.innerHTML = '<span class="thinking-dot"></span><span class="thinking-dot"></span><span class="thinking-dot"></span>';
|
|
agentContainer.appendChild(thinking);
|
|
agentContainer.scrollIntoView({ behavior: 'smooth', block: 'end' });
|
|
updateStopButton(true);
|
|
|
|
// Speed up polling while agent is working
|
|
startFastPoll();
|
|
|
|
const result = await new Promise((resolve) => {
|
|
chrome.runtime.sendMessage({ type: 'sidebar-command', message: msg, tabId: sidebarActiveTabId }, resolve);
|
|
});
|
|
|
|
commandInput.disabled = false;
|
|
sendBtn.disabled = false;
|
|
commandInput.focus();
|
|
|
|
if (result?.ok) {
|
|
// Poll immediately to sync server state
|
|
pollChat();
|
|
} else {
|
|
commandInput.classList.add('error');
|
|
commandInput.placeholder = result?.error || 'Failed to send';
|
|
setTimeout(() => {
|
|
commandInput.classList.remove('error');
|
|
commandInput.placeholder = 'Message Claude Code...';
|
|
}, 2000);
|
|
}
|
|
}
|
|
|
|
commandInput.addEventListener('keydown', (e) => {
|
|
if (e.key === 'Enter') { e.preventDefault(); sendMessage(); }
|
|
if (e.key === 'ArrowUp') {
|
|
e.preventDefault();
|
|
if (historyIndex > 0) { historyIndex--; commandInput.value = commandHistory[historyIndex]; }
|
|
}
|
|
if (e.key === 'ArrowDown') {
|
|
e.preventDefault();
|
|
if (historyIndex < commandHistory.length - 1) { historyIndex++; commandInput.value = commandHistory[historyIndex]; }
|
|
else { historyIndex = commandHistory.length; commandInput.value = ''; }
|
|
}
|
|
});
|
|
|
|
sendBtn.addEventListener('click', sendMessage);
|
|
document.getElementById('stop-agent-btn').addEventListener('click', stopAgent);
|
|
|
|
// Poll for new chat messages
|
|
let initialLoadDone = false;
|
|
|
|
async function pollChat() {
|
|
if (!serverUrl || !serverToken) return;
|
|
try {
|
|
// Request chat for the currently displayed tab
|
|
const tabParam = sidebarActiveTabId !== null ? `&tabId=${sidebarActiveTabId}` : '';
|
|
const resp = await fetch(`${serverUrl}/sidebar-chat?after=${chatLineCount}${tabParam}`, {
|
|
headers: authHeaders(),
|
|
signal: AbortSignal.timeout(3000),
|
|
});
|
|
if (!resp.ok) {
|
|
console.warn(`[gstack sidebar] Chat poll failed: ${resp.status} ${resp.statusText}`);
|
|
return;
|
|
}
|
|
const data = await resp.json();
|
|
|
|
// Detect tab switch from server — swap chat context.
|
|
// IMPORTANT: return before cleaning up thinking dots — the agent may be
|
|
// processing on the NEW tab while the OLD tab is idle. Removing the
|
|
// thinking indicator here would kill the optimistic UI before the switch.
|
|
if (data.activeTabId !== undefined && data.activeTabId !== sidebarActiveTabId) {
|
|
switchChatTab(data.activeTabId);
|
|
return; // switchChatTab triggers a fresh poll on the correct tab
|
|
}
|
|
|
|
// First successful poll — hide loading spinner
|
|
if (!initialLoadDone) {
|
|
initialLoadDone = true;
|
|
sidebarActiveTabId = data.activeTabId ?? null;
|
|
const loading = document.getElementById('chat-loading');
|
|
const welcome = document.getElementById('chat-welcome');
|
|
if (loading) loading.style.display = 'none';
|
|
// Show welcome only if no chat history
|
|
if (data.total === 0 && welcome) welcome.style.display = '';
|
|
}
|
|
|
|
if (data.entries && data.entries.length > 0) {
|
|
// Hide welcome on first real entry
|
|
const welcome = document.getElementById('chat-welcome');
|
|
if (welcome) welcome.style.display = 'none';
|
|
for (const entry of data.entries) {
|
|
addChatEntry(entry);
|
|
}
|
|
chatLineCount = data.total;
|
|
}
|
|
|
|
// Clean up orphaned thinking indicators after replay.
|
|
// Only remove if we're on the CORRECT tab and the agent is truly idle.
|
|
// Don't clean up during tab switches — the agent may be processing on
|
|
// the new tab while the old tab shows idle.
|
|
const thinking = document.getElementById('agent-thinking');
|
|
if (thinking && data.agentStatus !== 'processing') {
|
|
thinking.remove();
|
|
agentContainer = null;
|
|
agentTextEl = null;
|
|
}
|
|
|
|
// Show/hide stop button based on agent status
|
|
updateStopButton(data.agentStatus === 'processing');
|
|
} catch (err) {
|
|
console.error('[gstack sidebar] Chat poll error:', err.message);
|
|
}
|
|
}
|
|
|
|
/** Switch the sidebar to show a different tab's chat context */
|
|
function switchChatTab(newTabId) {
|
|
if (newTabId === sidebarActiveTabId) return;
|
|
|
|
// Save current tab's chat DOM + scroll position
|
|
if (sidebarActiveTabId !== null) {
|
|
chatDomByTab[sidebarActiveTabId] = chatMessages.innerHTML;
|
|
chatLineCountByTab[sidebarActiveTabId] = chatLineCount;
|
|
}
|
|
|
|
sidebarActiveTabId = newTabId;
|
|
|
|
// Restore saved chat for new tab, or carry over current DOM if we're
|
|
// mid-message (the server may have switched tabs because the user's
|
|
// Chrome tab changed, but we still want to show the optimistic UI).
|
|
if (chatDomByTab[newTabId]) {
|
|
chatMessages.innerHTML = chatDomByTab[newTabId];
|
|
chatLineCount = chatLineCountByTab[newTabId] || 0;
|
|
// Reset agent state for restored tab
|
|
agentContainer = null;
|
|
agentTextEl = null;
|
|
agentText = '';
|
|
} else if (lastOptimisticMsg && document.getElementById('agent-thinking')) {
|
|
// We're mid-send with optimistic UI — keep it, don't blow it away.
|
|
// The poll for the new tab will pick up the entries and sync naturally.
|
|
chatLineCount = 0;
|
|
// agentContainer/agentTextEl are already set from sendMessage()
|
|
} else {
|
|
chatMessages.innerHTML = `
|
|
<div class="chat-welcome" id="chat-welcome">
|
|
<div class="chat-welcome-icon">G</div>
|
|
<p>Send a message about this page.</p>
|
|
<p class="muted">Each tab has its own conversation.</p>
|
|
</div>`;
|
|
chatLineCount = 0;
|
|
// Reset agent state for fresh tab
|
|
agentContainer = null;
|
|
agentTextEl = null;
|
|
agentText = '';
|
|
}
|
|
|
|
// Immediately poll the new tab's chat
|
|
pollChat();
|
|
}
|
|
|
|
function updateStopButton(agentRunning) {
|
|
const stopBtn = document.getElementById('stop-agent-btn');
|
|
if (!stopBtn) return;
|
|
stopBtn.style.display = agentRunning ? '' : 'none';
|
|
}
|
|
|
|
async function stopAgent() {
|
|
if (!serverUrl) return;
|
|
try {
|
|
const resp = await fetch(`${serverUrl}/sidebar-agent/stop`, { method: 'POST', headers: authHeaders() });
|
|
if (!resp.ok) console.warn(`[gstack sidebar] Stop agent failed: ${resp.status}`);
|
|
} catch (err) {
|
|
console.error('[gstack sidebar] Stop agent error:', err.message);
|
|
}
|
|
// Immediately clean up UI
|
|
const thinking = document.getElementById('agent-thinking');
|
|
if (thinking) thinking.remove();
|
|
if (agentContainer) {
|
|
const notice = document.createElement('div');
|
|
notice.className = 'agent-text';
|
|
notice.style.color = 'var(--text-meta)';
|
|
notice.style.fontStyle = 'italic';
|
|
notice.textContent = 'Stopped';
|
|
agentContainer.appendChild(notice);
|
|
agentContainer = null;
|
|
agentTextEl = null;
|
|
}
|
|
updateStopButton(false);
|
|
stopFastPoll();
|
|
}
|
|
|
|
// ─── Adaptive poll speed ─────────────────────────────────────────
|
|
// 300ms while agent is working (fast first-token), 1000ms when idle.
|
|
const FAST_POLL_MS = 300;
|
|
const SLOW_POLL_MS = 1000;
|
|
|
|
function startFastPoll() {
|
|
if (chatPollInterval) clearInterval(chatPollInterval);
|
|
chatPollInterval = setInterval(pollChat, FAST_POLL_MS);
|
|
}
|
|
|
|
function stopFastPoll() {
|
|
if (chatPollInterval) clearInterval(chatPollInterval);
|
|
chatPollInterval = setInterval(pollChat, SLOW_POLL_MS);
|
|
}
|
|
|
|
// ─── Browser Tab Bar ─────────────────────────────────────────────
|
|
let tabPollInterval = null;
|
|
let lastTabJson = '';
|
|
|
|
async function pollTabs() {
|
|
if (!serverUrl || !serverToken) return;
|
|
try {
|
|
// Tell the server which Chrome tab the user is actually looking at.
|
|
// This syncs manual tab switches in the browser → server activeTabId.
|
|
let activeTabUrl = null;
|
|
try {
|
|
const chromeTabs = await chrome.tabs.query({ active: true, currentWindow: true });
|
|
activeTabUrl = chromeTabs?.[0]?.url || null;
|
|
} catch (err) {
|
|
console.debug('[gstack sidebar] Failed to get active tab URL:', err.message);
|
|
}
|
|
|
|
const resp = await fetch(`${serverUrl}/sidebar-tabs${activeTabUrl ? '?activeUrl=' + encodeURIComponent(activeTabUrl) : ''}`, {
|
|
headers: authHeaders(),
|
|
signal: AbortSignal.timeout(2000),
|
|
});
|
|
if (!resp.ok) {
|
|
console.warn(`[gstack sidebar] Tab poll failed: ${resp.status} ${resp.statusText}`);
|
|
return;
|
|
}
|
|
const data = await resp.json();
|
|
if (!data.tabs) return;
|
|
|
|
// Only re-render if tabs changed
|
|
const json = JSON.stringify(data.tabs);
|
|
if (json === lastTabJson) return;
|
|
lastTabJson = json;
|
|
|
|
renderTabBar(data.tabs);
|
|
} catch (err) {
|
|
console.error('[gstack sidebar] Tab poll error:', err.message);
|
|
}
|
|
}
|
|
|
|
function renderTabBar(tabs) {
|
|
const bar = document.getElementById('browser-tabs');
|
|
if (!bar) return;
|
|
|
|
if (!tabs || tabs.length <= 1) {
|
|
bar.style.display = 'none';
|
|
return;
|
|
}
|
|
|
|
bar.style.display = '';
|
|
bar.innerHTML = '';
|
|
|
|
for (const tab of tabs) {
|
|
const el = document.createElement('div');
|
|
el.className = 'browser-tab' + (tab.active ? ' active' : '');
|
|
el.title = tab.url || '';
|
|
|
|
// Show favicon-style domain + title
|
|
let label = tab.title || '';
|
|
if (!label && tab.url) {
|
|
try { label = new URL(tab.url).hostname; } catch { label = tab.url; }
|
|
}
|
|
if (label.length > 20) label = label.slice(0, 20) + '…';
|
|
|
|
el.textContent = label || `Tab ${tab.id}`;
|
|
el.dataset.tabId = tab.id;
|
|
|
|
el.addEventListener('click', () => switchBrowserTab(tab.id));
|
|
bar.appendChild(el);
|
|
}
|
|
}
|
|
|
|
async function switchBrowserTab(tabId) {
|
|
if (!serverUrl) return;
|
|
try {
|
|
await fetch(`${serverUrl}/sidebar-tabs/switch`, {
|
|
method: 'POST',
|
|
headers: authHeaders(),
|
|
body: JSON.stringify({ id: tabId }),
|
|
});
|
|
// Switch chat context + re-poll tabs
|
|
switchChatTab(tabId);
|
|
pollTabs();
|
|
} catch (err) {
|
|
console.error('[gstack sidebar] Failed to switch browser tab:', err.message);
|
|
}
|
|
}
|
|
|
|
// ─── Clear Chat ─────────────────────────────────────────────────
|
|
|
|
document.getElementById('clear-chat').addEventListener('click', async () => {
|
|
if (!serverUrl) return;
|
|
try {
|
|
const resp = await fetch(`${serverUrl}/sidebar-chat/clear`, { method: 'POST', headers: authHeaders() });
|
|
if (!resp.ok) console.warn(`[gstack sidebar] Clear chat failed: ${resp.status}`);
|
|
} catch (err) {
|
|
console.error('[gstack sidebar] Clear chat error:', err.message);
|
|
}
|
|
// Reset local state
|
|
chatLineCount = 0;
|
|
renderedEntryIds.clear();
|
|
agentContainer = null;
|
|
agentTextEl = null;
|
|
agentText = '';
|
|
chatMessages.innerHTML = `
|
|
<div class="chat-welcome" id="chat-welcome">
|
|
<div class="chat-welcome-icon">G</div>
|
|
<p>Send a message to Claude Code.</p>
|
|
<p class="muted">Your agent will see it and act on it.</p>
|
|
</div>`;
|
|
});
|
|
|
|
// ─── Reload Sidebar ─────────────────────────────────────────────
|
|
document.getElementById('reload-sidebar').addEventListener('click', () => {
|
|
location.reload();
|
|
});
|
|
|
|
// ─── Copy Cookies ───────────────────────────────────────────────
|
|
document.getElementById('chat-cookies-btn').addEventListener('click', async () => {
|
|
if (!serverUrl) return;
|
|
// Navigate the browser to the cookie picker page hosted by the browse server
|
|
try {
|
|
await fetch(`${serverUrl}/command`, {
|
|
method: 'POST',
|
|
headers: authHeaders(),
|
|
body: JSON.stringify({ command: 'goto', args: [`${serverUrl}/cookie-picker`] }),
|
|
});
|
|
} catch (err) {
|
|
console.error('[gstack sidebar] Failed to open cookie picker:', err.message);
|
|
}
|
|
});
|
|
|
|
// ─── Debug Tabs ─────────────────────────────────────────────────
|
|
|
|
const debugToggle = document.getElementById('debug-toggle');
|
|
const debugTabs = document.getElementById('debug-tabs');
|
|
const closeDebug = document.getElementById('close-debug');
|
|
let debugOpen = false;
|
|
|
|
debugToggle.addEventListener('click', () => {
|
|
debugOpen = !debugOpen;
|
|
debugToggle.classList.toggle('active', debugOpen);
|
|
debugTabs.style.display = debugOpen ? 'flex' : 'none';
|
|
if (!debugOpen) {
|
|
// Close debug panels, show chat
|
|
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
|
|
document.getElementById('tab-chat').classList.add('active');
|
|
document.querySelectorAll('.debug-tabs .tab').forEach(t => t.classList.remove('active'));
|
|
}
|
|
});
|
|
|
|
closeDebug.addEventListener('click', () => {
|
|
debugOpen = false;
|
|
debugToggle.classList.remove('active');
|
|
debugTabs.style.display = 'none';
|
|
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
|
|
document.getElementById('tab-chat').classList.add('active');
|
|
});
|
|
|
|
document.querySelectorAll('.debug-tabs .tab:not(.close-debug)').forEach(tab => {
|
|
tab.addEventListener('click', () => {
|
|
document.querySelectorAll('.debug-tabs .tab').forEach(t => t.classList.remove('active'));
|
|
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
|
|
tab.classList.add('active');
|
|
document.getElementById(`tab-${tab.dataset.tab}`).classList.add('active');
|
|
|
|
if (tab.dataset.tab === 'refs') fetchRefs();
|
|
});
|
|
});
|
|
|
|
// ─── Activity Feed ──────────────────────────────────────────────
|
|
|
|
function getEntryClass(entry) {
|
|
if (entry.status === 'error') return 'error';
|
|
if (entry.type === 'command_start') return 'pending';
|
|
const cmd = entry.command || '';
|
|
if (NAV_COMMANDS.has(cmd)) return 'nav';
|
|
if (INTERACTION_COMMANDS.has(cmd)) return 'interaction';
|
|
if (OBSERVE_COMMANDS.has(cmd)) return 'observe';
|
|
return '';
|
|
}
|
|
|
|
function formatTime(ts) {
|
|
const d = new Date(ts);
|
|
return d.toLocaleTimeString('en-US', { hour12: false, hour: '2-digit', minute: '2-digit', second: '2-digit' });
|
|
}
|
|
|
|
let pendingEntries = new Map();
|
|
|
|
function createEntryElement(entry) {
|
|
const div = document.createElement('div');
|
|
div.className = `activity-entry ${getEntryClass(entry)}`;
|
|
div.setAttribute('role', 'article');
|
|
div.tabIndex = 0;
|
|
|
|
const argsText = entry.args ? entry.args.join(' ') : '';
|
|
const statusIcon = entry.status === 'ok' ? '\u2713' : entry.status === 'error' ? '\u2717' : '';
|
|
const statusClass = entry.status === 'ok' ? 'ok' : entry.status === 'error' ? 'err' : '';
|
|
const duration = entry.duration ? `${entry.duration}ms` : '';
|
|
|
|
div.innerHTML = `
|
|
<div class="entry-header">
|
|
<span class="entry-time">${formatTime(entry.timestamp)}</span>
|
|
<span class="entry-command">${escapeHtml(entry.command || entry.type)}</span>
|
|
</div>
|
|
${argsText ? `<div class="entry-args">${escapeHtml(argsText)}</div>` : ''}
|
|
${entry.type === 'command_end' ? `
|
|
<div class="entry-status">
|
|
<span class="${statusClass}">${statusIcon}</span>
|
|
<span class="duration">${duration}</span>
|
|
</div>
|
|
` : ''}
|
|
${entry.result ? `
|
|
<div class="entry-detail">
|
|
<div class="entry-result">${escapeHtml(entry.result)}</div>
|
|
</div>
|
|
` : ''}
|
|
`;
|
|
|
|
div.addEventListener('click', () => div.classList.toggle('expanded'));
|
|
return div;
|
|
}
|
|
|
|
function addEntry(entry) {
|
|
const feed = document.getElementById('activity-feed');
|
|
const empty = document.getElementById('empty-state');
|
|
if (empty) empty.style.display = 'none';
|
|
|
|
if (entry.type === 'command_end') {
|
|
for (const [id, el] of pendingEntries) {
|
|
if (el.querySelector('.entry-command')?.textContent === entry.command) {
|
|
el.remove();
|
|
pendingEntries.delete(id);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
const el = createEntryElement(entry);
|
|
feed.appendChild(el);
|
|
if (entry.type === 'command_start') pendingEntries.set(entry.id, el);
|
|
el.scrollIntoView({ behavior: 'smooth', block: 'end' });
|
|
|
|
if (entry.url) document.getElementById('footer-url')?.textContent && (document.getElementById('footer-url').textContent = new URL(entry.url).hostname);
|
|
lastId = Math.max(lastId, entry.id);
|
|
}
|
|
|
|
function escapeHtml(str) {
|
|
const div = document.createElement('div');
|
|
div.textContent = str;
|
|
return div.innerHTML;
|
|
}
|
|
|
|
// ─── SSE Connection ─────────────────────────────────────────────
|
|
|
|
function connectSSE() {
|
|
if (!serverUrl) return;
|
|
if (eventSource) { eventSource.close(); eventSource = null; }
|
|
|
|
const tokenParam = serverToken ? `&token=${serverToken}` : '';
|
|
const url = `${serverUrl}/activity/stream?after=${lastId}${tokenParam}`;
|
|
eventSource = new EventSource(url);
|
|
|
|
eventSource.addEventListener('activity', (e) => {
|
|
try { addEntry(JSON.parse(e.data)); } catch (err) {
|
|
console.error('[gstack sidebar] Failed to parse activity event:', err.message);
|
|
}
|
|
});
|
|
|
|
eventSource.addEventListener('gap', (e) => {
|
|
try {
|
|
const data = JSON.parse(e.data);
|
|
const feed = document.getElementById('activity-feed');
|
|
const banner = document.createElement('div');
|
|
banner.className = 'gap-banner';
|
|
banner.textContent = `Missed ${data.availableFrom - data.gapFrom} events`;
|
|
feed.appendChild(banner);
|
|
} catch (err) {
|
|
console.error('[gstack sidebar] Failed to parse gap event:', err.message);
|
|
}
|
|
});
|
|
}
|
|
|
|
// ─── Refs Tab ───────────────────────────────────────────────────
|
|
|
|
async function fetchRefs() {
|
|
if (!serverUrl) return;
|
|
try {
|
|
const headers = {};
|
|
if (serverToken) headers['Authorization'] = `Bearer ${serverToken}`;
|
|
const resp = await fetch(`${serverUrl}/refs`, { signal: AbortSignal.timeout(3000), headers });
|
|
if (!resp.ok) return;
|
|
const data = await resp.json();
|
|
|
|
const list = document.getElementById('refs-list');
|
|
const empty = document.getElementById('refs-empty');
|
|
const footer = document.getElementById('refs-footer');
|
|
|
|
if (!data.refs || data.refs.length === 0) {
|
|
empty.style.display = '';
|
|
list.innerHTML = '';
|
|
footer.textContent = '';
|
|
return;
|
|
}
|
|
|
|
empty.style.display = 'none';
|
|
list.innerHTML = data.refs.map(r => `
|
|
<div class="ref-row">
|
|
<span class="ref-id">${escapeHtml(r.ref)}</span>
|
|
<span class="ref-role">${escapeHtml(r.role)}</span>
|
|
<span class="ref-name">"${escapeHtml(r.name)}"</span>
|
|
</div>
|
|
`).join('');
|
|
footer.textContent = `${data.refs.length} refs`;
|
|
} catch (err) {
|
|
console.error('[gstack sidebar] Failed to fetch refs:', err.message);
|
|
}
|
|
}
|
|
|
|
// ─── Inspector Tab ──────────────────────────────────────────────
|
|
|
|
let inspectorPickerActive = false;
|
|
let inspectorData = null; // last inspect result
|
|
let inspectorModifications = []; // tracked style changes
|
|
let inspectorSSE = null;
|
|
|
|
// Inspector DOM refs
|
|
const inspectorPickBtn = document.getElementById('inspector-pick-btn');
|
|
const inspectorSelected = document.getElementById('inspector-selected');
|
|
const inspectorModeBadge = document.getElementById('inspector-mode-badge');
|
|
const inspectorEmpty = document.getElementById('inspector-empty');
|
|
const inspectorLoading = document.getElementById('inspector-loading');
|
|
const inspectorError = document.getElementById('inspector-error');
|
|
const inspectorPanels = document.getElementById('inspector-panels');
|
|
const inspectorBoxmodel = document.getElementById('inspector-boxmodel');
|
|
const inspectorRules = document.getElementById('inspector-rules');
|
|
const inspectorRuleCount = document.getElementById('inspector-rule-count');
|
|
const inspectorComputed = document.getElementById('inspector-computed');
|
|
const inspectorQuickedit = document.getElementById('inspector-quickedit');
|
|
const inspectorSend = document.getElementById('inspector-send');
|
|
const inspectorSendBtn = document.getElementById('inspector-send-btn');
|
|
|
|
// Pick button
|
|
inspectorPickBtn.addEventListener('click', () => {
|
|
if (inspectorPickerActive) {
|
|
inspectorPickerActive = false;
|
|
inspectorPickBtn.classList.remove('active');
|
|
chrome.runtime.sendMessage({ type: 'stopInspector' });
|
|
} else {
|
|
inspectorPickerActive = true;
|
|
inspectorPickBtn.classList.add('active');
|
|
inspectorShowLoading(false); // don't show loading yet, just activate
|
|
chrome.runtime.sendMessage({ type: 'startInspector' }, (result) => {
|
|
if (result?.error) {
|
|
inspectorPickerActive = false;
|
|
inspectorPickBtn.classList.remove('active');
|
|
inspectorShowError(result.error);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
function inspectorShowEmpty() {
|
|
inspectorEmpty.style.display = '';
|
|
inspectorLoading.style.display = 'none';
|
|
inspectorError.style.display = 'none';
|
|
inspectorPanels.style.display = 'none';
|
|
inspectorSend.style.display = 'none';
|
|
}
|
|
|
|
function inspectorShowLoading(show) {
|
|
if (show) {
|
|
inspectorEmpty.style.display = 'none';
|
|
inspectorLoading.style.display = '';
|
|
inspectorError.style.display = 'none';
|
|
inspectorPanels.style.display = 'none';
|
|
} else {
|
|
inspectorLoading.style.display = 'none';
|
|
}
|
|
}
|
|
|
|
function inspectorShowError(message) {
|
|
inspectorEmpty.style.display = 'none';
|
|
inspectorLoading.style.display = 'none';
|
|
inspectorError.style.display = '';
|
|
inspectorError.textContent = message;
|
|
inspectorPanels.style.display = 'none';
|
|
}
|
|
|
|
function inspectorShowData(data) {
|
|
inspectorData = data;
|
|
inspectorModifications = [];
|
|
inspectorEmpty.style.display = 'none';
|
|
inspectorLoading.style.display = 'none';
|
|
inspectorError.style.display = 'none';
|
|
inspectorPanels.style.display = '';
|
|
inspectorSend.style.display = '';
|
|
|
|
// Update toolbar
|
|
const tag = data.tagName || '?';
|
|
const cls = data.classes && data.classes.length > 0 ? '.' + data.classes.join('.') : '';
|
|
const idStr = data.id ? '#' + data.id : '';
|
|
inspectorSelected.textContent = `<${tag}>${idStr}${cls}`;
|
|
inspectorSelected.title = data.selector;
|
|
|
|
// Mode badge
|
|
if (data.mode === 'basic') {
|
|
inspectorModeBadge.textContent = 'Basic mode';
|
|
inspectorModeBadge.style.display = '';
|
|
inspectorModeBadge.className = 'inspector-mode-badge basic';
|
|
} else if (data.mode === 'cdp') {
|
|
inspectorModeBadge.textContent = 'CDP';
|
|
inspectorModeBadge.style.display = '';
|
|
inspectorModeBadge.className = 'inspector-mode-badge cdp';
|
|
} else {
|
|
inspectorModeBadge.style.display = 'none';
|
|
}
|
|
|
|
// Render sections
|
|
renderBoxModel(data);
|
|
renderMatchedRules(data);
|
|
renderComputedStyles(data);
|
|
renderQuickEdit(data);
|
|
updateSendButton();
|
|
}
|
|
|
|
// ─── Box Model Rendering ────────────────────────────────────────
|
|
|
|
function renderBoxModel(data) {
|
|
const box = data.basicData?.boxModel || data.boxModel;
|
|
if (!box) { inspectorBoxmodel.innerHTML = '<span class="inspector-no-data">No box model data</span>'; return; }
|
|
|
|
const m = box.margin || {};
|
|
const b = box.border || {};
|
|
const p = box.padding || {};
|
|
const c = box.content || {};
|
|
|
|
inspectorBoxmodel.innerHTML = `
|
|
<div class="boxmodel-margin">
|
|
<span class="boxmodel-label">margin</span>
|
|
<span class="boxmodel-value boxmodel-top">${fmtBoxVal(m.top)}</span>
|
|
<span class="boxmodel-value boxmodel-right">${fmtBoxVal(m.right)}</span>
|
|
<span class="boxmodel-value boxmodel-bottom">${fmtBoxVal(m.bottom)}</span>
|
|
<span class="boxmodel-value boxmodel-left">${fmtBoxVal(m.left)}</span>
|
|
<div class="boxmodel-border">
|
|
<span class="boxmodel-label">border</span>
|
|
<span class="boxmodel-value boxmodel-top">${fmtBoxVal(b.top)}</span>
|
|
<span class="boxmodel-value boxmodel-right">${fmtBoxVal(b.right)}</span>
|
|
<span class="boxmodel-value boxmodel-bottom">${fmtBoxVal(b.bottom)}</span>
|
|
<span class="boxmodel-value boxmodel-left">${fmtBoxVal(b.left)}</span>
|
|
<div class="boxmodel-padding">
|
|
<span class="boxmodel-label">padding</span>
|
|
<span class="boxmodel-value boxmodel-top">${fmtBoxVal(p.top)}</span>
|
|
<span class="boxmodel-value boxmodel-right">${fmtBoxVal(p.right)}</span>
|
|
<span class="boxmodel-value boxmodel-bottom">${fmtBoxVal(p.bottom)}</span>
|
|
<span class="boxmodel-value boxmodel-left">${fmtBoxVal(p.left)}</span>
|
|
<div class="boxmodel-content">
|
|
<span>${Math.round(c.width || 0)} x ${Math.round(c.height || 0)}</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
function fmtBoxVal(v) {
|
|
if (v === undefined || v === null) return '-';
|
|
const n = typeof v === 'number' ? v : parseFloat(v);
|
|
if (isNaN(n) || n === 0) return '0';
|
|
return Math.round(n * 10) / 10;
|
|
}
|
|
|
|
// ─── Matched Rules Rendering ────────────────────────────────────
|
|
|
|
function renderMatchedRules(data) {
|
|
const rules = data.matchedRules || data.basicData?.matchedRules || [];
|
|
inspectorRuleCount.textContent = rules.length > 0 ? `(${rules.length})` : '';
|
|
|
|
if (rules.length === 0) {
|
|
inspectorRules.innerHTML = '<div class="inspector-no-data">No matched rules</div>';
|
|
return;
|
|
}
|
|
|
|
// Separate UA rules from author rules
|
|
const authorRules = [];
|
|
const uaRules = [];
|
|
for (const rule of rules) {
|
|
if (rule.origin === 'user-agent' || rule.isUA) {
|
|
uaRules.push(rule);
|
|
} else {
|
|
authorRules.push(rule);
|
|
}
|
|
}
|
|
|
|
let html = '';
|
|
|
|
// Author rules (expanded)
|
|
for (const rule of authorRules) {
|
|
html += renderRule(rule, false);
|
|
}
|
|
|
|
// UA rules (collapsed by default)
|
|
if (uaRules.length > 0) {
|
|
html += `
|
|
<div class="inspector-ua-rules">
|
|
<button class="inspector-ua-toggle collapsed" aria-expanded="false">
|
|
<span class="inspector-toggle-arrow">▶</span>
|
|
User Agent (${uaRules.length})
|
|
</button>
|
|
<div class="inspector-ua-body collapsed">
|
|
`;
|
|
for (const rule of uaRules) {
|
|
html += renderRule(rule, true);
|
|
}
|
|
html += '</div></div>';
|
|
}
|
|
|
|
inspectorRules.innerHTML = html;
|
|
|
|
// Bind UA toggle
|
|
const uaToggle = inspectorRules.querySelector('.inspector-ua-toggle');
|
|
if (uaToggle) {
|
|
uaToggle.addEventListener('click', () => {
|
|
const body = inspectorRules.querySelector('.inspector-ua-body');
|
|
const isCollapsed = uaToggle.classList.contains('collapsed');
|
|
uaToggle.classList.toggle('collapsed', !isCollapsed);
|
|
uaToggle.setAttribute('aria-expanded', isCollapsed);
|
|
uaToggle.querySelector('.inspector-toggle-arrow').innerHTML = isCollapsed ? '▼' : '▶';
|
|
body.classList.toggle('collapsed', !isCollapsed);
|
|
});
|
|
}
|
|
}
|
|
|
|
function renderRule(rule, isUA) {
|
|
const selectorText = escapeHtml(rule.selector || '');
|
|
const truncatedSelector = selectorText.length > 35 ? selectorText.slice(0, 35) + '...' : selectorText;
|
|
const source = rule.source || '';
|
|
const sourceDisplay = source.includes('/') ? source.split('/').pop() : source;
|
|
const specificity = rule.specificity || '';
|
|
|
|
let propsHtml = '';
|
|
const props = rule.properties || [];
|
|
for (const prop of props) {
|
|
const overridden = prop.overridden ? ' overridden' : '';
|
|
const nameHtml = escapeHtml(prop.name);
|
|
const valText = escapeHtml(prop.value || '');
|
|
const truncatedVal = valText.length > 30 ? valText.slice(0, 30) + '...' : valText;
|
|
const priority = prop.priority === 'important' ? ' <span class="inspector-important">!important</span>' : '';
|
|
propsHtml += `<div class="inspector-prop${overridden}"><span class="inspector-prop-name">${nameHtml}</span>: <span class="inspector-prop-value" title="${valText}">${truncatedVal}</span>${priority};</div>`;
|
|
}
|
|
|
|
return `
|
|
<div class="inspector-rule" role="treeitem">
|
|
<div class="inspector-rule-header">
|
|
<span class="inspector-selector" title="${selectorText}">${truncatedSelector}</span>
|
|
${specificity ? `<span class="inspector-specificity">${escapeHtml(specificity)}</span>` : ''}
|
|
</div>
|
|
<div class="inspector-rule-props">${propsHtml}</div>
|
|
${sourceDisplay ? `<div class="inspector-rule-source">${escapeHtml(sourceDisplay)}</div>` : ''}
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
// ─── Computed Styles Rendering ──────────────────────────────────
|
|
|
|
function renderComputedStyles(data) {
|
|
const styles = data.computedStyles || data.basicData?.computedStyles || {};
|
|
const keys = Object.keys(styles);
|
|
|
|
if (keys.length === 0) {
|
|
inspectorComputed.innerHTML = '<div class="inspector-no-data">No computed styles</div>';
|
|
return;
|
|
}
|
|
|
|
let html = '';
|
|
for (const key of keys) {
|
|
const val = styles[key];
|
|
if (!val || val === 'none' || val === 'normal' || val === 'auto' || val === '0px' || val === 'rgba(0, 0, 0, 0)') continue;
|
|
html += `<div class="inspector-computed-row"><span class="inspector-prop-name">${escapeHtml(key)}</span>: <span class="inspector-prop-value">${escapeHtml(val)}</span></div>`;
|
|
}
|
|
|
|
if (!html) {
|
|
html = '<div class="inspector-no-data">All values are defaults</div>';
|
|
}
|
|
|
|
inspectorComputed.innerHTML = html;
|
|
}
|
|
|
|
// ─── Quick Edit ─────────────────────────────────────────────────
|
|
|
|
function renderQuickEdit(data) {
|
|
const selector = data.selector;
|
|
if (!selector) { inspectorQuickedit.innerHTML = ''; return; }
|
|
|
|
// Show common editable properties with current values
|
|
const editableProps = ['color', 'background-color', 'font-size', 'padding', 'margin', 'border', 'display', 'opacity'];
|
|
const computed = data.computedStyles || data.basicData?.computedStyles || {};
|
|
|
|
let html = '<div class="inspector-quickedit-list">';
|
|
for (const prop of editableProps) {
|
|
const val = computed[prop] || '';
|
|
html += `
|
|
<div class="inspector-quickedit-row" data-prop="${escapeHtml(prop)}">
|
|
<span class="inspector-prop-name">${escapeHtml(prop)}</span>:
|
|
<span class="inspector-quickedit-value" data-selector="${escapeHtml(selector)}" data-prop="${escapeHtml(prop)}" tabindex="0" role="button" title="Click to edit">${escapeHtml(val || '(none)')}</span>
|
|
</div>
|
|
`;
|
|
}
|
|
html += '</div>';
|
|
inspectorQuickedit.innerHTML = html;
|
|
|
|
// Bind click-to-edit
|
|
inspectorQuickedit.querySelectorAll('.inspector-quickedit-value').forEach(el => {
|
|
el.addEventListener('click', () => startQuickEdit(el));
|
|
el.addEventListener('keydown', (e) => {
|
|
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); startQuickEdit(el); }
|
|
});
|
|
});
|
|
}
|
|
|
|
function startQuickEdit(valueEl) {
|
|
if (valueEl.querySelector('input')) return; // already editing
|
|
|
|
const currentVal = valueEl.textContent === '(none)' ? '' : valueEl.textContent;
|
|
const prop = valueEl.dataset.prop;
|
|
const selector = valueEl.dataset.selector;
|
|
|
|
const input = document.createElement('input');
|
|
input.type = 'text';
|
|
input.className = 'inspector-quickedit-input';
|
|
input.value = currentVal;
|
|
valueEl.textContent = '';
|
|
valueEl.appendChild(input);
|
|
input.focus();
|
|
input.select();
|
|
|
|
function commit() {
|
|
const newVal = input.value.trim();
|
|
valueEl.textContent = newVal || '(none)';
|
|
if (newVal && newVal !== currentVal) {
|
|
chrome.runtime.sendMessage({
|
|
type: 'applyStyle',
|
|
selector,
|
|
property: prop,
|
|
value: newVal,
|
|
});
|
|
inspectorModifications.push({ property: prop, value: newVal, selector });
|
|
updateSendButton();
|
|
}
|
|
}
|
|
|
|
function cancel() {
|
|
valueEl.textContent = currentVal || '(none)';
|
|
}
|
|
|
|
input.addEventListener('blur', commit);
|
|
input.addEventListener('keydown', (e) => {
|
|
if (e.key === 'Enter') { e.preventDefault(); input.blur(); }
|
|
if (e.key === 'Escape') { e.preventDefault(); input.removeEventListener('blur', commit); cancel(); }
|
|
});
|
|
}
|
|
|
|
// ─── Send to Agent ──────────────────────────────────────────────
|
|
|
|
function updateSendButton() {
|
|
if (inspectorModifications.length > 0) {
|
|
inspectorSendBtn.textContent = 'Send to Code';
|
|
inspectorSendBtn.title = `${inspectorModifications.length} modification(s) to send`;
|
|
} else {
|
|
inspectorSendBtn.textContent = 'Send to Agent';
|
|
inspectorSendBtn.title = 'Send full inspector data';
|
|
}
|
|
}
|
|
|
|
inspectorSendBtn.addEventListener('click', () => {
|
|
if (!inspectorData) return;
|
|
|
|
let message;
|
|
if (inspectorModifications.length > 0) {
|
|
// Format modification diff
|
|
const diffs = inspectorModifications.map(m =>
|
|
` ${m.property}: ${m.value} (selector: ${m.selector})`
|
|
).join('\n');
|
|
message = `CSS Inspector modifications:\n\nSelector: ${inspectorData.selector}\n\nChanges:\n${diffs}`;
|
|
|
|
// Include source file info if available
|
|
const rules = inspectorData.matchedRules || inspectorData.basicData?.matchedRules || [];
|
|
const sources = rules.filter(r => r.source && r.source !== 'inline').map(r => r.source);
|
|
if (sources.length > 0) {
|
|
message += `\n\nSource files:\n${[...new Set(sources)].map(s => ` ${s}`).join('\n')}`;
|
|
}
|
|
} else {
|
|
// Send full inspector data
|
|
message = `CSS Inspector data for: ${inspectorData.selector}\n\n${JSON.stringify(inspectorData, null, 2)}`;
|
|
}
|
|
|
|
chrome.runtime.sendMessage({ type: 'sidebar-command', message });
|
|
});
|
|
|
|
// ─── Quick Action Helpers (shared between chat toolbar + inspector) ──
|
|
|
|
async function runCleanup(...buttons) {
|
|
if (!serverUrl || !serverToken) {
|
|
return;
|
|
}
|
|
buttons.forEach(b => b?.classList.add('loading'));
|
|
|
|
// Smart cleanup: send a chat message to the sidebar agent (an LLM).
|
|
// The agent snapshots the page, understands it semantically, and removes
|
|
// clutter intelligently. Much better than brittle CSS selectors.
|
|
const cleanupPrompt = [
|
|
'Clean up this page for reading. First run a quick deterministic pass:',
|
|
'$B cleanup --all',
|
|
'',
|
|
'Then take a snapshot to see what\'s left:',
|
|
'$B snapshot -i',
|
|
'',
|
|
'Look at the snapshot and identify remaining non-content elements:',
|
|
'- Ad placeholders, "ADVERTISEMENT" labels, sponsored content',
|
|
'- Cookie/consent banners, newsletter popups, login walls',
|
|
'- Audio/podcast player widgets, video autoplay',
|
|
'- Sidebar widgets (puzzles, games, "most popular", recommendations)',
|
|
'- Social share buttons, follow prompts, "See more on Google"',
|
|
'- Floating chat widgets, feedback buttons',
|
|
'- Navigation drawers, mega-menus (unless they ARE the page content)',
|
|
'- Empty whitespace from removed ads',
|
|
'',
|
|
'KEEP: the site header/masthead/logo, article headline, article body,',
|
|
'article images, author byline, date. The page should still look like',
|
|
'the site it is, just without the crap.',
|
|
'',
|
|
'For each element to remove, run JavaScript via $B to hide it:',
|
|
'$B eval "document.querySelector(\'SELECTOR\').style.display=\'none\'"',
|
|
'',
|
|
'Also unlock scrolling if the page is scroll-locked:',
|
|
'$B eval "document.body.style.overflow=\'auto\';document.documentElement.style.overflow=\'auto\'"',
|
|
].join('\n');
|
|
|
|
try {
|
|
// Send as a sidebar command (spawns the agent)
|
|
const resp = await fetch(`${serverUrl}/sidebar-command`, {
|
|
method: 'POST',
|
|
headers: authHeaders(),
|
|
body: JSON.stringify({ message: cleanupPrompt }),
|
|
signal: AbortSignal.timeout(5000),
|
|
});
|
|
if (resp.ok) {
|
|
addChatEntry({ type: 'notification', message: 'Cleaning up page (agent is analyzing...)' });
|
|
} else {
|
|
addChatEntry({ type: 'notification', message: 'Failed to start cleanup' });
|
|
}
|
|
} catch (err) {
|
|
addChatEntry({ type: 'notification', message: 'Cleanup failed: ' + err.message });
|
|
} finally {
|
|
// Remove loading after a short delay (agent runs async)
|
|
setTimeout(() => buttons.forEach(b => b?.classList.remove('loading')), 2000);
|
|
}
|
|
}
|
|
|
|
async function runScreenshot(...buttons) {
|
|
if (!serverUrl || !serverToken) {
|
|
return;
|
|
}
|
|
buttons.forEach(b => b?.classList.add('loading'));
|
|
try {
|
|
const resp = await fetch(`${serverUrl}/command`, {
|
|
method: 'POST',
|
|
headers: { ...authHeaders(), 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ command: 'screenshot', args: [] }),
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
const text = await resp.text();
|
|
if (resp.ok) {
|
|
addChatEntry({ type: 'notification', message: text || 'Screenshot saved' });
|
|
} else {
|
|
const err = JSON.parse(text).error || 'Screenshot failed';
|
|
addChatEntry({ type: 'notification', message: 'Error: ' + err });
|
|
}
|
|
} catch (err) {
|
|
addChatEntry({ type: 'notification', message: 'Screenshot failed: ' + err.message });
|
|
} finally {
|
|
buttons.forEach(b => b?.classList.remove('loading'));
|
|
}
|
|
}
|
|
|
|
// ─── Wire up all cleanup/screenshot buttons (inspector + chat toolbar) ──
|
|
|
|
const inspectorCleanupBtn = document.getElementById('inspector-cleanup-btn');
|
|
const inspectorScreenshotBtn = document.getElementById('inspector-screenshot-btn');
|
|
const chatCleanupBtn = document.getElementById('chat-cleanup-btn');
|
|
const chatScreenshotBtn = document.getElementById('chat-screenshot-btn');
|
|
|
|
if (inspectorCleanupBtn) inspectorCleanupBtn.addEventListener('click', () => runCleanup(inspectorCleanupBtn, chatCleanupBtn));
|
|
if (inspectorScreenshotBtn) inspectorScreenshotBtn.addEventListener('click', () => runScreenshot(inspectorScreenshotBtn, chatScreenshotBtn));
|
|
if (chatCleanupBtn) chatCleanupBtn.addEventListener('click', () => runCleanup(chatCleanupBtn, inspectorCleanupBtn));
|
|
if (chatScreenshotBtn) chatScreenshotBtn.addEventListener('click', () => runScreenshot(chatScreenshotBtn, inspectorScreenshotBtn));
|
|
|
|
// ─── Section Toggles ────────────────────────────────────────────
|
|
|
|
document.querySelectorAll('.inspector-section-toggle').forEach(toggle => {
|
|
toggle.addEventListener('click', () => {
|
|
const section = toggle.dataset.section;
|
|
const body = document.getElementById(`inspector-${section}`);
|
|
const isCollapsed = toggle.classList.contains('collapsed');
|
|
|
|
toggle.classList.toggle('collapsed', !isCollapsed);
|
|
toggle.setAttribute('aria-expanded', isCollapsed);
|
|
toggle.querySelector('.inspector-toggle-arrow').innerHTML = isCollapsed ? '▼' : '▶';
|
|
body.classList.toggle('collapsed', !isCollapsed);
|
|
});
|
|
});
|
|
|
|
// ─── Inspector SSE ──────────────────────────────────────────────
|
|
|
|
function connectInspectorSSE() {
|
|
if (!serverUrl || !serverToken) return;
|
|
if (inspectorSSE) { inspectorSSE.close(); inspectorSSE = null; }
|
|
|
|
const tokenParam = serverToken ? `&token=${serverToken}` : '';
|
|
const url = `${serverUrl}/inspector/events?_=${Date.now()}${tokenParam}`;
|
|
|
|
try {
|
|
inspectorSSE = new EventSource(url);
|
|
|
|
inspectorSSE.addEventListener('inspectResult', (e) => {
|
|
try {
|
|
const data = JSON.parse(e.data);
|
|
inspectorShowData(data);
|
|
} catch (err) {
|
|
console.error('[gstack sidebar] Failed to parse inspectResult:', err.message);
|
|
}
|
|
});
|
|
|
|
inspectorSSE.addEventListener('error', () => {
|
|
// SSE connection failed — inspector works without it (basic mode)
|
|
if (inspectorSSE) { inspectorSSE.close(); inspectorSSE = null; }
|
|
});
|
|
} catch (err) {
|
|
console.debug('[gstack sidebar] Inspector SSE not available:', err.message);
|
|
}
|
|
}
|
|
|
|
// ─── Server Discovery ───────────────────────────────────────────
|
|
|
|
function setActionButtonsEnabled(enabled) {
|
|
const btns = document.querySelectorAll('.quick-action-btn, .inspector-action-btn');
|
|
btns.forEach(btn => {
|
|
btn.disabled = !enabled;
|
|
btn.classList.toggle('disabled', !enabled);
|
|
});
|
|
}
|
|
|
|
function updateConnection(url, token) {
|
|
const wasConnected = !!serverUrl;
|
|
serverUrl = url;
|
|
serverToken = token || null;
|
|
if (url) {
|
|
document.getElementById('footer-dot').className = 'dot connected';
|
|
const port = new URL(url).port;
|
|
document.getElementById('footer-port').textContent = `:${port}`;
|
|
setConnState('connected');
|
|
setActionButtonsEnabled(true);
|
|
// Tell the active tab's content script the sidebar is open — this hides
|
|
// the welcome page arrow hint. Only fires on actual sidebar connection.
|
|
chrome.runtime.sendMessage({ type: 'sidebarOpened' }).catch(() => {});
|
|
connectSSE();
|
|
connectInspectorSSE();
|
|
if (chatPollInterval) clearInterval(chatPollInterval);
|
|
chatPollInterval = setInterval(pollChat, SLOW_POLL_MS);
|
|
pollChat();
|
|
// Poll browser tabs every 2s (lightweight, just tab list)
|
|
if (tabPollInterval) clearInterval(tabPollInterval);
|
|
tabPollInterval = setInterval(pollTabs, 2000);
|
|
pollTabs();
|
|
} else {
|
|
document.getElementById('footer-dot').className = 'dot';
|
|
document.getElementById('footer-port').textContent = '';
|
|
setActionButtonsEnabled(false);
|
|
if (chatPollInterval) { clearInterval(chatPollInterval); chatPollInterval = null; }
|
|
if (tabPollInterval) { clearInterval(tabPollInterval); tabPollInterval = null; }
|
|
if (wasConnected) {
|
|
startReconnect();
|
|
}
|
|
}
|
|
}
|
|
|
|
// ─── Port Configuration ─────────────────────────────────────────
|
|
|
|
const portLabel = document.getElementById('footer-port');
|
|
const portInput = document.getElementById('port-input');
|
|
|
|
portLabel.addEventListener('click', () => {
|
|
portLabel.style.display = 'none';
|
|
portInput.style.display = '';
|
|
chrome.runtime.sendMessage({ type: 'getPort' }, (resp) => {
|
|
portInput.value = resp?.port || '';
|
|
portInput.focus();
|
|
portInput.select();
|
|
});
|
|
});
|
|
|
|
function savePort() {
|
|
const port = parseInt(portInput.value, 10);
|
|
if (port > 0 && port < 65536) {
|
|
chrome.runtime.sendMessage({ type: 'setPort', port });
|
|
}
|
|
portInput.style.display = 'none';
|
|
portLabel.style.display = '';
|
|
}
|
|
portInput.addEventListener('blur', savePort);
|
|
portInput.addEventListener('keydown', (e) => {
|
|
if (e.key === 'Enter') savePort();
|
|
if (e.key === 'Escape') { portInput.style.display = 'none'; portLabel.style.display = ''; }
|
|
});
|
|
|
|
// ─── Reconnect / Copy Buttons ────────────────────────────────────
|
|
|
|
document.getElementById('conn-reconnect').addEventListener('click', () => {
|
|
reconnectAttempts = 0;
|
|
startReconnect();
|
|
});
|
|
|
|
document.getElementById('conn-copy').addEventListener('click', () => {
|
|
navigator.clipboard.writeText('/open-gstack-browser').then(() => {
|
|
const btn = document.getElementById('conn-copy');
|
|
btn.textContent = 'copied!';
|
|
setTimeout(() => { btn.textContent = '/open-gstack-browser'; }, 2000);
|
|
});
|
|
});
|
|
|
|
// Try to connect immediately, retry every 2s until connected.
|
|
// Show exactly what's happening at each step so the user is never
|
|
// staring at a blank "Connecting..." with no info.
|
|
let connectAttempts = 0;
|
|
function setLoadingStatus(msg, debug) {
|
|
const status = document.getElementById('loading-status');
|
|
const dbg = document.getElementById('loading-debug');
|
|
if (status) status.textContent = msg;
|
|
if (dbg && debug !== undefined) dbg.textContent = debug;
|
|
}
|
|
|
|
async function tryConnect() {
|
|
connectAttempts++;
|
|
setLoadingStatus(
|
|
`Looking for browse server... (attempt ${connectAttempts})`,
|
|
`Asking background.js for server port...`
|
|
);
|
|
|
|
// Step 1: Ask background for the port
|
|
const resp = await new Promise(resolve => {
|
|
chrome.runtime.sendMessage({ type: 'getPort' }, (r) => {
|
|
if (chrome.runtime.lastError) {
|
|
resolve({ error: chrome.runtime.lastError.message });
|
|
} else {
|
|
resolve(r || {});
|
|
}
|
|
});
|
|
});
|
|
|
|
if (resp.error) {
|
|
setLoadingStatus(
|
|
`Extension error (attempt ${connectAttempts})`,
|
|
`chrome.runtime.sendMessage failed:\n${resp.error}`
|
|
);
|
|
setTimeout(tryConnect, 2000);
|
|
return;
|
|
}
|
|
|
|
const port = resp.port || 34567;
|
|
|
|
// Step 2: If background says connected + has token, use that
|
|
if (resp.port && resp.connected && resp.token) {
|
|
setLoadingStatus(
|
|
`Server found on port ${port}, connecting...`,
|
|
`token: yes\nStarting SSE + chat polling...`
|
|
);
|
|
updateConnection(`http://127.0.0.1:${port}`, resp.token);
|
|
return;
|
|
}
|
|
|
|
// Step 3: Background not connected yet. Try hitting /health directly.
|
|
// This bypasses the background.js health poll timing gap.
|
|
setLoadingStatus(
|
|
`Checking server directly... (attempt ${connectAttempts})`,
|
|
`port: ${port}\nbackground connected: ${resp.connected || false}\nTrying GET http://127.0.0.1:${port}/health ...`
|
|
);
|
|
|
|
try {
|
|
const healthResp = await fetch(`http://127.0.0.1:${port}/health`, {
|
|
signal: AbortSignal.timeout(2000)
|
|
});
|
|
if (healthResp.ok) {
|
|
const data = await healthResp.json();
|
|
if (data.status === 'healthy' && data.token) {
|
|
setLoadingStatus(
|
|
`Server healthy on port ${port}, connecting...`,
|
|
`token: yes (from /health)\nStarting SSE + chat polling...`
|
|
);
|
|
updateConnection(`http://127.0.0.1:${port}`, data.token);
|
|
return;
|
|
}
|
|
setLoadingStatus(
|
|
`Server responded but not healthy (attempt ${connectAttempts})`,
|
|
`status: ${data.status}\ntoken: ${data.token ? 'yes' : 'no'}`
|
|
);
|
|
} else {
|
|
setLoadingStatus(
|
|
`Server returned ${healthResp.status} (attempt ${connectAttempts})`,
|
|
`GET /health → ${healthResp.status} ${healthResp.statusText}`
|
|
);
|
|
}
|
|
} catch (e) {
|
|
setLoadingStatus(
|
|
`Server not reachable on port ${port} (attempt ${connectAttempts})`,
|
|
`GET /health failed: ${e.message}\n\nThe browse server may still be starting.\nRun /open-gstack-browser in Claude Code.`
|
|
);
|
|
}
|
|
|
|
setTimeout(tryConnect, 2000);
|
|
}
|
|
tryConnect();
|
|
|
|
// ─── Message Listener ───────────────────────────────────────────
|
|
|
|
chrome.runtime.onMessage.addListener((msg) => {
|
|
if (msg.type === 'health') {
|
|
if (msg.data) {
|
|
const url = `http://127.0.0.1:${msg.data.port || 34567}`;
|
|
updateConnection(url, msg.data.token);
|
|
applyChatEnabled(!!msg.data.chatEnabled);
|
|
} else {
|
|
updateConnection(null);
|
|
}
|
|
}
|
|
if (msg.type === 'refs') {
|
|
if (document.querySelector('.tab[data-tab="refs"].active')) {
|
|
fetchRefs();
|
|
}
|
|
}
|
|
if (msg.type === 'inspectResult') {
|
|
inspectorPickerActive = false;
|
|
inspectorPickBtn.classList.remove('active');
|
|
if (msg.data) {
|
|
inspectorShowData(msg.data);
|
|
} else {
|
|
inspectorShowError('Element not found, try picking again');
|
|
}
|
|
}
|
|
if (msg.type === 'pickerCancelled') {
|
|
inspectorPickerActive = false;
|
|
inspectorPickBtn.classList.remove('active');
|
|
}
|
|
// Instant tab switch — background.js fires this on chrome.tabs.onActivated
|
|
if (msg.type === 'browserTabActivated') {
|
|
// Tell the server which tab is now active, then switch chat context
|
|
if (serverUrl && serverToken) {
|
|
fetch(`${serverUrl}/sidebar-tabs?activeUrl=${encodeURIComponent(msg.url || '')}`, {
|
|
headers: authHeaders(),
|
|
signal: AbortSignal.timeout(2000),
|
|
}).then(r => r.json()).then(data => {
|
|
if (data.tabs) {
|
|
renderTabBar(data.tabs);
|
|
// Find the server-side tab ID for this Chrome tab
|
|
const activeTab = data.tabs.find(t => t.active);
|
|
if (activeTab && activeTab.id !== sidebarActiveTabId) {
|
|
switchChatTab(activeTab.id);
|
|
}
|
|
}
|
|
}).catch(() => {});
|
|
}
|
|
}
|
|
});
|
|
|
|
// ─── Chat Gate ──────────────────────────────────────────────────
|
|
// Show/hide Chat tab + command bar based on chatEnabled from server
|
|
|
|
function applyChatEnabled(enabled) {
|
|
const commandBar = document.querySelector('.command-bar');
|
|
const chatTab = document.getElementById('tab-chat');
|
|
const banner = document.getElementById('experimental-banner');
|
|
const clearBtn = document.getElementById('clear-chat');
|
|
|
|
if (enabled) {
|
|
// Chat is enabled: show command bar, chat tab, experimental banner
|
|
if (commandBar) commandBar.style.display = '';
|
|
if (chatTab) chatTab.style.display = '';
|
|
if (banner) banner.style.display = '';
|
|
if (clearBtn) clearBtn.style.display = '';
|
|
} else {
|
|
// Chat disabled: hide command bar, chat content, clear button
|
|
if (commandBar) commandBar.style.display = 'none';
|
|
if (banner) banner.style.display = 'none';
|
|
if (clearBtn) clearBtn.style.display = 'none';
|
|
// If currently on chat tab, switch to activity
|
|
if (chatTab && chatTab.classList.contains('active')) {
|
|
chatTab.classList.remove('active');
|
|
// Open debug tabs and show activity
|
|
const debugToggle = document.getElementById('debug-toggle');
|
|
const debugTabs = document.getElementById('debug-tabs');
|
|
if (debugToggle) debugToggle.classList.add('active');
|
|
if (debugTabs) debugTabs.style.display = 'flex';
|
|
const activityTab = document.getElementById('tab-activity');
|
|
if (activityTab) activityTab.classList.add('active');
|
|
const activityBtn = document.querySelector('.tab[data-tab="activity"]');
|
|
if (activityBtn) activityBtn.classList.add('active');
|
|
}
|
|
}
|
|
}
|