| | from flask import Flask, request, jsonify |
| | from playwright.sync_api import sync_playwright |
| | import base64 |
| |
|
| | app = Flask(__name__) |
| |
|
| | @app.route("/command", methods=["POST"]) |
| | def command(): |
| | data = request.json |
| | action = data.get("action") |
| | value = data.get("value") |
| |
|
| | |
| | with sync_playwright() as p: |
| | browser = p.chromium.launch(headless=True) |
| | page = browser.new_page() |
| | try: |
| | if action == "goto": |
| | page.goto(value) |
| | elif action == "scroll": |
| | page.evaluate(f"window.scrollTo(0, {value})") |
| | elif action == "click": |
| | x, y = value["x"], value["y"] |
| | page.mouse.click(x, y) |
| | elif action == "type": |
| | selector, text = value["selector"], value["text"] |
| | page.fill(selector, text) |
| | elif action == "hold": |
| | x, y, duration = value["x"], value["y"], value["duration"] |
| | page.mouse.move(x, y) |
| | page.mouse.down() |
| | page.wait_for_timeout(duration) |
| | page.mouse.up() |
| |
|
| | |
| | screenshot = page.screenshot() |
| | screenshot_b64 = base64.b64encode(screenshot).decode() |
| |
|
| | |
| | html = page.content() |
| |
|
| | return jsonify({"screenshot": screenshot_b64, "html": html}) |
| |
|
| | except Exception as e: |
| | return jsonify({"error": str(e)}), 400 |
| | finally: |
| | browser.close() |
| |
|
| | if __name__ == "__main__": |
| | app.run(host="0.0.0.0", port=5000) |