169 lines
7.1 KiB
Python
169 lines
7.1 KiB
Python
"""
|
|
browser_cli — Python API for controlling your running browser.
|
|
|
|
Usage:
|
|
from browser_cli import BrowserCLI
|
|
b = BrowserCLI()
|
|
tabs = b.tabs_list()
|
|
b.open("https://example.com")
|
|
"""
|
|
from browser_cli.client import BrowserNotConnected, send_command
|
|
|
|
__all__ = ["BrowserCLI", "BrowserNotConnected"]
|
|
|
|
|
|
class BrowserCLI:
|
|
# ── Navigation ────────────────────────────────────────────────────────
|
|
|
|
def open(self, url: str, *, background: bool = False, window: str | None = None):
|
|
return send_command("navigate.open", {"url": url, "background": background, "window": window})
|
|
|
|
def reload(self, tab_id: int | None = None):
|
|
return send_command("navigate.reload", {"tabId": tab_id})
|
|
|
|
def hard_reload(self, tab_id: int | None = None):
|
|
return send_command("navigate.hard_reload", {"tabId": tab_id})
|
|
|
|
def back(self, tab_id: int | None = None):
|
|
return send_command("navigate.back", {"tabId": tab_id})
|
|
|
|
def forward(self, tab_id: int | None = None):
|
|
return send_command("navigate.forward", {"tabId": tab_id})
|
|
|
|
def focus_url(self, pattern: str):
|
|
return send_command("navigate.focus", {"pattern": pattern})
|
|
|
|
# ── Tabs ──────────────────────────────────────────────────────────────
|
|
|
|
def tabs_list(self) -> list[dict]:
|
|
return send_command("tabs.list", {})
|
|
|
|
def tabs_close(self, tab_id: int | None = None, *, inactive: bool = False, duplicates: bool = False):
|
|
return send_command("tabs.close", {"tabId": tab_id, "inactive": inactive, "duplicates": duplicates})
|
|
|
|
def tabs_move(self, tab_id: int, *, group_id: int | None = None, window_id: int | None = None):
|
|
return send_command("tabs.move", {"tabId": tab_id, "groupId": group_id, "windowId": window_id})
|
|
|
|
def tabs_active(self, tab_id: int):
|
|
return send_command("tabs.active", {"tabId": tab_id})
|
|
|
|
def tabs_filter(self, pattern: str) -> list[dict]:
|
|
return send_command("tabs.filter", {"pattern": pattern})
|
|
|
|
def tabs_count(self, pattern: str | None = None) -> int:
|
|
return send_command("tabs.count", {"pattern": pattern})
|
|
|
|
def tabs_query(self, search: str) -> list[dict]:
|
|
return send_command("tabs.query", {"search": search})
|
|
|
|
def tabs_html(self, tab_id: int | None = None) -> str:
|
|
return send_command("tabs.html", {"tabId": tab_id})
|
|
|
|
def tabs_dedupe(self):
|
|
return send_command("tabs.dedupe", {})
|
|
|
|
def tabs_sort(self, by: str = "domain"):
|
|
return send_command("tabs.sort", {"by": by})
|
|
|
|
def tabs_merge_windows(self):
|
|
return send_command("tabs.merge_windows", {})
|
|
|
|
def tabs_close_inactive(self):
|
|
return send_command("tabs.close", {"inactive": True})
|
|
|
|
def tabs_close_duplicates(self):
|
|
return send_command("tabs.close", {"duplicates": True})
|
|
|
|
# ── Tab Groups ────────────────────────────────────────────────────────
|
|
|
|
def group_list(self) -> list[dict]:
|
|
return send_command("group.list", {})
|
|
|
|
def group_tabs(self, group_id: int) -> list[dict]:
|
|
return send_command("group.tabs", {"groupId": group_id})
|
|
|
|
def group_count(self) -> int:
|
|
return send_command("group.count", {})
|
|
|
|
def group_query(self, search: str) -> list[dict]:
|
|
return send_command("group.query", {"search": search})
|
|
|
|
def group_close(self, group_id: int):
|
|
return send_command("group.close", {"groupId": group_id})
|
|
|
|
def group_open(self, name: str) -> dict:
|
|
return send_command("group.open", {"name": name})
|
|
|
|
# ── Windows ───────────────────────────────────────────────────────────
|
|
|
|
def windows_list(self) -> list[dict]:
|
|
return send_command("windows.list", {})
|
|
|
|
def windows_rename(self, window_id: int, name: str):
|
|
return send_command("windows.rename", {"windowId": window_id, "name": name})
|
|
|
|
def windows_close(self, window_id: int):
|
|
return send_command("windows.close", {"windowId": window_id})
|
|
|
|
def windows_open(self, profile: str | None = None) -> dict:
|
|
return send_command("windows.open", {"profile": profile})
|
|
|
|
# ── DOM ───────────────────────────────────────────────────────────────
|
|
|
|
def dom_query(self, selector: str) -> list[dict]:
|
|
return send_command("dom.query", {"selector": selector})
|
|
|
|
def dom_click(self, selector: str):
|
|
return send_command("dom.click", {"selector": selector})
|
|
|
|
def dom_type(self, selector: str, text: str):
|
|
return send_command("dom.type", {"selector": selector, "text": text})
|
|
|
|
def dom_attr(self, selector: str, attr: str) -> list[str]:
|
|
return send_command("dom.attr", {"selector": selector, "attr": attr})
|
|
|
|
def dom_text(self, selector: str) -> list[str]:
|
|
return send_command("dom.text", {"selector": selector})
|
|
|
|
def dom_exists(self, selector: str) -> bool:
|
|
return send_command("dom.exists", {"selector": selector})
|
|
|
|
# ── Extract ───────────────────────────────────────────────────────────
|
|
|
|
def extract_links(self) -> list[dict]:
|
|
return send_command("extract.links", {})
|
|
|
|
def extract_images(self) -> list[dict]:
|
|
return send_command("extract.images", {})
|
|
|
|
def extract_text(self) -> str:
|
|
return send_command("extract.text", {})
|
|
|
|
def extract_json(self, selector: str):
|
|
return send_command("extract.json", {"selector": selector})
|
|
|
|
# ── Session ───────────────────────────────────────────────────────────
|
|
|
|
def session_save(self, name: str):
|
|
return send_command("session.save", {"name": name})
|
|
|
|
def session_load(self, name: str):
|
|
return send_command("session.load", {"name": name})
|
|
|
|
def session_diff(self, name_a: str, name_b: str) -> dict:
|
|
return send_command("session.diff", {"nameA": name_a, "nameB": name_b})
|
|
|
|
def session_list(self) -> list[dict]:
|
|
return send_command("session.list", {})
|
|
|
|
def session_remove(self, name: str):
|
|
return send_command("session.remove", {"name": name})
|
|
|
|
def session_auto_save(self, enabled: bool):
|
|
return send_command("session.auto_save", {"enabled": enabled})
|
|
|
|
# ── Misc ──────────────────────────────────────────────────────────────
|
|
|
|
def clients(self) -> list[dict]:
|
|
return send_command("clients.list", {})
|