Reputation: 159
How to get url from most active tab from firefox which is in focus, by bash or python ?
The follow partly solution, show how to do it if only one Firefox window are opened. ( This partly solution are not able to do this if more than one FF windows are open. This partly solution, check only the most active tab from only one running FF window or if more than one ff window are open, the most active tab, from first started FF window.).
Partly Python solution: import json, lz4.block, glob, subprocess
wins = subprocess.run('wmctrl -l', shell=True, stdout=subprocess.PIPE)
title = next(ln for ln in wins.stdout.decode('utf-8').splitlines() if 'Mozilla Firefox' in ln)
for f in glob.glob('.mozilla/firefox/*default*/sessionstore-backups/recovery.jsonlz4'):
j = json.loads(lz4.block.decompress(open(f, 'rb').read()[8:]))
for win in j['windows']:
for tab in win['tabs']:
for entry in tab['entries']:
if entry['title'] in title:
print(entry['url'])
exit()
Remark:
A solution in bash or Python is sought, but not one that requires the installation of a browser addon, based on Javascript, Selenium or Brotab and without to use the not treadsafe buggy xdotool.
Upvotes: 1
Views: 957
Reputation: 21
a more optimized version of this script
# 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
clipboard=$( xsel -ob )
echo "$clipboard"
Upvotes: 2
Reputation: 159
You can get the adress of most active browser tab on follow, possible potential unsecure way:
xdotool search "Navigator" windowactivate --sync key --clearmodifiers ctrl+l ctrl+c
clipboard=$( xsel -ob )
echo "$clipboard"
Upvotes: 0