senku21230 commited on
Commit
ed53ea2
·
verified ·
1 Parent(s): f241d61

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +88 -18
server.js CHANGED
@@ -32,8 +32,13 @@ let consoleLogs = [];
32
  let networkRequests = [];
33
  let blockedUrls = [];
34
 
 
 
 
 
 
35
  async function initBrowser() {
36
- console.log("Initializing Ultimate God-Mode Browser...");
37
  browser = await puppeteer.launch({
38
  headless: true,
39
  args: [
@@ -48,7 +53,7 @@ async function initBrowser() {
48
  const initialPages = await browser.pages();
49
  if (initialPages.length > 1) await initialPages[0].close();
50
 
51
- console.log("Browser successfully initialized with Advanced AI Engine!");
52
  }
53
 
54
  async function setupNewPage(newPage) {
@@ -56,7 +61,6 @@ async function setupNewPage(newPage) {
56
  pages = await browser.pages();
57
  await page.setViewport({ width: 1920, height: 1080 });
58
 
59
- // --- STEALTH MODE (Bypass Bot Detection) ---
60
  await page.evaluateOnNewDocument(() => {
61
  Object.defineProperty(navigator, 'webdriver', { get: () => undefined });
62
  window.navigator.chrome = { runtime: {} };
@@ -65,7 +69,7 @@ async function setupNewPage(newPage) {
65
  });
66
  await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36');
67
 
68
- // --- DEVTOOLS SETUP ---
69
  devToolsClient = await page.target().createCDPSession();
70
  consoleLogs = []; networkRequests = [];
71
 
@@ -78,8 +82,23 @@ async function setupNewPage(newPage) {
78
  else interceptedRequest.continue();
79
  });
80
  page.on('dialog', async dialog => { await dialog.accept(); });
 
 
 
 
 
 
81
  }
82
 
 
 
 
 
 
 
 
 
 
83
  // ==========================================
84
  // DEFINE 105 TOOLS SCHEMA
85
  // ==========================================
@@ -208,20 +227,18 @@ const toolSchemas = [
208
  { name: "emulate_color_scheme", description: "[F12] Dark/Light mode", inputSchema: { type: "object", properties: { theme: { type: "string", enum: ["dark", "light"] } }, required: ["theme"] } },
209
  { name: "clear_browser_cache", description: "[F12] Clear Cache", inputSchema: { type: "object", properties: {} } },
210
 
211
- // ==========================================
212
- // 🔥 ADVANCED AI ENGINE TOOLS (101-105) 🔥
213
- // ==========================================
214
  { name: "smart_scrape_markdown", description: "[ADVANCED ENGINE] Extract main article content and convert to Clean LLM-readable Markdown", inputSchema: { type: "object", properties: {} } },
215
  { name: "extract_structured_data", description: "[ADVANCED ENGINE] Extract structured data (Schema.org/JSON-LD)", inputSchema: { type: "object", properties: {} } },
216
- { name: "auto_smart_scroll", description: "[ADVANCED ENGINE] Auto-scroll to load dynamic/lazy content before scraping", inputSchema: { type: "object", properties: {} } },
217
- { name: "deep_domain_crawler", description: "[ADVANCED ENGINE] Extract all internal links of the same domain for deep crawling", inputSchema: { type: "object", properties: {} } },
218
- { name: "clean_page_clutter", description: "[ADVANCED ENGINE] Force remove ads, popups, cookie banners from DOM", inputSchema: { type: "object", properties: {} } }
219
  ];
220
 
221
  mcpServer.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: toolSchemas }));
222
 
223
  // ==========================================
224
- // TOOL EXECUTION HANDLERS (With Advanced AI Engine Logic)
225
  // ==========================================
226
  const toolHandlers = {
227
  // Nav
@@ -353,9 +370,7 @@ const toolHandlers = {
353
  "emulate_color_scheme": async (a) => { await page.emulateMediaFeatures([{ name: 'prefers-color-scheme', value: a.theme }]); return "Theme set"; },
354
  "clear_browser_cache": async () => { await devToolsClient.send('Network.clearBrowserCache'); return "Cache cleared"; },
355
 
356
- // ==========================================
357
- // 🔥 ADVANCED AI ENGINE HANDLERS 🔥
358
- // ==========================================
359
  "smart_scrape_markdown": async () => {
360
  try {
361
  await page.addScriptTag({ url: 'https://unpkg.com/@mozilla/readability/Readability.js' });
@@ -387,7 +402,7 @@ const toolHandlers = {
387
  }, 100);
388
  });
389
  });
390
- return "Smart scrolling finished. Dynamic content loaded.";
391
  },
392
  "deep_domain_crawler": async () => {
393
  const urlObj = new URL(await page.url());
@@ -403,7 +418,7 @@ const toolHandlers = {
403
  const selectors = ['header', 'footer', 'nav', '.ad', '.ads', '#cookie-banner', '.popup', 'iframe'];
404
  selectors.forEach(sel => document.querySelectorAll(sel).forEach(el => el.remove()));
405
  });
