File size: 1,607 Bytes
03cdf80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const puppeteer = require('puppeteer-core');
const axios = require('axios');

const BROWSER_API_URL = 'https://samleuma-browser.hf.space';

async function getRemoteBrowser() {
  const response = await axios.get(`${BROWSER_API_URL}/api/ws-endpoint`, { timeout: 30000 });
  
  if (!response.data.browserConnected || !response.data.wsEndpoint) {
    throw new Error('Remote browser not ready');
  }
  
  const wsEndpoint = response.data.wsEndpoint.replace('ws://127.0.0.1', 'wss://samleuma-browser.hf.space');
  
  const browser = await puppeteer.connect({
    browserWSEndpoint: wsEndpoint,
    defaultViewport: { width: 1280, height: 800 }
  });
  
  return browser;
}

async function executeOnRemoteBrowser(url, script, waitFor = null) {
  const payload = { url, script };
  if (waitFor) payload.waitFor = waitFor;
  
  const response = await axios.post(`${BROWSER_API_URL}/api/execute`, payload, { timeout: 60000 });
  return response.data;
}

async function takeRemoteScreenshot(url, fullPage = false) {
  const response = await axios.post(`${BROWSER_API_URL}/api/screenshot`, { url, fullPage }, { timeout: 60000 });
  return response.data;
}

async function createRemotePage() {
  const response = await axios.post(`${BROWSER_API_URL}/api/page/new`, {}, { timeout: 30000 });
  return response.data;
}

async function getBrowserStatus() {
  const response = await axios.get(`${BROWSER_API_URL}/api/status`, { timeout: 10000 });
  return response.data;
}

module.exports = {
  getRemoteBrowser,
  executeOnRemoteBrowser,
  takeRemoteScreenshot,
  createRemotePage,
  getBrowserStatus,
  BROWSER_API_URL
};