aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/browser-refresh
blob: c1410c4cb41b92996be3b66565a4acef94f242fd (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env bash

# set keyboard
keyboard=10

# set target browser and title
browser=$1
title=$2

# user ps to check if program is already running. See https://unix.stackexchange.com/a/48511
program="$(basename "$0")";
running=$(ps h -C "$program" | grep -wv "^$$" | wc -l);
[[ $running > 2 ]] && exit;

# browser we would like to live reload
target_window=$(

  comm -12 <(xdotool search --name  $title  | sort) <(xdotool search --class $browser | sort) | head -n 1

); # named pipe comparison comm -12 /dev/fd/63 /dev/fd/63. See https://unix.stackexchange.com/questions/254820/xdotool-how-to-search-for-window-by-title-and-class-with-different-patterns-si

# active window where the script was activated
initial_window=$(xdotool getwindowfocus)

# echo target window id for debugging purposes
#echo $target_window;

# bail on empty window id
if [ -z $target_window ]; then
  notify-send "No match for browser $browser with title $title!";
  exit 1;
fi

# activate target window
xdotool windowactivate $target_window;

# declare the variable i as an interger
declare -i i=0;

# disable keyboard at the start of search
xinput --disable $keyboard;

while true; do

    # bail out if there are too many searches
    if [ "$i" -gt 10 ]; then

      notify-send "No match for browser $browser with title $title!";
      break;

    fi

    # set index i to increment by 1
    i+=1

    # echo the amount of interations for debugging purposes
    echo "Search Iteration: $i";

    # get window focus
    focused=$(xdotool getwindowfocus);

    while [ "$target_window" -eq "$focused" ]; do

      xdotool key "ctrl+r";

      # echo received window id for debugging purposes
      #echo $target_window;

      # break out of both while loops
      break 2;

    done;

done


# declare the variable j as an interger
declare -i j=0;

while true; do

  while [ "$initial_window" -ne "$focused" ]; do

    xdotool windowactivate $initial_window;
    focused=$(xdotool getwindowfocus);

  done;

  j+=1

  # bail after a few checks
  if [ "$j" -eq 10 ]; then
    echo "Return Iteration: $j";
    break;
  fi

done

# enable keyboard at the end of the search
xinput --enable $keyboard;

exit 0;