mirror of
https://github.com/trycua/computer.git
synced 2026-01-01 19:10:30 -06:00
Patch anti-bot-detection/stealth-mode into playwright browser
This commit is contained in:
@@ -48,6 +48,14 @@ async def test_browser_tool():
|
||||
logger.info("Testing Browser Tool...")
|
||||
|
||||
try:
|
||||
# Test 0: Take a screenshot (pre-init)
|
||||
logger.info("Test 0: Taking a screenshot...")
|
||||
screenshot_bytes = await browser.screenshot()
|
||||
screenshot_path = Path(__file__).parent / "browser_screenshot_init.png"
|
||||
with open(screenshot_path, "wb") as f:
|
||||
f.write(screenshot_bytes)
|
||||
logger.info(f"Screenshot captured: {len(screenshot_bytes)} bytes")
|
||||
|
||||
# Test 1: Visit a URL
|
||||
logger.info("Test 1: Visiting a URL...")
|
||||
result = await browser.visit_url("https://www.trycua.com")
|
||||
@@ -56,6 +64,22 @@ async def test_browser_tool():
|
||||
# Wait a bit for the page to load
|
||||
await asyncio.sleep(2)
|
||||
|
||||
# Test 2: Take a screenshot
|
||||
logger.info("Test 2: Taking a screenshot...")
|
||||
screenshot_bytes = await browser.screenshot()
|
||||
screenshot_path = Path(__file__).parent / "browser_screenshot.png"
|
||||
with open(screenshot_path, "wb") as f:
|
||||
f.write(screenshot_bytes)
|
||||
logger.info(f"Screenshot captured: {len(screenshot_bytes)} bytes")
|
||||
|
||||
# Wait a bit
|
||||
await asyncio.sleep(1)
|
||||
|
||||
# Test 3: Visit bot detector
|
||||
logger.info("Test 3: Visiting bot detector...")
|
||||
result = await browser.visit_url("https://bot-detector.rebrowser.net/")
|
||||
logger.info(f"Visit URL result: {result}")
|
||||
|
||||
# Test 2: Web search
|
||||
logger.info("Test 2: Performing a web search...")
|
||||
result = await browser.web_search("Python programming")
|
||||
|
||||
@@ -114,3 +114,22 @@ class BrowserTool:
|
||||
Response dictionary with success status and current URL
|
||||
"""
|
||||
return await self._execute_command("web_search", {"query": query})
|
||||
|
||||
async def screenshot(self) -> bytes:
|
||||
"""
|
||||
Take a screenshot of the current browser page.
|
||||
|
||||
Returns:
|
||||
Screenshot image data as bytes (PNG format)
|
||||
"""
|
||||
import base64
|
||||
|
||||
result = await self._execute_command("screenshot", {})
|
||||
if result.get("success") and result.get("screenshot"):
|
||||
# Decode base64 screenshot to bytes
|
||||
screenshot_b64 = result["screenshot"]
|
||||
screenshot_bytes = base64.b64decode(screenshot_b64)
|
||||
return screenshot_bytes
|
||||
else:
|
||||
error = result.get("error", "Unknown error")
|
||||
raise RuntimeError(f"Failed to take screenshot: {error}")
|
||||
|
||||
@@ -109,6 +109,33 @@ class BrowserManager:
|
||||
# Removed --kiosk to allow desktop visibility
|
||||
)
|
||||
|
||||
# Add init script to make the browser less detectable
|
||||
await self.context.add_init_script(
|
||||
"""const defaultGetter = Object.getOwnPropertyDescriptor(
|
||||
Navigator.prototype,
|
||||
"webdriver"
|
||||
).get;
|
||||
defaultGetter.apply(navigator);
|
||||
defaultGetter.toString();
|
||||
Object.defineProperty(Navigator.prototype, "webdriver", {
|
||||
set: undefined,
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
get: new Proxy(defaultGetter, {
|
||||
apply: (target, thisArg, args) => {
|
||||
Reflect.apply(target, thisArg, args);
|
||||
return false;
|
||||
},
|
||||
}),
|
||||
});
|
||||
const patchedGetter = Object.getOwnPropertyDescriptor(
|
||||
Navigator.prototype,
|
||||
"webdriver"
|
||||
).get;
|
||||
patchedGetter.apply(navigator);
|
||||
patchedGetter.toString();"""
|
||||
)
|
||||
|
||||
# Get the first page or create one
|
||||
pages = self.context.pages
|
||||
if pages:
|
||||
@@ -167,6 +194,14 @@ class BrowserManager:
|
||||
await self.page.goto(search_url, wait_until="domcontentloaded", timeout=30000)
|
||||
return {"success": True, "url": self.page.url}
|
||||
|
||||
elif cmd == "screenshot":
|
||||
# Take a screenshot and return as base64
|
||||
import base64
|
||||
|
||||
screenshot_bytes = await self.page.screenshot(type="png")
|
||||
screenshot_b64 = base64.b64encode(screenshot_bytes).decode("utf-8")
|
||||
return {"success": True, "screenshot": screenshot_b64}
|
||||
|
||||
else:
|
||||
return {"success": False, "error": f"Unknown command: {cmd}"}
|
||||
|
||||
|
||||
@@ -108,7 +108,7 @@ RUN mkdir -p /home/cua/.cache && \
|
||||
RUN python3.12 -m pip install cua-computer-server
|
||||
|
||||
# Install playwright and Firefox dependencies
|
||||
RUN python3.12 -m pip install playwright && \
|
||||
RUN python3.12 -m pip install rebrowser-playwright && \
|
||||
python3.12 -m playwright install --with-deps firefox
|
||||
|
||||
# Fix any cache files created by pip
|
||||
|
||||
@@ -110,7 +110,7 @@ RUN python3.12 -m pip install /tmp/computer-server && \
|
||||
rm -rf /tmp/computer-server
|
||||
|
||||
# Install playwright and Firefox dependencies
|
||||
RUN python3.12 -m pip install playwright && \
|
||||
RUN python3.12 -m pip install rebrowser-playwright && \
|
||||
python3.12 -m playwright install --with-deps firefox
|
||||
|
||||
# Fix any cache files created by pip
|
||||
|
||||
Reference in New Issue
Block a user