53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
import logging
|
|
|
|
from playwright.sync_api import sync_playwright
|
|
|
|
logger = logging.getLogger("desktopenv.metrics.chrome")
|
|
|
|
|
|
|
|
|
|
# todo: move to getter module
|
|
|
|
# The following ones just need to load info from the files of software, no need to connect to the software
|
|
|
|
|
|
# The following ones require Playwright to be installed on the target machine, and the chrome needs to be pre-config on port info to allow remote debugging, see README.md for details
|
|
|
|
def get_open_tabs_info(remote_debugging_url):
|
|
with sync_playwright() as p:
|
|
# connect to remote Chrome instance
|
|
browser = p.chromium.connect_over_cdp(remote_debugging_url)
|
|
|
|
tabs_info = []
|
|
for context in browser.contexts:
|
|
for page in context.pages:
|
|
title = page.title()
|
|
url = page.url
|
|
tabs_info.append({'title': title, 'url': url})
|
|
|
|
browser.close()
|
|
return tabs_info
|
|
|
|
|
|
def get_active_tab_info(remote_debugging_url):
|
|
with sync_playwright() as p:
|
|
# connect to remote Chrome instance
|
|
browser = p.chromium.connect_over_cdp(remote_debugging_url)
|
|
|
|
active_tab_info = {}
|
|
for context in browser.contexts:
|
|
for page in context.pages():
|
|
if page.is_visible("body"): # check the visibility of the page body to determine the active status
|
|
active_tab_info = {
|
|
'title': page.title(),
|
|
'url': page.url,
|
|
'content': page.content() # get the HTML content of the page
|
|
}
|
|
break
|
|
if active_tab_info:
|
|
break
|
|
|
|
browser.close()
|
|
return active_tab_info
|