import dotenv from 'dotenv';
dotenv.config();
import { BrowserScraper } from './build/scraper/browser_scraper.js';
async function testHeadlessScraper() {
console.log('π€ Testing HEADLESS Browser Scraper');
console.log('ββββββββββββββββββββββββββββββββββββββ\n');
console.log('β
Browser will run invisibly in background');
console.log(' No Chrome window will appear\n');
const apiKey = process.env.OPENAI_API_KEY;
if (!apiKey) {
console.error('β OPENAI_API_KEY not found!');
process.exit(1);
}
const scraper = new BrowserScraper(apiKey, '999059198');
const orgNr = '999059198'; // Test company
console.log(`π Starting headless scraping for company ${orgNr}...`);
console.log('β³ This will take 30-60 seconds...\n');
try {
const startTime = Date.now();
// This should run completely in the background
const results = await scraper.getAllFinancialYears(orgNr);
const duration = Math.round((Date.now() - startTime) / 1000);
console.log('\nββββββββββββββββββββββββββββββββββββββ');
console.log('β
HEADLESS SCRAPING COMPLETE!');
console.log('ββββββββββββββββββββββββββββββββββββββ\n');
console.log(`β±οΈ Time taken: ${duration} seconds`);
console.log(`π Years processed: ${results.length}`);
const withData = results.filter(r => r.revenue !== null || r.profit !== null);
console.log(`β
Years with data: ${withData.length}`);
if (withData.length > 0) {
console.log('\nπ Sample results:');
withData.slice(0, 3).forEach(r => {
const revenue = r.revenue ? (r.revenue/1000000).toFixed(1) + 'M' : 'N/A';
const profit = r.profit ? (r.profit/1000000).toFixed(1) + 'M' : 'N/A';
console.log(` ${r.year}: Revenue=${revenue}, Profit=${profit}`);
});
}
console.log('\nπ Headless mode working perfectly!');
console.log(' No browser window was shown');
console.log(' All PDFs downloaded in background');
console.log(' Data extracted successfully');
} catch (error) {
console.error('β Headless scraping failed:', error.message || error);
console.error('\nThis might indicate:');
console.error('- Headless mode compatibility issue');
console.error('- Download path problems in headless mode');
}
}
testHeadlessScraper();