5

It's possible to get the current tab with xdotool:

# set focus to address on browser
xdotool search --onlyvisible --classname Navigator windowactivate --sync key F6

# copy address from browser tab to clipboard
xdotool search --onlyvisible --classname Navigator windowactivate --sync key Ctrl+c

# get off the focus from address from browser tab
xdotool search --onlyvisible --classname Navigator windowactivate --sync key F6

# delivery of clipboard content to variable
clipboard=`xclip -o -selection clipboard`

# clear clipboard
xsel -bc; xsel -c

# echo URL of active tab of active browser
echo $clipboard

Since xdotool is buggy and sometimes fires keys without stopping with the code above, I need another way to do the same, perhaps with wmctrl.

This is what I tried, using wmctrl on bash; it didn't work for me:

id=$(wmctrl -l | grep -oP "(?<=)(0x\w+)(?=.*Firefox)")

# set focus to address on browser
xdotool key --window $id "ctrl+l"

# copy address from browser tab
xdotool key --window $id "ctrl+c"

# delivery of clipboard content to variable
url=`xclip -o -selection clipboard`

# clear clipboard
xsel -bc; xsel -c

# echo URL of active tab of active browser
echo $url
Alfred.37
  • 110
  • 1
  • 19

3 Answers3

5

I wouldn't use xdotool for that - it is too fragile and error-prone. Use a proper browser automation tool instead such as Marionette. Install marionatte_driver python module:

pip3 install --user marionette_driver

Start a new instance firefox with --marionette option:

firefox --marionette

Use the following get_url.py:

#!/usr/bin/env python3

import marionette_driver

m = marionette_driver.marionette.Marionette()

m.start_session()
print(m.get_url())
m.delete_session()

Example:

$ ./get_url.py
https://unix.stackexchange.com/questions/629440/how-to-get-the-url-from-current-tab-of-firefox-by-help-of-wmctrl

You can save output of ./get_url.py to a variable using command substitution:

$ url="$(./get_url.py )"
$ echo $url
https://unix.stackexchange.com/questions/629440/how-to-get-the-url-from-current-tab-of-firefox-by-help-of-wmctrl/629447?noredirect=1#comment1177925_629447
Arkadiusz Drabczyk
  • 25,049
  • 5
  • 53
  • 68
  • @Alfred.37: see updated answer – Arkadiusz Drabczyk Jan 16 '21 at 16:43
  • If you like this answer please mark it as solved to let other users know your problem is solved. Also see [What should I do when someone answers my question?](https://unix.stackexchange.com/help/someone-answers) – Arkadiusz Drabczyk Jan 16 '21 at 20:48
  • You don't need 15 points to be able to accept the answer. See https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235 – Arkadiusz Drabczyk Jan 16 '21 at 20:56
  • Does it need to do any additional like open the FF browser on a special way before ? I dont get any output by this on Linux Debian/Ubuntu. – Alfred.37 Jan 26 '21 at 23:52
  • @Alfred.37: yes, you need, and I described it. – Arkadiusz Drabczyk Jan 27 '21 at 10:15
  • I am getting some erreors by installing the marionette driver. It can be the not working is related on this. I will try to chek. The errors are all path errors like follow: WARNING: The script structlog is installed in '/root/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. WARNING: The scripts diff-profiles, mozprofile and view-profile are installed in '/root/.local/bin' which is not on PATH. – Alfred.37 Jan 27 '21 at 12:16
  • @Alfred.37: 1. you don't need to use `pip3` as root 2. These are warnings, not errors and they don't matter here 3. How isn't it working for you? You need to be more specific. – Arkadiusz Drabczyk Jan 27 '21 at 15:32
1

Here is an alternative way to write

# Search for the visible Firefox window(s) and get its window ID
window_id=$(xdotool search --onlyvisible --class "firefox")

# Send the keyboard shortcut to open the URL bar, copy the URL to clipboard and then close the URL bar by sending the Escape key.
# The command is sent to the Firefox window with the specified ID using the --window option.
xdotool key --window $window_id --delay 20 --clearmodifiers ctrl+l ctrl+c Escape

# delivery of clipboard content to variable
clipboard=`xclip -o -selection clipboard`

# clear clipboard
xsel -bc; xsel -c

# echo URL of active tab of active browser
echo $clipboard
phnamonia
  • 11
  • 2
0

You can fix the xdotool related issue on your samle code. See follow sample code for fix the xdotool bug:

#!/bin/bash

while true; do

echo Get current_wid
current_wid=$(xdotool getwindowfocus) # Get wid of current window and save this value for go bac later
echo "$current_wid"
echo
echo

echo F5 senden
xdotool windowactivate --sync "$current_wid" key --clearmodifiers F5
echo
echo

echo Fix von aktivem Fenster
xdotool windowactivate "$current_wid" # Done, now go back to where we were
echo
echo

echo "sleep 1"
sleep 1
done

echo "Zum Beenden Enter drücken oder Ctl + C."; read -r
Alfred.37
  • 110
  • 1
  • 19