summary refs log tree commit diff
path: root/bin/chrome-extension-dl
blob: a40b51971c218a9d2b62c8caeb0dde22be1474d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env sh
# Download extensions from chrome webstore
#
# source: https://gitlab.com/snippets/1888145

[ -z "$EXTENSIONS_DIR" ] && EXTENSIONS_DIR="${extensions_dir:=${XDG_DATA_HOME:=$HOME/.local/share}/chrome-extensions}"

magenta="\033[35;1m"
nc="\033[m"

die() { printf 'Error: %s\n' "$1" >&2; exit 1; }

parse_url(){
    extension_id="$(echo "$1" | grep -oP '[_a-zA-Z0-9]{30,}')"
    download_extension
}

download_extension(){
    extension_file="$(mktemp "${TMPDIR:-/tmp/}$extension_id.XXXXXXXXXXXX.zip")"
    printf "\nDownloading extension %s\n" "$extension_id"
    curl -fsSL "https://clients2.google.com/service/update2/crx?response=redirect&prodversion=49.0&x=id%3D$extension_id%26installsource%3Dondemand%26uc" --output "$extension_file" || die "Unable to download extension"
    extract_extension
}

extract_extension(){
    nohup rm -fr "$extensions_dir/${extension_id:?}" > /dev/null 2>&1 ||:
    mkdir -p "$extensions_dir/$extension_id"
    nohup unzip -q "$extension_file" -d "$extensions_dir/$extension_id" > /dev/null 2>&1
    printf "\n Extension installed to directory %s\n" "$extensions_dir/$extension_id"
    install_help
}

update_extensions(){
    cd "$extensions_dir" || die "Extensions directory does not exist"
    for extension in *; do
        if [ -d "$extension" ]; then
            printf "\nUpdating extension %s\n" "$extension"
            extension_id="$extension"
            download_extension
        fi
    done
}

general_help(){
    echo -e "
    ${magenta}NAME${nc}
    $(basename "$0") - install chrome extensions

    ${magenta}SYNOPSIS${nc}
    $(basename "$0") [webstore url]
    $(basename "$0") update

    ${magenta}DESCRIPTION${nc}
    $(basename "$0") Download extensions from chrome webstore.

    set EXTENSIONS_DIR to change extensions folder."
}

install_help(){
    echo -e "
    ${magenta}Install Guide:${nc}
    1. Visit chrome://extensions in your browser.
    2. Ensure that the Developer mode checkbox in the top right-hand corner is checked.
    3. Click Load unpacked extension... to pop up a file-selection dialog.
    4. Navigate to $extensions_dir/, and select it."
}

main(){
    if [ "$#" -eq 0 ]; then
        general_help && install_help
    else
        for input in "$@"; do
            shift
            case "$input" in
                -h|--h*) general_help && install_help ;;
                u*) update_extensions ;;
                *)  parse_url "$input" ;;
            esac
        done
    fi
}

main "$@"