← back to Ken

kalshi-dash/ken-refresh-site.sh

47 lines

#!/bin/bash
# ken-refresh-site.sh — reload the Ken dashboard tab (127.0.0.1:7810) in whichever
# browser currently has it open. Only touches ALREADY-RUNNING browsers (never launches one).
# Reloads ALL matching tabs; prints how many it refreshed.
URLMATCH="${1:-127.0.0.1:7810}"
n=0
# Chromium-family (Chrome/Brave/Arc/Edge): tab has .reload
for app in "Google Chrome" "Brave Browser" "Arc" "Microsoft Edge"; do
  osascript >/dev/null 2>&1 <<OSA && n=$((n+1))
tell application "System Events" to if not (exists process "$app") then error "notrunning"
tell application "$app"
  set hit to false
  repeat with w in windows
    repeat with t in tabs of w
      if (URL of t) contains "$URLMATCH" then
        tell t to reload
        set hit to true
      end if
    end repeat
  end repeat
  if not hit then error "notab"
end tell
OSA
done
# Safari: reload by re-setting URL to itself
osascript >/dev/null 2>&1 <<'OSA' && n=$((n+1))
tell application "System Events" to if not (exists process "Safari") then error "notrunning"
tell application "Safari"
  set hit to false
  repeat with w in windows
    repeat with t in tabs of w
      if (URL of t) contains "127.0.0.1:7810" then
        set URL of t to (URL of t)
        set hit to true
      end if
    end repeat
  end repeat
  if not hit then error "notab"
end tell
OSA
if [ "$n" -eq 0 ]; then
  # no existing tab found -> (re)open the dashboard fresh in the default browser
  open "http://${URLMATCH}/" 2>/dev/null && echo "ken-refresh-site: no tab found -> reopened http://${URLMATCH}/ fresh"
else
  echo "ken-refresh-site: reloaded the 7810 tab in $n browser(s)"
fi