const express = require('express'); const puppeteer = require('puppeteer'); const PuppeteerHar = require('puppeteer-har'); const fs = require('fs'); const path = require('path'); const app = express(); const PORT = 3000; let browser; // Define the browser globally to control its lifecycle let page; // Define the page globally for reusability // 1. Capture Full HAR app.post('/captureFullHAR', async (req, res) => { const outputFilePath = path.join(__dirname, 'full_har.txt'); try { if (!browser) browser = await puppeteer.launch(); page = await browser.newPage(); const har = new PuppeteerHar(page); await har.start(); await page.goto('https://example.com'); const harData = await har.stop(); fs.writeFileSync(outputFilePath, JSON.stringify(harData, null, 2)); res.json({ message: 'Full HAR captured and saved as full_har.txt!' }); } catch (error) { res.status(500).json({ message: 'Error capturing HAR', error: error.message }); } }); // 2. Capture Fetch Requests Only app.post('/captureFetchRequests', async (req, res) => { const outputFilePath = path.join(__dirname, 'fetch_requests.txt'); try { const logs = []; page.on('request', request => { if (request.resourceType() === 'fetch') { logs.push({ url: request.url(), method: request.method(), headers: request.headers(), }); } }); await page.goto('https://example.com'); fs.writeFileSync(outputFilePath, JSON.stringify(logs, null, 2)); res.json({ message: 'Fetch requests captured and saved as fetch_requests.txt!' }); } catch (error) { res.status(500).json({ message: 'Error capturing fetch requests', error: error.message }); } }); // 3. Take Screenshot app.post('/takeScreenshot', async (req, res) => { const outputFilePath = path.join(__dirname, 'screenshot.png'); try { await page.goto('https://example.com'); await page.screenshot({ path: outputFilePath }); res.json({ message: 'Screenshot taken and saved as screenshot.png!' }); } catch (error) { res.status(500).json({ message: 'Error taking screenshot', error: error.message }); } }); // 4. Log All Network Activity app.post('/logNetworkActivity', async (req, res) => { const outputFilePath = path.join(__dirname, 'network_log.txt'); try { const networkLogs = []; page.on('request', request => { networkLogs.push({ url: request.url(), method: request.method(), type: request.resourceType(), }); }); await page.goto('https://example.com'); fs.writeFileSync(outputFilePath, JSON.stringify(networkLogs, null, 2)); res.json({ message: 'Network activity logged and saved as network_log.txt!' }); } catch (error) { res.status(500).json({ message: 'Error logging network activity', error: error.message }); } }); // 5. Save Page Content app.post('/savePageContent', async (req, res) => { const outputFilePath = path.join(__dirname, 'page_content.html'); try { const content = await page.content(); fs.writeFileSync(outputFilePath, content); res.json({ message: 'Page content saved as page_content.html!' }); } catch (error) { res.status(500).json({ message: 'Error saving page content', error: error.message }); } }); // 6. Close Browser app.post('/closeBrowser', async (req, res) => { try { if (browser) await browser.close(); browser = null; res.json({ message: 'Browser closed successfully!' }); } catch (error) { res.status(500).json({ message: 'Error closing browser', error: error.message }); } }); app.listen(PORT, () => { console.log(`Server running on http://localhost:${PORT}`); });