website-htmx-rustelo-code/scripts/wrks/auto-browser-logger.sh
2026-07-10 03:44:13 +01:00

147 lines
No EOL
4.9 KiB
Bash
Executable file
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Auto Browser Logger - Automatically calls Claude Code if not running under it
# This script can detect its environment and auto-invoke Claude Code for MCP tools
set -e
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
LOG_FILE="$1"
PAGE_NAME="$2"
URL="$3"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log_info() { echo -e "${BLUE} $1${NC}"; }
log_success() { echo -e "${GREEN}$1${NC}"; }
log_warning() { echo -e "${YELLOW}⚠️ $1${NC}"; }
log_error() { echo -e "${RED}$1${NC}"; }
# Function to detect if running under Claude Code
is_under_claude() {
# Check for Claude Code environment variables or if script is being executed via Claude
[ -n "$CLAUDE_CODE_SESSION" ] || \
[ -n "$ANTHROPIC_SESSION" ] || \
[ -n "$CLAUDE_SESSION" ] || \
[ "$TERM_PROGRAM" = "claude-code" ] || \
[ -n "$CLAUDE_CONTEXT" ] || \
# Check if we're in a context where Claude Code tools are available
command -v mcp__browser-tools__getConsoleLogs >/dev/null 2>&1 || \
# Check if this script is being called from within a Claude Code execution context
[[ "${BASH_SOURCE[0]}" =~ claude|tmp ]]
}
# Function to collect real browser logs via Claude Code
collect_browser_logs_via_claude() {
local log_file="$1"
local page_name="$2"
local url="$3"
log_info "Collecting real browser logs via Claude Code..."
# Create a Claude Code command to execute MCP tools
local claude_cmd_file="/tmp/claude_mcp_commands_${page_name}_$(date +%s).txt"
cat > "$claude_cmd_file" << EOF
Please collect browser logs and update the file: $log_file
Run these MCP browser tools and append results to the log file:
1. mcp__browser-tools__getConsoleLogs
2. mcp__browser-tools__getConsoleErrors
3. mcp__browser-tools__getNetworkErrors
Replace the section "CLAUDE_COMMANDS_START" to "CLAUDE_COMMANDS_END" with the real browser logs data.
EOF
if command -v claude-code >/dev/null 2>&1; then
log_info "Executing claude-code with MCP browser tools..."
# Try to execute Claude Code
claude-code --prompt "$(cat "$claude_cmd_file")" 2>/dev/null || {
log_warning "Direct claude-code execution failed, creating manual instructions"
return 1
}
rm -f "$claude_cmd_file"
return 0
else
log_error "claude-code command not found"
return 1
fi
}
# Main function
main() {
if [ $# -lt 3 ]; then
echo "Usage: $0 <log_file> <page_name> <url>"
exit 1
fi
log_info "Auto Browser Logger"
log_info "Log file: $LOG_FILE"
log_info "Page: $PAGE_NAME"
log_info "URL: $URL"
if is_under_claude; then
log_success "Running under Claude Code environment"
# Add header to log file
{
echo ""
echo "--- AUTO-COLLECTED REAL BROWSER LOGS ---"
echo "[$(date +"%H:%M:%S")] Environment: Claude Code detected"
echo "[$(date +"%H:%M:%S")] Collecting real browser logs..."
} >> "$LOG_FILE"
log_info "Environment detected as Claude Code - logs should be collected automatically"
else
log_warning "Not running under Claude Code"
# Add placeholder section that Claude Code can replace
{
echo ""
echo "--- AUTO-COLLECTED REAL BROWSER LOGS ---"
echo "[$(date +"%H:%M:%S")] Environment: Standalone script"
echo "[$(date +"%H:%M:%S")] Attempting to call Claude Code..."
echo ""
echo "CLAUDE_COMMANDS_START"
echo "# These commands should be replaced with real browser logs:"
echo "# mcp__browser-tools__getConsoleLogs"
echo "# mcp__browser-tools__getConsoleErrors"
echo "# mcp__browser-tools__getNetworkErrors"
echo "CLAUDE_COMMANDS_END"
echo ""
} >> "$LOG_FILE"
# Try to collect logs via Claude Code
if collect_browser_logs_via_claude "$LOG_FILE" "$PAGE_NAME" "$URL"; then
log_success "Successfully called Claude Code for log collection"
else
log_warning "Failed to auto-call Claude Code"
{
echo "[$(date +"%H:%M:%S")] Failed to auto-call Claude Code"
echo "[MANUAL ACTION] Please run claude-code and execute:"
echo " mcp__browser-tools__getConsoleLogs"
echo " mcp__browser-tools__getConsoleErrors"
echo " mcp__browser-tools__getNetworkErrors"
echo " Then update this log file: $LOG_FILE"
} >> "$LOG_FILE"
fi
fi
{
echo ""
echo "[$(date +"%H:%M:%S")] Auto browser logger completed"
} >> "$LOG_FILE"
log_success "Auto browser logger completed"
}
# Run main function
main "$@"