// Global declaration for Chrome extension API declare const chrome: { runtime?: { getManifest?: () => { version: string }; getURL?: (path: string) => string; }; }; /** * Simple BuzzTrail.txt Parser * Focuses on site-specific information like payments and offerings */ export type PartnershipTier = 'bronze' | 'silver' | 'gold' | 'platinum'; export interface SitePaymentConfig { partnershipTier: PartnershipTier; paysForText: boolean; paysForVoice: boolean; paysForVideo: boolean; showCosts: boolean; currency: 'USD' | 'EUR' | 'GBP'; } export interface SiteOfferingConfig { offeringType: string; offersCredits: boolean; creditAmount?: number; providesGuidance: boolean; } export interface BuzzTrailConfig { domain: string; siteName: string; description: string; offering: SiteOfferingConfig; payment: SitePaymentConfig; schemaVersion: string; lastUpdated: string; isActive: boolean; } export class BuzzTrailParser { public static async parseBuzzTrailFile(url: string): Promise { try { // First, try to load from local test configurations during development const localConfig = await this.loadLocalTestConfig(url); if (localConfig) { console.log(`[DEV] Loaded local test config for ${url}`); return localConfig; } // Fall back to fetching from the actual website const response = await fetch(`${url}/buzztrail.txt`); if (!response.ok) { return this.createDefaultConfig(url); } const content = await response.text(); return this.parseContent(content, url); } catch (error) { console.error('Failed to fetch buzztrail.txt:', error); return this.createDefaultConfig(url); } } private static async loadLocalTestConfig(url: string): Promise { try { // Only load local configs in development mode if (chrome?.runtime?.getManifest) { const manifest = chrome.runtime.getManifest(); const isDevelopment = manifest.version === '0.0.0' || process.env.NODE_ENV === 'development'; if (!isDevelopment) { return null; } } const domain = this.extractDomain(url); if (!chrome.runtime?.getURL) { return null; } const testConfigPath = chrome.runtime.getURL('testing/buzztrail-examples/test-config.json'); const configResponse = await fetch(testConfigPath); if (!configResponse.ok) { return null; } const config = await configResponse.json(); const siteConfig = config.testSites?.[domain]; if (!siteConfig?.file) { return null; } // Load the specific buzztrail.txt file const filePath = chrome.runtime?.getURL?.(`testing/buzztrail-examples/${siteConfig.file}`); if (!filePath) { return null; } const fileResponse = await fetch(filePath); if (!fileResponse.ok) { return null; } const content = await fileResponse.text(); return this.parseContent(content, url); } catch (error) { console.debug('No local test config found, falling back to remote:', error); return null; } } public static parseContent(content: string, domain: string): BuzzTrailConfig { return { domain: this.extractDomain(domain), siteName: this.extractSection(content, 'Site Name') ?? this.extractDomain(domain), description: this.extractSection(content, 'Description') ?? '', offering: this.parseOfferingSection(content), payment: this.parsePaymentSection(content), schemaVersion: '1.0.0', lastUpdated: new Date().toISOString(), isActive: true }; } private static extractSection(content: string, sectionName: string): string | null { const regex = new RegExp(`${sectionName}:\\s*([\\s\\S]*?)(?=\\n\\w+:|$)`, 'i'); const match = content.match(regex); return match ? match[1].trim() : null; } private static parseOfferingSection(content: string): SiteOfferingConfig { return { offeringType: this.extractSection(content, 'Offering Type') ?? 'general', offersCredits: this.extractSection(content, 'Offers Credits')?.toLowerCase() === 'true', creditAmount: ((): number | undefined => { const amount = this.extractSection(content, 'Credit Amount'); if (!amount) return undefined; const parsed = parseInt(amount); return isNaN(parsed) ? undefined : parsed; })(), providesGuidance: this.extractSection(content, 'Provides Guidance')?.toLowerCase() === 'true' }; } private static parsePaymentSection(content: string): SitePaymentConfig { return { partnershipTier: (this.extractSection(content, 'Partnership Tier') as PartnershipTier) || 'bronze', paysForText: this.extractSection(content, 'Pays For Text')?.toLowerCase() === 'true', paysForVoice: this.extractSection(content, 'Pays For Voice')?.toLowerCase() === 'true', paysForVideo: this.extractSection(content, 'Pays For Video')?.toLowerCase() === 'true', showCosts: this.extractSection(content, 'Show Costs')?.toLowerCase() !== 'false', currency: (this.extractSection(content, 'Currency') as 'USD' | 'EUR' | 'GBP') || 'USD' }; } private static extractDomain(url: string): string { try { const domain = new URL(url).hostname; return domain.startsWith('www.') ? domain.substring(4) : domain; } catch { return url.replace(/^https?:\/\//, '').replace(/\/.*$/, ''); } } private static createDefaultConfig(url: string): BuzzTrailConfig { return { domain: this.extractDomain(url), siteName: this.extractDomain(url), description: '', offering: { offeringType: 'general', offersCredits: false, providesGuidance: false }, payment: { partnershipTier: 'bronze', paysForText: false, paysForVoice: false, paysForVideo: false, showCosts: true, currency: 'USD' }, schemaVersion: '1.0.0', lastUpdated: new Date().toISOString(), isActive: true }; } }