我想通过一些 messenger 的 api 机器人(例如 telegram 机器人)发送命令并获取特定选项卡的响应(链接到某个网站),但我不知道如何实现有关取出 Firefox 选项卡的那部分
有没有不那么复杂的方法来做到这一点?请给出一些建议,如果这是一个愚蠢的问题,请原谅。提前致谢。
答案1
您可以使用 Python 通过这个相当不优雅的解决方案来完成此任务:
#!/usr/bin/env python3
"""
Copyright (c) 2018 Helio Machado <0x2b3bfa0>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import json
from pathlib import Path
from lz4.block import decompress
def load_recovery(path):
"""
Loads the recovery.jsonlz4 file from the path specified and
parses the json data to a native structure. This file contains
the current tabs of the browser.
"""
with open(str(path), "rb") as recovery_file:
assert recovery_file.read(8) == b"mozLz40\0"
compressed_data = recovery_file.read()
json_data = decompress(compressed_data)
recovery = json.loads(json_data.decode())
return recovery
def recovery_path():
"""
Get the path to the recovery.jsonlz4 file.
"""
firefox_path = Path.home() / ".mozilla" / "firefox"
profile_path = list(firefox_path.glob("*.default"))[0]
session_path = profile_path / "sessionstore-backups"
return session_path / "recovery.jsonlz4"
windows = load_recovery(recovery_path())["_closedWindows"]
tabs = [tab for window in windows for tab in window["tabs"]]
urls = [tab["entries"][0]["originalURI"] for tab in tabs]
# Here you go!
print("\n".join(urls))
执照: GPLv2
这应该是一个很好的起点;即使你的问题并不像你想象的那么愚蠢,我认为你应该更加努力地寻找解决方案,并将你的研究结果与问题一起发布。
我将不再维护该问题并将其保留为社区 Wiki。