aboutsummaryrefslogtreecommitdiff
path: root/.local/share/mode/reload.d/brave
blob: beb0a91b88ba9f3554d49fec09b323d11640b15a (plain)
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
#!/bin/sh
# WIP
browser='brave'
driver='chromedriver'
devtools_addr='localhost:9222'
url_base='http://localhost:9515'

command -v "$browser" > /dev/null && command -v "$driver" > /dev/null
[ $? -ne 0 ] && exit 0

get() { curl -s -o - -X GET "$@" ; }
post() { curl -s -o - -X POST -H 'Content-Type: application/json' -d @- "$@" ; }
delete() { curl -s -X DELETE "$@" > /dev/null ; }

$driver --silent > /dev/null &
sleep 0.1 # (wait for chromedriver to start server)

create_session() {
	args="$(jq --raw-input --slurp 'split("\n") | map(select(. != ""))' << EOF
new-window
headless=new
EOF
	)"
	jq -nc \
		--arg name "$browser" \
		--arg debugger "$devtools_addr" \
		--argjson args "$args" \
	'{
		desiredCapabilities: {
			"browserName": $name,
			"goog:chromeOptions": {
				binary: $name,
				args: $args,
				debuggerAddress: $debugger
			},
		}
	}' |\
	post "$url_base/session" |\
	jq --raw-output '.sessionId'
}

navigate() {
	jq -nc --arg url "$1" '{"url": $url}' |\
	post "$url_base/url" > /dev/null
}

script() {
	jq -nc --arg script "$1" '{"script": $script, args: []}' |\
	post "$url_base/execute/sync" > /dev/null
}

create_tab() {
	handle="$(jq -nc '{"type": "tab"}' |\
	post "$url_base/window/new" |\
	jq --raw-output '.value.handle')"
	jq -nc --arg handle "$handle" '{"name": $handle}' |\
	post "$url_base/window" > /dev/null
}

# create session
session_id="$(create_session)"
url_base="$url_base/session/$session_id"
create_tab

# set chrome theme
navigate 'brave://settings'
script "chrome.send(\"setBraveThemeType\", [$([ "$mode" = "light" ] && echo "2" || echo "1")])"

# todo: reload theme extension
# navigate 'brave://extensions'
# - run `chrome.developerPrivate.loadUnpacked()` (TODO: how to handle inputting file path non-interactively)
# TODO: how to use chromedriver from script (preferably without nodejs+npm or python3+pip dependencies?)

# cleanup
delete "$url_base/window"
delete "$url_base"
kill $(jobs -rp)
wait $(jobs -rp)

# reference: https://w3c.github.io/webdriver/