Can I dump the source of a live Chromium window to a string? - bash

I'm using xdotool to open a Chromium browser and click a few things , but I'm wondering if I can get the source code of the live window it's using and dump it into a string to check if the source contains a particular string.
I know a wget can get me the source of a window , but what I want is the window that's already open as it will contain the string I want, but the wget option may not.
How can I do that?

If ctrl+U (show source) can be used, it will save your time, but if you need to inspect live HTML, try this xdotool script:
#!/usr/bin/env xdotool
search --sync --onlyvisible --name chrome
windowactivate --sync
key Ctrl+Shift+C
sleep 0.5
key Down
sleep 0.5
key ctrl+F
sleep 1
type "What you try to find"
key Return
Save as 'xdotool_script', then run it like you executing bash script
$ chmod +x xdotool_script
$ ./xdotool_script`

Related

How do I send keys programmatically (simulated key typing) in Linux Mint?

I have passwords saved in a password manager (KeePass running on Wine) on Linux Mint.
The login site that I have does not allow pasting the password. The password is a 40 random characters and special characters which makes it difficult and tedious to type manually using keyboard.
I want to copy the password from the KeePass and then run some script (like AutoIt, AutoIt is not supported on Linux Mint) that reads the clipboard data (the copied password) and sends character by character (simulate typing by keyboard) to the password field in the browser.
How do I achieve this on Linux Mint 20? If AutoIt was working on Linux Mint I would have been happy.
I found xdotool but cannot figure out a way to set the focus to Firefox window.
I found the solution
pass=`clipit -c`
xdotool search --sync --onlyvisible --name "My Bank" windowactivate
xdotool type --delay 500 $pass

Creating a Service with automator, caffeinate

First of all, sorry for my english, not a native speaker.
Since I'm a bit fed up opening Termninal and ^C I want to create a shortcut for activating caffeinate -d on my Mac OS X 10.11
I've been trying a simple Automator Service but with my noob skills it doesn't work.
This is the settings:
I chose Service as type of document
Library->Utilities->Run Shell Script
Service receives: no input
in: any application
Shell: /bin/bash
Pass input: to stdin
CAFFEINATECHECK=`ps | grep caffeinate | cut -d ' ' -f7,8`
if [ $CAFFEINATECHECK == 'caffeinate -d' ]; then
killall caffeinate
else
caffeinate -d
fi
Problem is that such a short scrip keeps running with no response and I have to stop it after a minute
I hope someone could give me any tips necessary
I got the solution in a duplicate on Ask Different
In short, just replacing mine code with a more elegant command lines:
if [[ $(pgrep caffeinate) == "" ]]; then
caffeinate -d &
else
pkill caffeinate
fi
The only change I made inside user3439894's solution was to remove the '&' (usually useful at the end of a command line to run it in background) because apparently Automator doesn't work well with it (shame I can't remember where I read/heard that..!) .
After saving the Service I just had to
Open System Preferences Keyboard->Shortcuts->Services
Bind my shortcut with my saved workflow found under ▼General in the right pannel
This way a cog gear wheel ⚙ appears on the menubar when caffeinate shortcut is activated and spins till I deactive it .

Send xdotool keypresses/clicks to multiple windows simultaneously

I want to be able to run a bash script, which sends keypresses and clicks to multiple windows. This does NOT necessarily have to be done with xdotool, if there is an alternative I would be happy to use it. Preferably it would work with bash though.
So, if I had three windows, all of which I wanted to send the "w" key to, I would need to do something like set the active window as each in quick succession followed by sending the w key.
I want to be able to do it simultaneously, so this will not work very well.
You would need to know the window names beforehand, i.e before running the script .. You could do it this way
#!/bin/bash
for winname in "$#"
do
xdotool type --window $(xwininfo -int -name "$winname" | egrep -o 'Window id: [0-9]+' | cut -d ' ' -f 3) w
done
Output
$./script1.bash "*Untitled 1 - Mousepad" "*(Untitled)" "[No Name] + - GVIM"
Above script sends w keystroke to all window names that are specified on the command line.
PS: for me i have 3 editors opened in the following order: Mousepad, Leafpad and GVIM
You can use xdotool to do the heavy lifting for you.
It will search for a window using name, class or classname (use xprop or xwininfo to get the details).
Here is an example that will output "Hello World" into all running occurrences of a libreoffice writer document.
#!/bin/bash
for pid in $(xdotool search --class "libreoffice-writer")
do
xdotool type --window $pid 'Hello World'
done

pager (less) -- get current scroll position?

I am scripting the display of the output of a script (well, it is just the program git diff) with tmux: Once a filesystem change is detected the shell script executes tmux send-keys q enter C-l "git diff" enter which has it effectively refresh the git diff view.
You might consider this similar to functionality provided by iTerm's coprocesses.
Problem is, I want it on refresh to scroll back to the same position that it was in.
One of the reasons for using tmux is that the window is actually a totally normal and interactive terminal session that can be interacted with as normal to scroll around to look at the full output.
But I want to obtain the scroll position somehow.
Suppose I want to actually do computation on the text content of the terminal window itself, exactly like iTerm2's coprocess does, but so that I can use it on Linux (over ssh). Does tmux provide this ability?
I'm unsure about capturing this with a script, but less -N will show line numbers.
And -jn or --jump-target=n can jump to a location.
About iTerm's coprocesses,
tmux has a command pipe-pane that can be used to pipe the input and output of a shell command to the output and input of a target pane specified by -t.
So if I have a shell program, ~/script.sh for example:
#!/usr/bin/env bash
while read line; do
if [[ "$line" = "are you there?"* ]]; then
echo "echo yes"
fi
done
Note that read line will read every line printed to the pane, i.e. the prompt as well.
I can connect its stdin and stdout to pane 0 in my-session:my-window like so:
tmux pipe-pane -IO -t my-session:my-window.0 "~/script.sh"
and then at the prompt, type echo are you there?, and it responds:
$ echo are you there?
are you there?
$ echo yes
yes
Be careful using -IO together as it can easily cause a feedback loop (I had to kill the tmux server a few times while experimenting with this feature, lol).

How to set iterm2 tab title to that of a running tmux session name?

i know tmux display-message -p '#S' will display the current tmux session name, but i donno how to set the current tmux session name to the iterm2 tab title?
This would really help me to distinguish the various tmux sessions that i am running concurrently and jump to the correct tab rightaway.
add these to your ~/.tmux.conf:
set-option -g set-titles on
set-option -g set-titles-string "#{session_name} - #{host}"
My workflow is usually centered around panes, and I don't use tmux, so I used a slight variation of #mislav answer:
set_terminal_tab_title() {
print -Pn "\e]1;$TABTITLE:q\a"
}
precmd_functions=($precmd_functions set_terminal_tab_title)
I threw that into my zshrc; then, in each pane, I export TABTITLE='FOO'. That way, when I switch panes, I get the title I want on the tab.
Stick this in your ~/.zshrc:
set_terminal_tab_title() {
print -Pn "\e]1;$1:q\a"
}
indicate_tmux_session_in_terminal() {
set_terminal_tab_title "$(tmux display-message -p '#S')"
}
precmd_functions=($precmd_functions indicate_tmux_session_in_terminal)
precmd_functions is an array that in zsh contains the list of functions to call prior to showing the prompt. If you add your own function to the list, it will get called whenever the prompt is shown, making it a good place to periodically update the terminal tab title.
Bash Version to display Hello World as a title:
echo -ne "\033]0; Hello World \007"
And if you want title refreshed each time bash print your prompt:
export PROMPT_COMMAND='echo -ne "\033]0;${USER}#${HOSTNAME%%.*}: ${PWD/#$HOME/~}\007"'
Found it on http://hints.macworld.com/article.php?story=20031015173932306

Resources