1

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.

2 Answers 2

2

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Avoiding to use the clipboard could probably improve the security of this solution.
0

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"

1 Comment

Avoiding to use the clipboard could probably improve the security of this solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.