Sebebeb commited on
Commit
ea3029f
·
verified ·
1 Parent(s): bebe442

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -5
app.py CHANGED
@@ -11,15 +11,44 @@ VIEWPORT = {"width": 1280, "height": 800}
11
  _lock = threading.Lock()
12
  _state = {"playwright": None, "browser": None, "page": None}
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  def get_page():
16
  if _state["page"] is None:
17
  _state["playwright"] = sync_playwright().start()
18
  _state["browser"] = _state["playwright"].chromium.launch(
19
  headless=True,
20
- args=["--no-sandbox", "--disable-dev-shm-usage"],
 
 
 
 
 
 
 
 
 
 
 
21
  )
22
- context = _state["browser"].new_context(viewport=VIEWPORT)
23
  _state["page"] = context.new_page()
24
  _state["page"].goto("https://example.com", wait_until="domcontentloaded")
25
  return _state["page"]
@@ -68,7 +97,7 @@ img.addEventListener('click', async (e) => {
68
  const y = (e.clientY - rect.top) / rect.height * vh;
69
  await fetch('/click', {method:'POST', headers:{'Content-Type':'application/json'},
70
  body: JSON.stringify({x, y})});
71
- setTimeout(refresh, 300);
72
  });
73
 
74
  document.addEventListener('keydown', async (e) => {
@@ -85,7 +114,7 @@ document.getElementById('stage').addEventListener('wheel', async (e) => {
85
  refresh();
86
  });
87
 
88
- setInterval(refresh, 1000);
89
  </script>
90
  </body></html>"""
91
 
@@ -103,7 +132,7 @@ def screenshot():
103
  with _lock:
104
  page = get_page()
105
  try:
106
- data = page.screenshot(type="jpeg", quality=60)
107
  except Exception:
108
  data = b""
109
  return Response(data, content_type="image/jpeg")
 
11
  _lock = threading.Lock()
12
  _state = {"playwright": None, "browser": None, "page": None}
13
 
14
+ USER_AGENT = (
15
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
16
+ "(KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
17
+ )
18
+
19
+ STEALTH_JS = """
20
+ Object.defineProperty(navigator, 'webdriver', {get: () => undefined});
21
+ Object.defineProperty(navigator, 'plugins', {get: () => [1, 2, 3, 4, 5]});
22
+ Object.defineProperty(navigator, 'languages', {get: () => ['en-US', 'en']});
23
+ window.chrome = { runtime: {} };
24
+ const originalQuery = window.navigator.permissions.query;
25
+ window.navigator.permissions.query = (parameters) => (
26
+ parameters.name === 'notifications'
27
+ ? Promise.resolve({ state: Notification.permission })
28
+ : originalQuery(parameters)
29
+ );
30
+ """
31
+
32
 
33
  def get_page():
34
  if _state["page"] is None:
35
  _state["playwright"] = sync_playwright().start()
36
  _state["browser"] = _state["playwright"].chromium.launch(
37
  headless=True,
38
+ args=[
39
+ "--no-sandbox",
40
+ "--disable-dev-shm-usage",
41
+ "--disable-blink-features=AutomationControlled",
42
+ ],
43
+ ignore_default_args=["--enable-automation"],
44
+ )
45
+ context = _state["browser"].new_context(
46
+ viewport=VIEWPORT,
47
+ user_agent=USER_AGENT,
48
+ locale="en-US",
49
+ timezone_id="America/New_York",
50
  )
51
+ context.add_init_script(STEALTH_JS)
52
  _state["page"] = context.new_page()
53
  _state["page"].goto("https://example.com", wait_until="domcontentloaded")
54
  return _state["page"]
 
97
  const y = (e.clientY - rect.top) / rect.height * vh;
98
  await fetch('/click', {method:'POST', headers:{'Content-Type':'application/json'},
99
  body: JSON.stringify({x, y})});
100
+ setTimeout(refresh, 150);
101
  });
102
 
103
  document.addEventListener('keydown', async (e) => {
 
114
  refresh();
115
  });
116
 
117
+ setInterval(refresh, 500);
118
  </script>
119
  </body></html>"""
120
 
 
132
  with _lock:
133
  page = get_page()
134
  try:
135
+ data = page.screenshot(type="jpeg", quality=85)
136
  except Exception:
137
  data = b""
138
  return Response(data, content_type="image/jpeg")