171 lines
6.1 KiB
Bash
Executable File
171 lines
6.1 KiB
Bash
Executable File
#!/bin/bash
|
|
set -e
|
|
|
|
# Installs opencode Browser's native messaging host and (optionally) wires the
|
|
# MCP server into opencode.
|
|
#
|
|
# Usage:
|
|
# ./install.sh register the native host for the extension
|
|
# ./install.sh --opencode ...and add the MCP server to opencode's
|
|
# global config (~/.config/opencode/opencode.json)
|
|
# ./install.sh --uninstall remove the native host registrations
|
|
#
|
|
# The extension has a fixed ID (manifest "key"), so no ID needs to be passed.
|
|
# Extra IDs can be given with --id <extension-id> (repeatable), e.g. for a
|
|
# copy of the extension loaded from another folder.
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
HOST_DIR="$SCRIPT_DIR/host"
|
|
HOST_NAME="com.opencode.browser"
|
|
EXTENSION_IDS=("ilgnnaeidkbcbnklkmjllflfkmendiib")
|
|
CONFIGURE_OPENCODE=0
|
|
UNINSTALL=0
|
|
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--opencode) CONFIGURE_OPENCODE=1 ;;
|
|
--uninstall) UNINSTALL=1 ;;
|
|
--id) shift; EXTENSION_IDS+=("$1") ;;
|
|
-h|--help) sed -n 4,16p "$0"; exit 0 ;;
|
|
*) echo "Unknown option: $1"; exit 1 ;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
case "$(uname)" in
|
|
Darwin) OS_KIND="mac" ;;
|
|
Linux) OS_KIND="linux" ;;
|
|
MINGW*|MSYS*|CYGWIN*) OS_KIND="windows" ;;
|
|
*) echo "Unsupported platform: $(uname)"; exit 1 ;;
|
|
esac
|
|
|
|
host_dirs() {
|
|
case "$OS_KIND" in
|
|
mac)
|
|
local base="$HOME/Library/Application Support"
|
|
echo "Google Chrome|$base/Google/Chrome/NativeMessagingHosts"
|
|
echo "Google Chrome Beta|$base/Google/Chrome Beta/NativeMessagingHosts"
|
|
echo "Google Chrome Canary|$base/Google/Chrome Canary/NativeMessagingHosts"
|
|
echo "Chrome for Testing|$base/Google/Chrome for Testing/NativeMessagingHosts"
|
|
echo "Chromium|$base/Chromium/NativeMessagingHosts"
|
|
echo "Microsoft Edge|$base/Microsoft Edge/NativeMessagingHosts"
|
|
echo "Brave|$base/BraveSoftware/Brave-Browser/NativeMessagingHosts"
|
|
echo "Vivaldi|$base/Vivaldi/NativeMessagingHosts"
|
|
echo "Arc|$base/Arc/User Data/NativeMessagingHosts"
|
|
;;
|
|
linux)
|
|
echo "Google Chrome|$HOME/.config/google-chrome/NativeMessagingHosts"
|
|
echo "Chromium|$HOME/.config/chromium/NativeMessagingHosts"
|
|
echo "Microsoft Edge|$HOME/.config/microsoft-edge/NativeMessagingHosts"
|
|
echo "Brave|$HOME/.config/BraveSoftware/Brave-Browser/NativeMessagingHosts"
|
|
echo "Vivaldi|$HOME/.config/vivaldi/NativeMessagingHosts"
|
|
;;
|
|
esac
|
|
}
|
|
|
|
if [ "$UNINSTALL" = 1 ]; then
|
|
if [ "$OS_KIND" = "windows" ]; then
|
|
for key in 'HKCU\Software\Google\Chrome' 'HKCU\Software\Microsoft\Edge' 'HKCU\Software\BraveSoftware\Brave-Browser' 'HKCU\Software\Chromium'; do
|
|
reg.exe delete "$key\\NativeMessagingHosts\\$HOST_NAME" //f >/dev/null 2>&1 && echo "Removed: $key" || true
|
|
done
|
|
else
|
|
while IFS='|' read -r name dir; do
|
|
if [ -f "$dir/$HOST_NAME.json" ]; then rm "$dir/$HOST_NAME.json"; echo "Removed: $name"; fi
|
|
done < <(host_dirs)
|
|
fi
|
|
exit 0
|
|
fi
|
|
|
|
command -v node >/dev/null || { echo "Error: Node.js 18+ is required."; exit 1; }
|
|
NODE_BIN="$(command -v node)"
|
|
|
|
echo "Installing host dependencies..."
|
|
(cd "$HOST_DIR" && npm install --silent --no-audit --no-fund)
|
|
|
|
ORIGINS=""
|
|
for i in "${!EXTENSION_IDS[@]}"; do
|
|
[ "$i" -gt 0 ] && ORIGINS="$ORIGINS,"
|
|
ORIGINS="$ORIGINS
|
|
\"chrome-extension://${EXTENSION_IDS[$i]}/\""
|
|
done
|
|
|
|
json_escape() { printf '%s' "$1" | sed 's/\\/\\\\/g'; }
|
|
|
|
manifest() {
|
|
cat <<EOF
|
|
{
|
|
"name": "$HOST_NAME",
|
|
"description": "opencode Browser native messaging host",
|
|
"path": "$1",
|
|
"type": "stdio",
|
|
"allowed_origins": [$ORIGINS
|
|
]
|
|
}
|
|
EOF
|
|
}
|
|
|
|
echo ""
|
|
if [ "$OS_KIND" = "windows" ]; then
|
|
NODE_WIN="$(cygpath -w "$NODE_BIN")"
|
|
[ -f "${NODE_BIN}.exe" ] && NODE_WIN="$(cygpath -w "${NODE_BIN}.exe")"
|
|
BAT="$HOST_DIR/native-host-wrapper.bat"
|
|
printf '@echo off\r\n"%s" "%s" %%*\r\n' "$NODE_WIN" "$(cygpath -w "$HOST_DIR/native-host.js")" > "$BAT"
|
|
MANIFEST="$HOST_DIR/$HOST_NAME.json"
|
|
manifest "$(json_escape "$(cygpath -w "$BAT")")" > "$MANIFEST"
|
|
for key in 'HKCU\Software\Google\Chrome' 'HKCU\Software\Microsoft\Edge' 'HKCU\Software\BraveSoftware\Brave-Browser' 'HKCU\Software\Chromium'; do
|
|
reg.exe add "$key\\NativeMessagingHosts\\$HOST_NAME" //ve //t REG_SZ //d "$(cygpath -w "$MANIFEST")" //f >/dev/null 2>&1 \
|
|
&& echo " Registered: $key" || echo " Could not register: $key"
|
|
done
|
|
MCP_SERVER="$(cygpath -w "$HOST_DIR/mcp-server.js")"
|
|
else
|
|
# The browser runs the native host, and macOS blocks apps from executing
|
|
# files in ~/Documents, ~/Desktop and ~/Downloads without a privacy grant.
|
|
# So the host (dependency-free: three files) is copied somewhere neutral.
|
|
if [ "$OS_KIND" = "mac" ]; then
|
|
RUN_DIR="$HOME/Library/Application Support/opencode-browser/host"
|
|
else
|
|
RUN_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/opencode-browser/host"
|
|
fi
|
|
mkdir -p "$RUN_DIR"
|
|
cp "$HOST_DIR/native-host.js" "$HOST_DIR/endpoint.js" "$RUN_DIR/"
|
|
printf '{ "type": "module", "private": true }\n' > "$RUN_DIR/package.json"
|
|
echo "Copied the native host to $RUN_DIR"
|
|
WRAPPER="$RUN_DIR/native-host-wrapper.sh"
|
|
# Chrome launches the host with a minimal PATH, so pin the node binary.
|
|
cat > "$WRAPPER" <<EOF
|
|
#!/bin/sh
|
|
cd "$RUN_DIR" || exit 1
|
|
exec "$NODE_BIN" "$RUN_DIR/native-host.js" "\$@"
|
|
EOF
|
|
chmod +x "$WRAPPER"
|
|
while IFS='|' read -r name dir; do
|
|
parent="$(dirname "$dir")"
|
|
[ -d "$parent" ] || continue
|
|
mkdir -p "$dir"
|
|
manifest "$(json_escape "$WRAPPER")" > "$dir/$HOST_NAME.json"
|
|
echo " Registered for $name"
|
|
done < <(host_dirs)
|
|
MCP_SERVER="$HOST_DIR/mcp-server.js"
|
|
fi
|
|
|
|
if [ "$CONFIGURE_OPENCODE" = 1 ]; then
|
|
echo ""
|
|
node "$SCRIPT_DIR/scripts/configure-opencode.mjs" "$NODE_BIN" "$MCP_SERVER" "$HOST_DIR/instructions.md"
|
|
fi
|
|
|
|
cat <<EOF
|
|
|
|
Done. Next steps:
|
|
1. Open chrome://extensions, enable Developer mode, click "Load unpacked"
|
|
and select: $SCRIPT_DIR/extension
|
|
(the extension ID must be ${EXTENSION_IDS[0]})
|
|
2. Click the extension's toolbar icon: it should say "Connected".
|
|
EOF
|
|
if [ "$CONFIGURE_OPENCODE" = 0 ]; then
|
|
cat <<EOF
|
|
3. Add the MCP server to opencode (or re-run with --opencode):
|
|
"mcp": { "chrome": { "type": "local", "command": ["$NODE_BIN", "$MCP_SERVER"], "timeout": 150000 } },
|
|
"instructions": ["$HOST_DIR/instructions.md"]
|
|
EOF
|
|
fi
|