import subprocess
import time
import re
def get_firefox_window_by_title(keyword="豆包"):
"""按标题关键词找Firefox窗口,返回最匹配的ID"""
try:
result = subprocess.run(
["xdotool", "search", "--name", "Firefox"],
capture_output=True, text=True, timeout=2
)
firefox_ids = result.stdout.strip().split("\n")
target_id = ""
for win_id in firefox_ids:
if not win_id.strip():
continue
title_result = subprocess.run(
["xdotool", "getwindowname", win_id],
capture_output=True, text=True, timeout=1
)
win_title = title_result.stdout.strip()
if keyword in win_title and len(win_title) > 10:
target_id = win_id
print(f"✅ 找到目标窗口:ID={win_id},标题={win_title}")
break
return target_id if target_id else None
except Exception as e:
print(f"❌ 查找窗口失败:{e}")
return None
def get_mouse_coords():
"""获取当前鼠标坐标(用于校准书签位置)"""
result = subprocess.run(
["xdotool", "getmouselocation"],
capture_output=True, text=True
)
coords = re.findall(r'x:(\d+) y:(\d+)', result.stdout)
if coords:
return int(coords[0][0]), int(coords[0][1])
return None, None
def execute_bookmark():
print("📌 请把鼠标放到Firefox右上角菜单按钮,按回车...")
input()
menu_x, menu_y = get_mouse_coords()
if not menu_x:
print("❌ 无法获取鼠标坐标")
return
print("📌 请把鼠标放到'豆包备份带进度条'书签,按回车...")
input()
bookmark_x, bookmark_y = get_mouse_coords()
if not bookmark_x:
print("❌ 无法获取鼠标坐标")
return
target_win_id = get_firefox_window_by_title("豆包")
if not target_win_id:
print("❌ 没找到豆包窗口")
return
subprocess.run(["xdotool", "windowactivate", "--sync", target_win_id], timeout=2)
time.sleep(0.8)
subprocess.run(["xdotool", "mousemove", "--window", target_win_id, str(menu_x), str(menu_y)], timeout=1)
time.sleep(0.2)
subprocess.run(["xdotool", "click", "--window", target_win_id, "1"], timeout=1)
time.sleep(0.5)
subprocess.run(["xdotool", "mousemove", "--window", target_win_id, str(bookmark_x), str(bookmark_y)], timeout=1)
time.sleep(0.2)
subprocess.run(["xdotool", "click", "--window", target_win_id, "1"], timeout=1)
time.sleep(2)
print("🎉 操作完成!JS书签已执行")
if __name__ == "__main__":
execute_bookmark()