Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | // 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<BuzzTrailConfig> {
try {
// First, try to load from local test configurations during development
const localConfig = await this.loadLocalTestConfig(url);
Iif (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`);
Iif (!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<BuzzTrailConfig | null> {
try {
// Only load local configs in development mode
Iif (chrome?.runtime?.getManifest) {
const manifest = chrome.runtime.getManifest();
const isDevelopment = manifest.version === '0.0.0' || process.env.NODE_ENV === 'development';
Iif (!isDevelopment) {
return null;
}
}
const domain = this.extractDomain(url);
Iif (!chrome.runtime?.getURL) {
return null;
}
const testConfigPath = chrome.runtime.getURL('testing/buzztrail-examples/test-config.json');
const configResponse = await fetch(testConfigPath);
Iif (!configResponse.ok) {
return null;
}
const config = await configResponse.json();
const siteConfig = config.testSites?.[domain];
Iif (!siteConfig?.file) {
return null;
}
// Load the specific buzztrail.txt file
const filePath = chrome.runtime?.getURL?.(`testing/buzztrail-examples/${siteConfig.file}`);
Iif (!filePath) {
return null;
}
const fileResponse = await fetch(filePath);
Iif (!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');
Iif (!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
};
}
} |