406
- return "Clutter removed from DOM.";
407
  }
408
  };
409
 
@@ -416,10 +431,65 @@ mcpServer.setRequestHandler(CallToolRequestSchema, async (req) => {
416
  });
417
 
418
  // ==========================================
419
- // SSE SERVER CONNECTION LOGIC
420
  // ==========================================
421
  const transports = new Map();
422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
423
  app.get('/sse', async (req, res) => {
424
  try {
425
  globalProtocol = req.headers['x-forwarded-proto'] || req.protocol;
@@ -441,6 +511,6 @@ app.post('/messages', async (req, res) => {
441
  });
442
 
443
  app.listen(PORT, async () => {
444
- console.log(`God-Mode MCP Server running on ${PORT}`);
445
  await initBrowser();
446
  });
 
32
  let networkRequests = [];
33
  let blockedUrls = [];
34
 
35
+ // --- LIVE STREAM STATE ---
36
+ let latestFrameBuffer = null;
37
+ let isScreencastRunning = false;
38
+ let lastStreamRequestTime = 0;
39
+
40
  async function initBrowser() {
41
+ console.log("Initializing Ultimate God-Mode Browser with Live Stream...");
42
  browser = await puppeteer.launch({
43
  headless: true,
44
  args: [
 
53
  const initialPages = await browser.pages();
54
  if (initialPages.length > 1) await initialPages[0].close();
55
 
56
+ console.log("Browser successfully initialized with Live Stream Ready!");
57
  }
58
 
59
  async function setupNewPage(newPage) {
 
61
  pages = await browser.pages();
62
  await page.setViewport({ width: 1920, height: 1080 });
63
 
 
64
  await page.evaluateOnNewDocument(() => {
65
  Object.defineProperty(navigator, 'webdriver', { get: () => undefined });
66
  window.navigator.chrome = { runtime: {} };
 
69
  });
70
  await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36');
71
 
72
+ // --- DEVTOOLS & SCREENCAST SETUP ---
73
  devToolsClient = await page.target().createCDPSession();
74
  consoleLogs = []; networkRequests = [];
75
 
 
82
  else interceptedRequest.continue();
83
  });
84
  page.on('dialog', async dialog => { await dialog.accept(); });
85
+
86
+ // Handle Live Screencast Frames
87
+ devToolsClient.on('Page.screencastFrame', async (event) => {
88
+ latestFrameBuffer = Buffer.from(event.data, 'base64');
89
+ try { await devToolsClient.send('Page.screencastFrameAck', { sessionId: event.sessionId }); } catch(e) {}
90
+ });
91
  }
92
 
93
+ // Auto-stop screencast if no viewers to save server memory
94
+ setInterval(() => {
95
+ if (isScreencastRunning && Date.now() - lastStreamRequestTime > 5000) {
96
+ if (devToolsClient) devToolsClient.send('Page.stopScreencast').catch(()=>{});
97
+ isScreencastRunning = false;
98
+ console.log("Live stream paused (No active viewers).");
99
+ }
100
+ }, 2000);
101
+
102
  // ==========================================
103
  // DEFINE 105 TOOLS SCHEMA
104
  // ==========================================
 
227
  { name: "emulate_color_scheme", description: "[F12] Dark/Light mode", inputSchema: { type: "object", properties: { theme: { type: "string", enum: ["dark", "light"] } }, required: ["theme"] } },
228
  { name: "clear_browser_cache", description: "[F12] Clear Cache", inputSchema: { type: "object", properties: {} } },
229
 
230
+ // --- ADVANCED AI ENGINE TOOLS (101-105) ---
 
 
231
  { name: "smart_scrape_markdown", description: "[ADVANCED ENGINE] Extract main article content and convert to Clean LLM-readable Markdown", inputSchema: { type: "object", properties: {} } },
232
  { name: "extract_structured_data", description: "[ADVANCED ENGINE] Extract structured data (Schema.org/JSON-LD)", inputSchema: { type: "object", properties: {} } },
233
+ { name: "auto_smart_scroll", description: "[ADVANCED ENGINE] Auto-scroll to load dynamic content", inputSchema: { type: "object", properties: {} } },
234
+ { name: "deep_domain_crawler", description: "[ADVANCED ENGINE] Extract internal links", inputSchema: { type: "object", properties: {} } },
235
+ { name: "clean_page_clutter", description: "[ADVANCED ENGINE] Force remove ads, popups", inputSchema: { type: "object", properties: {} } }
236
  ];
237
 
238
  mcpServer.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: toolSchemas }));
239
 
240
  // ==========================================
241
+ // TOOL EXECUTION HANDLERS
242
  // ==========================================
