如何通过 bash 或 python 从最活跃的 firefox 浏览器选项卡 url 中获取 url,来自焦点中的 firefox?

How to get url from most active firefox browser tab url by bash or python, from firefox which is in focus?

如何通过 bash 或 python 从 firefox 中最活跃的标签中获取 url?

下面的部分解决方案,展示了如果只打开一个 Firefox window 如何做到这一点。 (如果打开了多个 FF windows,则此部分解决方案无法执行此操作。此部分解决方案仅检查一个 运行 FF window 中最活跃的选项卡,或者如果有更多比一个 ff window 都打开,最活跃的选项卡,从第一个开始 FF window.).

部分Python解决方案: 导入 json、lz4.block、glob、子进程

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()

备注:

寻求 bash 或 Python 中的解决方案,但不是需要安装基于 Javascript、Selenium 或 Brotab 且不使用不是 treadsafe 越野车 xdotool。

您可以通过关注获取最活跃的浏览器选项卡的地址,可能存在潜在不安全的方式:

xdotool search "Navigator" windowactivate --sync key --clearmodifiers ctrl+l ctrl+c

clipboard=$( xsel -ob )
echo "$clipboard"