Josedcape commited on
Commit
d4ca640
·
verified ·
1 Parent(s): 46159c3

Update src/controller/custom_controller.py

Browse files
Files changed (1) hide show
  1. src/controller/custom_controller.py +31 -27
src/controller/custom_controller.py CHANGED
@@ -1,27 +1,31 @@
1
- import pyperclip
2
- from browser_use.agent.views import ActionResult
3
- from browser_use.browser.context import BrowserContext
4
- from browser_use.controller.service import Controller
5
-
6
-
7
- class CustomController(Controller):
8
- def __init__(self):
9
- super().__init__()
10
- self._register_custom_actions()
11
-
12
- def _register_custom_actions(self):
13
- """Register all custom browser actions"""
14
-
15
- @self.registry.action("Copy text to clipboard")
16
- def copy_to_clipboard(text: str):
17
- pyperclip.copy(text)
18
- return ActionResult(extracted_content=text)
19
-
20
- @self.registry.action("Paste text from clipboard", requires_browser=True)
21
- async def paste_from_clipboard(browser: BrowserContext):
22
- text = pyperclip.paste()
23
- # send text to browser
24
- page = await browser.get_current_page()
25
- await page.keyboard.type(text)
26
-
27
- return ActionResult(extracted_content=text)
 
 
 
 
 
1
+ import pyperclip
2
+ from browser_use.agent.views import ActionResult
3
+ from browser_use.browser.context import BrowserContext
4
+ from browser_use.controller.service import Controller
5
+
6
+ class CustomController(Controller):
7
+ def __init__(self):
8
+ super().__init__()
9
+ self._register_custom_actions()
10
+
11
+ def _register_custom_actions(self):
12
+ @self.registry.action("Copy text to clipboard")
13
+ def copy_to_clipboard(text: str):
14
+ try:
15
+ pyperclip.copy(text)
16
+ return ActionResult(extracted_content=text)
17
+ except Exception as e:
18
+ return ActionResult(error=f"Failed to copy text to clipboard: {str(e)}")
19
+
20
+ @self.registry.action("Paste text from clipboard", requires_browser=True)
21
+ async def paste_from_clipboard(browser: BrowserContext):
22
+ try:
23
+ text = pyperclip.paste()
24
+ page = await browser.get_current_page()
25
+ if not page:
26
+ return ActionResult(error="No active page found in browser.")
27
+
28
+ await page.keyboard.type(text)
29
+ return ActionResult(extracted_content=text)
30
+ except Exception as e:
31
+ return ActionResult(error=f"Failed to paste text from clipboard: {str(e)}")