old*_*aur 5 command-line firefox
我正在运行 Mozilla Firefox 54.0。
鉴于已经打开的 Firefox 会话和多个打开的选项卡,有没有办法通过命令行提取当前活动的选项卡(我正在查看的选项卡)?
我在命令行参数列表或Mozilla 开发人员页面上找不到任何内容。
我的问题与这个问题不同,因为它既不适合我的预期方式,也不想要所有选项卡;我想要一个特定的选项卡,我正在查看的选项卡。
有人有想法吗?
有没有办法与正在运行的 Firefox 实例交互?
谢谢阅读
编辑:解决方案:
import json
f= open('~/.mozilla/firefox/RANDOM.default/sessionstore-backups/recovery.js' )
jdata = json.loads(f.read())
f.close()
CurrentTab = jdata.get("windows")[0].get("tabs")[jdata["windows"][0]["sel??ected"]-1].get("entr??ies")[HistLen-1].get??("url")
while
HistLen = len(jdata.get("windows")[0].get("tabs")[jdata["windows"][0][??"selected"]-1].get("??entries"))
Run Code Online (Sandbox Code Playgroud)
这HistLen是必要的,因为否则我总是得到一些我以前在该选项卡中打开的旧页面。
谢谢阅读
这里提供了一个解决方案,它是sed和的组合python2。这是它的更清晰的版本:
sed -n "$(
python2 <<< $'import json
f = open("/home/username/.mozilla/firefox/RANDOM.default/sessionstore-backups/recovery.js", "r")
jdata = json.loads(f.read())
f.close()
print str(jdata["windows"][0]["selected"])')p" <(python2 <<< $'import json
f = open("/home/username/.mozilla/firefox/RANDOM.default/sessionstore-backups/recovery.js", "r")
jdata = json.loads(f.read())
f.close()
for win in jdata.get("windows"):
for tab in win.get("tabs"):
i = tab.get("index") - 1
print tab.get("entries")[i].get("url")'
)
Run Code Online (Sandbox Code Playgroud)
它使用的文件是:
/home/username/.mozilla/firefox/RANDOM.profile/sessionstore.js
Run Code Online (Sandbox Code Playgroud)
在更新的版本中,您应该将其更改为:
/home/username/.mozilla/firefox/RANDOM.default/sessionstore-backups/recovery.js
Run Code Online (Sandbox Code Playgroud)
请注意,此文件每 15 秒重新生成一次,因此在窗口立即更改后,它不会为您提供正确的 URL,您必须等待几秒钟。
在第一部分中,它查找活动选项卡的 ID,该 ID 介于 1 到打开选项卡计数之间。假设是“3”,对应这个目的的代码是:
str(jdata["windows"][0]["selected"])
Run Code Online (Sandbox Code Playgroud)
接下来,它返回 URL 列表(所有打开的选项卡)并将其提供给stdinof sed:
for win in jdata.get("windows"):
for tab in win.get("tabs"):
i = tab.get("index") - 1
print tab.get("entries")[i].get("url")
Run Code Online (Sandbox Code Playgroud)
所以我们正在做类似的事情:
sed -n 3p <<< "URL1
URL2
URL3"
Run Code Online (Sandbox Code Playgroud)
这将我们带到“URL3”。