243
  const toolHandlers = {
244
  // Nav
 
370
  "emulate_color_scheme": async (a) => { await page.emulateMediaFeatures([{ name: 'prefers-color-scheme', value: a.theme }]); return "Theme set"; },
371
  "clear_browser_cache": async () => { await devToolsClient.send('Network.clearBrowserCache'); return "Cache cleared"; },
372
 
373
+ // AI Engine
 
 
374
  "smart_scrape_markdown": async () => {
375
  try {
376
  await page.addScriptTag({ url: 'https://unpkg.com/@mozilla/readability/Readability.js' });
 
402
  }, 100);
403
  });
404
  });
405
+ return "Smart scrolling finished.";
406
  },
407
  "deep_domain_crawler": async () => {
408
  const urlObj = new URL(await page.url());
 
418
  const selectors = ['header', 'footer', 'nav', '.ad', '.ads', '#cookie-banner', '.popup', 'iframe'];
419
  selectors.forEach(sel => document.querySelectorAll(sel).forEach(el => el.remove()));
420
  });
421
+ return "Clutter removed.";
422
  }
423
  };
424
 
 
431
  });
432
 
433
  // ==========================================
434
+ // EXPRESS ROUTES (INCLUDING LIVE STREAM)
435
  // ==========================================
436
  const transports = new Map();
437
 
438
+ // Endpoint to fetch the raw image buffer for live streaming
439
+ app.get('/latest-frame', async (req, res) => {
440
+ lastStreamRequestTime = Date.now();
441
+ if (!isScreencastRunning && devToolsClient) {
442
+ try {
443
+ await devToolsClient.send('Page.startScreencast', { format: 'jpeg', quality: 60, everyNthFrame: 1 });
444
+ isScreencastRunning = true;
445
+ console.log("Live stream started by viewer.");
446
+ } catch(e) {}
447
+ }
448
+
449
+ if (latestFrameBuffer) {
450
+ res.set('Content-Type', 'image/jpeg');
451
+ res.send(latestFrameBuffer);
452
+ } else {
453
+ res.status(404).send('Waiting for AI to start working...');
454
+ }
455
+ });
456
+
457
+ // The actual HTML page you will view or embed
458
+ app.get('/live', (req, res) => {
459
+ res.send(`
460
+ <!DOCTYPE html>
461
+ <html lang="en">
462
+ <head>
463
+ <meta charset="UTF-8">
464
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
465
+ <title>AI God-Mode Live Tracker</title>
466
+ <style>
467
+ body { margin: 0; background-color: #0f172a; display: flex; justify-content: center; align-items: center; height: 100vh; overflow: hidden; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
468
+ .container { position: relative; width: 100%; height: 100%; display: flex; flex-direction: column; justify-content: center; align-items: center; background-image: radial-gradient(#1e293b 1px, transparent 1px); background-size: 20px 20px; }
469
+ img { max-width: 95%; max-height: 90%; object-fit: contain; border-radius: 12px; box-shadow: 0 10px 40px rgba(16, 185, 129, 0.2); border: 2px solid #10b981; transition: opacity 0.2s; }
470
+ .status-bar { position: absolute; top: 20px; left: 20px; color: #10b981; font-weight: bold; font-size: 15px; background: rgba(15, 23, 42, 0.8); padding: 8px 15px; border-radius: 8px; border: 1px solid #1e293b; display: flex; align-items: center; gap: 10px; backdrop-filter: blur(5px); }
471
+ .dot { width: 12px; height: 12px; background-color: #10b981; border-radius: 50%; animation: pulse 1.5s infinite; }
472
+ @keyframes pulse { 0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7); } 70% { transform: scale(1); box-shadow: 0 0 0 10px rgba(16, 185, 129, 0); } 100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(16, 185, 129, 0); } }
473
+ </style>
474
+ </head>
475
+ <body>
476
+ <div class="container">
477
+ <div class="status-bar"><div class="dot"></div> LIVE AI TRACKING</div>
478
+ <img id="stream" src="" alt="Connecting to AI Browser..." />
479
+ </div>
480
+ <script>
481
+ const img = document.getElementById('stream');
482
+ // Fetch a fresh frame from the browser every 500ms
483
+ setInterval(() => {
484
+ img.src = '/latest-frame?t=' + new Date().getTime();
485
+ }, 500);
486
+ </script>
487
+ </body>
488
+ </html>
489
+ `);
490
+ });
491
+
492
+ // SSE MCP Endpoint
493
  app.get('/sse', async (req, res) => {
494
  try {
495
  globalProtocol = req.headers['x-forwarded-proto'] || req.protocol;
 
511
  });
512
 
513
  app.listen(PORT, async () => {
514
+ console.log(`God-Mode MCP Server running on ${PORT} with Live Streaming enabled!`);
515
  await initBrowser();
516
  });