File size: 3,927 Bytes
5c7290f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from flask import Flask, request, jsonify
from playwright.sync_api import sync_playwright
import base64

app = Flask(__name__)

# Global variables to keep the browser session alive
p = None
browser = None
page = None

def initialize_browser():
    """Browser start karta hai agar pehle se nahi chal raha."""
    global p, browser, page
    if not page:
        p = sync_playwright().start()
        # Headless mode = True (Browser background me chalega)
        browser = p.chromium.launch(headless=True)
        # Context create karte hain taake history maintain rahe
        context = browser.new_context(viewport={'width': 1280, 'height': 720})
        page = context.new_page()
        print("Browser Initialized")

def get_page_data():
    """Current page ka Screenshot aur HTML DOM return karta hai."""
    global page
    
    # 1. HTML Content
    html_content = page.content()
    
    # 2. Screenshot (Base64 string format mein)
    screenshot_bytes = page.screenshot(full_page=False)
    screenshot_b64 = base64.b64encode(screenshot_bytes).decode('utf-8')
    
    return {
        "html": html_content,
        "screenshot": screenshot_b64
    }

@app.route('/')
def home():
    return "Browser Server is Running. Use POST /command to interact."

@app.route('/command', methods=['POST'])
def handle_command():
    global page
    
    # Ensure browser is running
    initialize_browser()
    
    data = request.json
    action = data.get('action')
    
    try:
        # --- 1. NAVIGATE (Website kholna) ---
        if action == 'navigate':
            url = data.get('url')
            if not url:
                return jsonify({"error": "URL is required"}), 400
            page.goto(url, timeout=60000)
            page.wait_for_load_state('networkidle') # Wait until page settles

        # --- 2. CLICK / TAP ---
        elif action == 'click':
            selector = data.get('selector')
            if selector:
                page.click(selector)
            else:
                # Agar selector nahi to coordinates par click (optional)
                x = data.get('x')
                y = data.get('y')
                if x is not None and y is not None:
                    page.mouse.click(x, y)

        # --- 3. TYPE (Likhna) ---
        elif action == 'type':
            selector = data.get('selector')
            text = data.get('text')
            if selector and text:
                page.fill(selector, text)

        # --- 4. ENTER KEY (Form submit waghera ke liye) ---
        elif action == 'enter':
            page.keyboard.press('Enter')

        # --- 5. SCROLL ---
        elif action == 'scroll':
            direction = data.get('direction', 'down')
            amount = data.get('amount', 500)
            if direction == 'down':
                page.mouse.wheel(0, amount)
            elif direction == 'up':
                page.mouse.wheel(0, -amount)
            # Thoda wait karein taake scroll render ho jaye
            page.wait_for_timeout(500)

        # --- 6. HOVER ---
        elif action == 'hover':
            selector = data.get('selector')
            if selector:
                page.hover(selector)

        # --- 7. WAIT (Agar manual delay chahiye) ---
        elif action == 'wait':
            duration = data.get('duration', 1000)
            page.wait_for_timeout(duration)

        else:
            return jsonify({"error": "Unknown action"}), 400

        # --- RETURN DATA (Screenshot + DOM) ---
        response_data = get_page_data()
        return jsonify({
            "status": "success",
            "action_performed": action,
            "current_url": page.url,
            "dom": response_data['html'],
            "screenshot_base64": response_data['screenshot']
        })

    except Exception as e:
        return jsonify({"status": "error", "message": str(e)}), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)