File size: 1,288 Bytes
d4ca640
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pyperclip
from browser_use.agent.views import ActionResult
from browser_use.browser.context import BrowserContext
from browser_use.controller.service import Controller

class CustomController(Controller):
    def __init__(self):
        super().__init__()
        self._register_custom_actions()

    def _register_custom_actions(self):
        @self.registry.action("Copy text to clipboard")
        def copy_to_clipboard(text: str):
            try:
                pyperclip.copy(text)
                return ActionResult(extracted_content=text)
            except Exception as e:
                return ActionResult(error=f"Failed to copy text to clipboard: {str(e)}")

        @self.registry.action("Paste text from clipboard", requires_browser=True)
        async def paste_from_clipboard(browser: BrowserContext):
            try:
                text = pyperclip.paste()
                page = await browser.get_current_page()
                if not page:
                    return ActionResult(error="No active page found in browser.")

                await page.keyboard.type(text)
                return ActionResult(extracted_content=text)
            except Exception as e:
                return ActionResult(error=f"Failed to paste text from clipboard: {str(e)}")