c84a6aebb73c13d659511c3771c6c4c4d1970387
[distro-setup] / filesystem / usr / local / bin / abrowser
1 #!/bin/bash
2
3 # https://stackoverflow.com/a/2108540
4 tmp=:$PATH:
5 del=/usr/local/bin
6 tmp=${tmp//:$del:/:}
7 tmp=${tmp%:}
8 tmp=${tmp#:}
9 PATH=$tmp
10
11 # -allow-downgrade good enough?
12 #rm -f {/p/c/firefox-main-profile,/p/c/firefox-main-profile,/p/c/firefox-vpn2-profile,/mnt/z/firefox-vpn-profile}/compatibility.ini
13
14 # --allow-downgrade
15 # ^ useful option for when the browser refuses to run, but it always
16 # causes a new browser window to open, even if normally it would open a
17 # new tab
18
19
20 tmpf=$(mktemp)
21 i3-msg -t get_tree | jq -e '.nodes[].nodes[].nodes[].nodes | [.[]] + ( [.[].nodes[]]) | .[] | select(.window_properties.class=="abrowser") | .id' | sort >$tmpf
22
23 # prefer abrowser
24 if type -P abrowser &>/dev/null; then
25 abrowser "$@" &
26 else
27 firefox "$@" &
28 fi
29
30 # .5 was too fast
31 sleep 1
32 # debug
33 #printf "%s\n" "$*" >> /tmp/a
34 if (( $# == 0 )) && ! i3-msg -t get_tree | jq --stream -r 'select(.[1]|scalars!=null) | "\(.[0]|join(".")): \(.[1]|tojson)"' | grep 'marks.0: "abrowser"$' &>/dev/null; then
35 # explaining this jq nonsense. when the abrowser window starts, it
36 # might be in a vertical split container, and then it is nested down
37 # another level. the best way I could find to look in both levels was
38 # to get both, then combine them with + (and you have to turn them
39 # into a single array instead of a list of arrays with [.[]], or else
40 # it will add the arrays a bunch of times and give several results.
41 # comm gives us just the new id.
42 id=$(i3-msg -t get_tree | jq -e '.nodes[].nodes[].nodes[].nodes | [.[]] + ( [.[].nodes[]]) | .[] | select(.window_properties.class=="abrowser") | .id' | comm -23 - $tmpf | head -n1)
43 rm -f $tmpf
44 if [[ $id ]]; then
45 i3-msg "[con_id=$id] mark abrowser"
46 fi
47 fi
48
49 wait