-
-
Notifications
You must be signed in to change notification settings - Fork 198
/
vimbot.py
59 lines (48 loc) · 1.7 KB
/
vimbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import time
from io import BytesIO
from PIL import Image
from playwright.sync_api import sync_playwright
vimium_path = "./vimium-master"
class Vimbot:
def __init__(self, headless=False):
self.context = (
sync_playwright()
.start()
.chromium.launch_persistent_context(
"",
headless=headless,
args=[
f"--disable-extensions-except={vimium_path}",
f"--load-extension={vimium_path}",
],
ignore_https_errors=True,
)
)
self.page = self.context.new_page()
self.page.set_viewport_size({"width": 1080, "height": 720})
def perform_action(self, action):
if "done" in action:
return True
if "click" in action and "type" in action:
self.click(action["click"])
self.type(action["type"])
if "navigate" in action:
self.navigate(action["navigate"])
elif "type" in action:
self.type(action["type"])
elif "click" in action:
self.click(action["click"])
def navigate(self, url):
self.page.goto(url=url if "://" in url else "https://" + url, timeout=60000)
def type(self, text):
time.sleep(1)
self.page.keyboard.type(text)
self.page.keyboard.press("Enter")
def click(self, text):
self.page.keyboard.type(text)
def capture(self):
# capture a screenshot with vim bindings on the screen
self.page.keyboard.press("Escape")
self.page.keyboard.type("f")
screenshot = Image.open(BytesIO(self.page.screenshot())).convert("RGB")
return screenshot