#!/usr/bin/env bash
# PreToolUse hook — intercepts browser_navigate with file:// URLs.
#
# Scoped to mcp__plugin_playwright_playwright__browser_navigate via
# the settings.json matcher. Blocks file:// attempts with actionable
# guidance so the agent self-corrects on the first try instead of
# retrying into Playwright's cryptic error + 2-minute MCP timeout.
#
# Error philosophy: fail open. This is a guidance hook, not a security
# boundary. If parsing fails, the tool proceeds normally and the agent
# gets the standard Playwright error.

if [ -t 0 ]; then
  exit 0
fi
INPUT=$(cat)

if echo "$INPUT" | grep -qiE '"url"\s*:\s*"file://'; then
  cat >&2 << 'EOF'
file:// URLs are blocked by Playwright. To view a local HTML file in the browser:

1. Start a local HTTP server: python3 -m http.server 8080
2. Navigate to http://localhost:8080/filename.html instead

Do not retry with file:// — it will always fail.
EOF
  exit 2
fi

exit 0
