Skip to content

Instantly share code, notes, and snippets.

@msanders
Last active August 30, 2024 17:16
Show Gist options
  • Save msanders/365b06cbc2f9c63981f4ab88234cd330 to your computer and use it in GitHub Desktop.
Save msanders/365b06cbc2f9c63981f4ab88234cd330 to your computer and use it in GitHub Desktop.
Script to remove all cookies from Safari except for a list of domains

Installation

First, install CookiesTool to a location of your choice (by default the script looks for ~/bin/CookiesTool).

Then download the script below.

curl --proto "=https" --tlsv1.2 "https://gist.githubusercontent.com/msanders/365b06cbc2f9c63981f4ab88234cd330/raw/safari_clear_cookies" -o /path/to/safari_clear_cookies
chmod u+x /path/to/safari_clear_cookies

To automatically invoke when Safari exits, you can use add this to Hammerspoon:

local eventTable <const> = {
  [hs.application.watcher.terminated] = function()
    hs.task.new(
      os.getenv("HOME")
      ..
      "/.config/hammerspoon/scripts/safari_clear_cookies"
    , nil):start()
  end
}

watcher = hs.application.watcher.new(function(app, event, _)
  if app:bundleID() == "com.apple.Safari" then
    local fn = eventTable[event]
    if fn ~= nil then fn() end
  end
end):start()

Homebrew environment variables can be passed to Hammerspoon via:

launchctl setenv HOMEBREW_PREFIX "$HOMEBREW_PREFIX"

Or via shell config.

Requirements

  • jq
brew install jq

Clear Safari Cache

To clear Safari cache, run:

rm -rf "$HOME/Library/Containers/com.apple.Safari/Data/Library/Caches/com.apple.Safari/"
rm -rf "$HOME/Library/Caches/com.apple.Safari/"

Caveats

Currently incompatible with tab grouping.

License

This is made available under the terms of the MIT license. For a copy, see https://opensource.org/licenses/MIT.

#!/bin/sh
# Script to remove all cookies from Safari except for a list of domains.
# https://gist.github.com/msanders/365b06cbc2f9c63981f4ab88234cd330
# License: MIT
set -o errexit -o nounset
# Path to CookiesTool
# https://github.com/Lessica/CookiesTool
COOKIES_TOOL="$HOME/bin/CookiesTool"
# Adjust PATH for Homebrew.
if [ -z "$HOMEBREW_PREFIX" ]; then
PATH="$HOMEBREW_PREFIX/bin:$HOMEBREW_PREFIX/sbin${PATH+:$PATH}"
fi
ALLOWLIST='
apple\.com
icloud\.com
youtube\.com
'
COOKIES_FILE="$HOME/Library/Containers/com.apple.Safari/Data/Library/Cookies/Cookies.binarycookies"
running_app_bundle_ids() {
osascript -l JavaScript <<EOF
Application("System Events")
.processes.whose({ backgroundOnly: false })()
.map(x => x.bundleIdentifier())
.join("\n");
EOF
}
main() (
if running_app_bundle_ids | grep -Fx com.apple.Safari; then
echo "Safari is currently running; aborting." >&2
exit 1
fi
tmp_dir="$(mktemp -d)"
trap 'nohup rm -rf "$tmp_dir" >/dev/null 2>&1 &' EXIT HUP INT QUIT TERM
cookie_bin_permissions="$(stat -f "%Lp" "$COOKIES_FILE")"
cookies_json_in_path="$tmp_dir/cookies.in.json"
cookies_json_out_path="$tmp_dir/cookies.out.json"
cookies_bin_out_path="$tmp_dir/cookies.out.binarycookies"
"$COOKIES_TOOL" "$COOKIES_FILE" --convert edit-this-cookie \
--output "$cookies_json_in_path"
domain_patterns="$(
printf "%s" "$ALLOWLIST" |
tail -n +2 |
xargs -I{} printf '^[^\\.]*\\.{}$\n' |
paste -sd '|' -
)"
jq -c --arg p "$domain_patterns" '[.[] | select(.domain | test($p))]' \
"$cookies_json_in_path" >"$cookies_json_out_path"
lc_prev="$(jq length "$cookies_json_in_path")"
lc_new="$(jq length "$cookies_json_out_path")"
"$COOKIES_TOOL" "$cookies_json_out_path" --convert binarycookies \
--output "$cookies_bin_out_path"
chmod "$cookie_bin_permissions" "$cookies_bin_out_path"
mv -f "$cookies_bin_out_path" "$COOKIES_FILE"
printf "Removed %d cookies.\n" $((lc_prev - lc_new))
)
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment