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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
#!/bin/bash
PROMPT_LINES=2
input_buffer=""
init_terminal() {
# Hide cursor to avoid flickering
tput civis
# Scroll enough for the prompt in case we're at the
# bottom of the screen
for i in $(seq 1 $PROMPT_LINES); do
echo
done
# Move cursor to the top of the room we just made
tput cuu $((PROMPT_LINES + 1))
# Save cursor position
tput sc
# Exclude prompt from scrollable area
tput csr 0 "$(($(tput lines) - PROMPT_LINES - 1))"
# Changing scrollable area moves cursor, so put it back
tput rc
}
restore_terminal() {
clear_status
clear_prompt
tput csr 0 "$(tput lines)"
tput rc
tput cnorm
}
eval_javascript_in_gnome_shell() {
json=$(mktemp)
busctl call --user --json=short \
org.gnome.Shell \
/org/gnome/Shell \
org.gnome.Shell \
Eval "s" "$1" > "$json"
result=$(jq '.data[0]' < "$json")
output=$(jq '.data[1] | select(. != null and . != "") | try fromjson catch .' < "$json")
rm -f "$json"
if [ "$result" = "false" ]; then
echo -e "\e[31m${output:1:-1}\e[0m" > /dev/stderr
return 1
fi
echo "$output"
return 0
}
eval_javascript_in_looking_glass() {
# encode the text so we can side-step complicated escaping rules
ENCODED_TEXT=$(echo -n "$1" | hexdump -v -e '/1 "%02x"')
eval_javascript_in_gnome_shell "
const GLib = imports.gi.GLib;
Main.createLookingGlass();
const results = Main.lookingGlass._resultsArea;
Main.lookingGlass._entry.text = '${ENCODED_TEXT}'.replace(/([0-9a-fA-F]{2})/g, (_, h) => String.fromCharCode(parseInt(h, 16)));
Main.lookingGlass._entry.clutter_text.activate();
GLib.timeout_add(GLib.PRIORITY_DEFAULT, 125, () => {
const index = results.get_n_children() - 1;
if (index < 0)
return;
const resultsActor = results.get_children()[index];
const output = \`\${resultsActor.get_children()[1].get_children()[0].text}\${resultsActor.get_children()[1].get_children()[1].get_children()[0].text}\`;
Main.lookingGlass._lastEncodedResult = output.split('').map(char => char.charCodeAt(0).toString(16).padStart(2, '0')).join('');
});
" > /dev/null
if [ $? -ne 0 ]; then
return
fi
sleep .250
OUTPUT=$(echo -e $(eval_javascript_in_gnome_shell 'Main.lookingGlass._lastEncodedResult;' | sed 's/\([[:xdigit:]][[:xdigit:]]\)/\\x\1/g'))
if [ -z "$OUTPUT" ]; then
echo -e "\e[31mCould not fetch result from call\e[0m" > /dev/stderr
return
fi
eval_javascript_in_gnome_shell "delete Main.lookingGlass._lastEncodedResult;" > /dev/null
echo ">>> $1"
echo "${OUTPUT}"
}
jump_to_prompt() {
# Move to the bottom of the terminal
tput cup $(($(tput lines) - PROMPT_LINES)) 0
}
clear_prompt() {
jump_to_prompt
tput el
}
jump_to_status() {
# Move to just below the prompt
tput cup $(($(tput lines) - PROMPT_LINES + 1)) 0
}
clear_status() {
jump_to_status
tput el
}
print_status_line() {
jump_to_status
echo -ne "Type quit to exit, ^G to inspect, ^L to clear screen"
tput rc
}
clear_screen() {
clear
ask_user_for_input
}
ask_user_for_input() {
# Save cursor position
tput sc
clear_prompt
tput cnorm
read -i "$READLINE_LINE" -p ">>> " -re input_buffer
STATUS="$?"
tput civis
[ $STATUS != 0 ] && exit
if [ "$input_buffer" = "quit" -o "$input_buffer" = "q" -o "$input_buffer" = "exit" ]; then
exit
fi
# Save input to history
history -s "$input_buffer"
# Move cursor back to saved position before output
tput rc
}
quit_message() {
print_status_line
ask_user_for_input
}
load_history() {
while IFS= read -r line; do
history -s "$line"
done < <(eval_javascript_in_gnome_shell 'Main.lookingGlass._history._history.join("\n");' | jq -r '. | select(. != null and . != "") | tostring')
}
check_for_unsafe_mode() {
unsafe_mode=$(eval_javascript_in_gnome_shell 'global.context.unsafe_mode')
if [ "$unsafe_mode" != "true" ]; then
echo -e "Please enable unsafe-mode in the Flags tab of looking glass." > /dev/stderr
exit
fi
}
eval_autocomplete_javascript() {
ENCODED_TEXT=$(echo -n "$1" | hexdump -v -e '/1 "%02x"')
OUTPUT=$(eval_javascript_in_gnome_shell '
const AsyncFunction = async function () {}.constructor;
const command = `
const JsParse = await import("resource:///org/gnome/shell/misc/jsParse.js");
function getGlobalState() {
const keywords = ["true", "false", "null", "new"];
const windowProperties = Object.getOwnPropertyNames(globalThis).filter(
a => a.charAt(0) !== "_");
const headerProperties = JsParse.getDeclaredConstants(commandHeader);
return keywords.concat(windowProperties).concat(headerProperties);
}
const commandHeader = '"'"'const {Clutter, Gio, GLib, GObject, Meta, Shell, St} = imports.gi; const Main = await import("resource:///org/gnome/shell/ui/main.js"); const inspect = Main.lookingGlass.inspect.bind(Main.lookingGlass); const it = Main.lookingGlass.getIt(); const r = Main.lookingGlass.getResult.bind(Main.lookingGlass);'"'"';
const text="'${ENCODED_TEXT}'".replace(/([0-9a-fA-F]{2})/g, (_, h) => String.fromCharCode(parseInt(h, 16)));
const completions = await JsParse.getCompletions(text, commandHeader, getGlobalState());
return {
"completions": completions[0],
"attrHead": completions[1]
};`;
AsyncFunction(command)();
')
if [ $? = 1 ]; then
echo fail
fi
echo "$OUTPUT"
}
autocomplete() {
RESULT="$(eval_autocomplete_javascript "$READLINE_LINE")"
COMPLETIONS="$(echo "$RESULT" | jq '.completions[]' | sed 's/\"//g')"
ATTR_HEAD=$(echo "$RESULT" | jq '.attrHead' | sed 's/\"//g')
N_COMPLETIONS=$(echo "$COMPLETIONS" | wc -w)
if [ $N_COMPLETIONS = 0 ]; then
return
elif [ $N_COMPLETIONS = 1 ]; then
TO_ADD=$(echo $COMPLETIONS | sed s/$ATTR_HEAD//)
READLINE_LINE+="$TO_ADD"
(( READLINE_POINT += $(echo "$TO_ADD" | wc -c) ))
else
tput rc
echo
echo "$COMPLETIONS" | pr -T -2 -o 4
ask_user_for_input
fi
}
inspect() {
eval_javascript_in_gnome_shell '
const AsyncFunction = async function () {}.constructor;
delete Main.lookingGlass._lastInspection;
const command = `
const Main = await import("resource:///org/gnome/shell/ui/main.js");
const LookingGlass = await import("resource:///org/gnome/shell/ui/lookingGlass.js");
Main.lookingGlass._inspector = new LookingGlass.Inspector(Main.lookingGlass);
const inspector = Main.lookingGlass._inspector;
inspector.connectObject("target", (i, obj, stageX, stageY) => {
let command = "inspect(" + Math.round(stageX) + ", " + Math.round(stageY) + ")";
Main.lookingGlass._lastInspection = command;
},
"closed", () => {
delete Main.lookingGlass._inspector;
});
`;
AsyncFunction(command)();
'
while sleep .250; do
finished=$(eval_javascript_in_gnome_shell 'Main.lookingGlass._inspector? false : true')
last_inspection=$(eval_javascript_in_gnome_shell 'Main.lookingGlass._lastInspection' | jq -r)
[ "$finished" = "true" ] && break
[ -n "$last_inspection" ] && break
done
tput rc
[ -n "$last_inspection" ] && eval_javascript_in_looking_glass "$last_inspection"
eval_javascript_in_gnome_shell 'delete Main.lookingGlass._lastInspection; Main.lookingGlass.setBorderPaintTarget(null);' > /dev/null
ask_user_for_input
}
main_loop() {
bind -x '"\a"':inspect 2> /dev/null
bind -x '"\t"':autocomplete 2> /dev/null
bind -x '"\f"':clear_screen 2> /dev/null
while true; do
ask_user_for_input
eval_javascript_in_looking_glass "$input_buffer"
done
}
check_for_unsafe_mode
trap 'quit_message' SIGINT
trap restore_terminal EXIT
init_terminal
load_history
print_status_line
main_loop
restore_terminal
|