Skip to content

Commit

Permalink
add more docs for scripting with the chrome runner addon
Browse files Browse the repository at this point in the history
  • Loading branch information
miaucl committed Sep 20, 2024
1 parent d8512c0 commit 7ffc4b4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"cSpell.words": [
"aarch",
"asyncio",
"codenotary",
"gethostbyname",
"homeassistant",
"mdns",
"miaucl",
"Miaucl's",
"socat",
Expand Down
39 changes: 39 additions & 0 deletions chrome-runner/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,42 @@ Open your browser to: `http://localhost:9222` and then click on the tab you want
`https://chrome-devtools-frontend.appspot.com/serve_file/@.../inspector.html?ws=localhost:9222/[END]`
by
`chrome-devtools://devtools/bundled/inspector.html?ws=localhost:9222/[END]`

### Example

Using python and playwright, following snippet leverages the addons capability to serve as a remote web runner. This functionality can be used ad hoc or within an integration of course. The addons requires to be address by its IP, which is why we pre-resolve it in this snipped.

`pip install playwright`

```python
import asyncio
import time
from playwright.async_api import async_playwright
import socket
import re

# Set the remote addr
remote = 'homeassistant.local' # Use in case of mdns local domain
# remote = 'homeassistant.home' # Use in case of private DNS server
# remote = '192.168.1.36' # Use in case of direct IP
# remote = 'localhost' # Use in case of running directly inside the addon

port = "9222"

# Regex match for IP address
ipv4_pattern = re.compile(r'^(\d{1,3}\.){3}\d{1,3}$')

def resolve_remote_addr(addr):
"""Resolve the DNS entries."""
return addr if addr == 'localhost' or ipv4_pattern.match(addr) else socket.gethostbyname(addr)

async def main():
async with async_playwright() as p:
browser = await p.chromium.connect_over_cdp(f"http://{resolve_remote_addr(remote)}:{port}")
page = await browser.new_page()
await page.goto('http://playwright.dev')
await page.screenshot(path=f'demo.png')
await browser.close()

asyncio.run(main())
```

0 comments on commit 7ffc4b4

Please sign in to comment.