如何从最活跃的firefox浏览器选项卡url的bash或python,从firefox这是焦点? &



如何从最活跃的选项卡获取url从firefox这是在焦点,通过bash或python ?

下面的部分解决方案,说明如何做到这一点,如果只打开一个Firefox窗口。(这部分解决方案不能做到这一点,如果多个FF窗口打开。这是部分解决方案,只从一个正在运行的FF窗口检查最活跃的选项卡,或者如果打开了多个FF窗口,则从第一个启动的FF窗口检查最活跃的选项卡。

部分Python解决方案:导入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()

备注:

在bash或Python中寻求解决方案,但不需要安装浏览器插件,基于Javascript, Selenium或Brotab,而不使用不安全的错误xdotool。

这个脚本的更优化的版本

# 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"

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

xdotool search "Navigator" windowactivate --sync key --clearmodifiers ctrl+l ctrl+c
clipboard=$( xsel -ob )
echo "$clipboard"

最新更新