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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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/**
 * Content Extractor V2 - Enhanced Full Paragraph Extraction
 * Extracts ALL paragraphs from article with structure preservation
 */

const ContentExtractorV2 = {
  /**
   * Extract complete article content with all paragraphs
   */
  extractFullContent() {
    console.log('πŸ” Starting enhanced content extraction...');
    
    const content = {
      title: '',
      subtitle: '',
      author: '',
      publishDate: '',
      source: window.location.hostname,
      url: window.location.href,
      pageType: this.detectPageType(),
      contentType: this.detectContentType(),
      paragraphs: [],  // Array of paragraph objects
      images: [],
      metadata: {},
      fullText: '',
      confidence: 0
    };

    // Extract metadata first
    content.metadata = this.extractMetadata();
    content.title = this.extractTitle();
    content.author = content.metadata.author;
    content.publishDate = content.metadata.publishDate;

    // Extract ALL paragraphs with structure
    const paragraphsData = this.extractAllParagraphs();
    content.paragraphs = paragraphsData.paragraphs;
    content.fullText = paragraphsData.fullText;
    content.images = this.extractImages();

    // Calculate extraction confidence
    content.confidence = this.calculateConfidence(content);

    console.log('βœ… Extraction complete:', {
      title: content.title,
      paragraphs: content.paragraphs.length,
      characters: content.fullText.length,
      pageType: content.pageType,
      contentType: content.contentType,
      confidence: content.confidence
    });

    return content;
  },

  /**
   * Extract ALL paragraphs from the article
   * Returns array of paragraph objects with metadata
   */
  extractAllParagraphs() {
    console.log('πŸ“ Extracting all paragraphs...');
    
    const paragraphs = [];
    let fullText = '';
    let paragraphIndex = 0;

    // Find the main article container
    const articleElement = this.findArticleContainer();
    
    if (!articleElement) {
      console.warn('⚠️ No article container found, using body');
      return this.fallbackExtraction();
    }

    console.log('βœ“ Found article container:', articleElement.tagName, articleElement.className);

    // Extract all paragraph elements
    const paragraphElements = articleElement.querySelectorAll('p, h1, h2, h3, h4, h5, h6, li, blockquote, figcaption');
    
    console.log(`πŸ“„ Found ${paragraphElements.length} paragraph elements`);

    paragraphElements.forEach((element, index) => {
      const text = this.cleanText(element.textContent);
      
      // Skip if too short (likely not content)
      if (text.length < 20) {
        return;
      }

      // Skip if it's a navigation/ad element
      if (this.isNonContentElement(element)) {
        return;
      }

      const paragraph = {
        index: paragraphIndex,
        type: element.tagName.toLowerCase(),
        text: text,
        length: text.length,
        element_id: element.id || null,
        element_class: element.className || null,
        xpath: this.getXPath(element),  // For precise location
        position: {
          top: element.offsetTop,
          left: element.offsetLeft
        }
      };

      paragraphs.push(paragraph);
      fullText += text + '\n\n';
      paragraphIndex++;
    });

    console.log(`βœ… Extracted ${paragraphs.length} valid paragraphs`);

    return {
      paragraphs: paragraphs,
      fullText: fullText.trim()
    };
  },

  /**
   * Find the main article container element
   */
  findArticleContainer() {
    // Priority selectors for article content
    const selectors = [
      // Semantic HTML
      'article[role="main"]',
      'article[role="article"]',
      'main article',
      'article',
      '[role="article"]',
      '[role="main"]',
      'main',
      
      // Schema.org
      '[itemprop="articleBody"]',
      '[itemtype*="Article"]',
      
      // Common class patterns
      '.article-body',
      '.article-content',
      '.article__body',
      '.story-body',
      '.post-content',
      '.entry-content',
      '.post-body',
      '.content-body',
      
      // News-specific
      '.ins_storybody',  // NDTV
      '.story-body__inner',  // BBC
      '.article__content',  // Guardian
      '.article-text',  // Hindu
      '.single-post',  // PageSix, NY Post
      '.entry__content',  // PageSix
      '.single__content',  // PageSix
      '.post__content',  // Various news sites
      '.story__content'  // Various news sites
    ];

    for (const selector of selectors) {
      const element = document.querySelector(selector);
      if (element && element.textContent.trim().length > 100) {
        return element;
      }
    }

    // Fallback: Find largest text container
    return this.findLargestTextContainer();
  },

  /**
   * Find the element with the most paragraph content
   */
  findLargestTextContainer() {
    const candidates = document.querySelectorAll('div, section, article');
    let largest = null;
    let maxParagraphs = 0;

    candidates.forEach(element => {
      const paragraphs = element.querySelectorAll('p');
      const textLength = element.textContent.trim().length;
      
      if (paragraphs.length > maxParagraphs && textLength > 500) {
        maxParagraphs = paragraphs.length;
        largest = element;
      }
    });

    return largest;
  },

  /**
   * Check if element is likely non-content (nav, ads, etc.)
   */
  isNonContentElement(element) {
    const text = element.textContent.toLowerCase();
    const classList = element.className.toLowerCase();
    const id = element.id?.toLowerCase() || '';

    // Check for timestamps (e.g., "2 hours ago", "Published: Jan 10, 2025", "Updated: 3:45 PM")
    const timestampPatterns = [
      /\d{1,2}\s*(hours?|mins?|minutes?|seconds?|days?|weeks?|months?|years?)\s*ago/i,
      /published:?\s*\d/i,
      /updated:?\s*\d/i,
      /\d{1,2}:\d{2}\s*(am|pm)/i,
      /^\s*\d{1,2}\/\d{1,2}\/\d{2,4}\s*$/,  // Date format: 10/18/2025
      /^(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[a-z]*\s+\d{1,2},?\s+\d{4}/i  // Jan 10, 2025
    ];
    
    if (timestampPatterns.some(pattern => pattern.test(text))) {
      return true;
    }

    // Check for navigation/UI text
    const navPatterns = [
      'skip to', 'menu', 'navigation', 'cookie', 'subscribe',
      'newsletter', 'advertisement', 'related articles', 'read more',
      'share this', 'follow us', 'sponsored', 'powered by', 'published',
      'updated', 'posted', 'last modified'
    ];

    if (navPatterns.some(pattern => text.includes(pattern))) {
      return true;
    }

    // Check classes/IDs
    const excludePatterns = [
      'nav', 'menu', 'footer', 'header', 'sidebar', 'ad', 'advertisement',
      'related', 'recommend', 'share', 'social', 'comment', 'cookie',
      'timestamp', 'date', 'time', 'publish', 'author', 'byline'
    ];

    if (excludePatterns.some(pattern => classList.includes(pattern) || id.includes(pattern))) {
      return true;
    }

    return false;
  },

  /**
   * Get XPath of element for precise location
   */
  getXPath(element) {
    if (element.id) {
      return `//*[@id="${element.id}"]`;
    }
    
    const parts = [];
    while (element && element.nodeType === Node.ELEMENT_NODE) {
      let index = 0;
      let sibling = element.previousSibling;
      
      while (sibling) {
        if (sibling.nodeType === Node.ELEMENT_NODE && sibling.nodeName === element.nodeName) {
          index++;
        }
        sibling = sibling.previousSibling;
      }
      
      const tagName = element.nodeName.toLowerCase();
      const pathIndex = index > 0 ? `[${index + 1}]` : '';
      parts.unshift(tagName + pathIndex);
      element = element.parentNode;
    }
    
    return parts.length ? '/' + parts.join('/') : '';
  },

  /**
   * Fallback extraction if no article container found
   */
  fallbackExtraction() {
    console.log('⚠️ Using fallback extraction - extracting ALL <p> tags from page');
    
    const paragraphs = [];
    const allParagraphs = document.querySelectorAll('p');
    let fullText = '';
    
    console.log(`πŸ“„ Found ${allParagraphs.length} total <p> tags on page`);
    
    allParagraphs.forEach((p, index) => {
      const text = this.cleanText(p.textContent);
      
      // βœ… VERY LENIENT: Accept even short paragraphs (15+ chars)
      if (text.length < 15) {
        return;
      }
      
      // βœ… Skip ONLY obvious non-content (but be lenient)
      if (this.isNonContentElement(p)) {
        console.log(`   ⏭️ Skipping: "${text.substring(0, 50)}..."`);
        return;
      }
      
      paragraphs.push({
        index: paragraphs.length,  // Use running index, not DOM index
        type: 'p',
        text: text,
        length: text.length,
        xpath: this.getXPath(p)
      });
      fullText += text + '\n\n';
    });
    
    console.log(`βœ… Fallback extracted ${paragraphs.length} paragraphs`);

    return { paragraphs, fullText };
  },

  /**
   * Detect page type (news, blog, social media, etc.)
   */
  detectPageType() {
    const url = window.location.href.toLowerCase();
    const hostname = window.location.hostname.toLowerCase();

    // News portals
    const newsPortals = [
      'bbc.com', 'cnn.com', 'nytimes.com', 'theguardian.com', 'reuters.com',
      'ndtv.com', 'timesofindia.com', 'hindustantimes.com', 'thehindu.com'
    ];

    if (newsPortals.some(domain => hostname.includes(domain))) {
      return 'news-article';
    }

    // Check for article indicators
    if (document.querySelector('article') || document.querySelector('[itemtype*="Article"]')) {
      return 'article';
    }

    // Blog detection
    if (url.includes('blog') || hostname.includes('wordpress') || hostname.includes('blogger')) {
      return 'blog-post';
    }

    return 'webpage';
  },

  /**
   * Detect content type from metadata and structure
   */
  detectContentType() {
    // Check meta tags
    const ogType = document.querySelector('meta[property="og:type"]')?.content;
    const articleSection = document.querySelector('meta[property="article:section"]')?.content;
    
    if (ogType === 'article') {
      if (articleSection) {
        return {
          type: 'news-article',
          category: articleSection.toLowerCase(),
          confidence: 0.9
        };
      }
      return { type: 'article', confidence: 0.8 };
    }

    // Check for opinion/editorial indicators
    const title = document.title.toLowerCase();
    const url = window.location.href.toLowerCase();
    
    if (title.includes('opinion') || url.includes('opinion') || 
        title.includes('editorial') || url.includes('editorial')) {
      return { type: 'opinion-piece', confidence: 0.85 };
    }

    // Check for scientific paper
    if (document.querySelector('meta[name="citation_title"]')) {
      return { type: 'scientific-paper', confidence: 0.95 };
    }

    // Default
    return { type: 'general-content', confidence: 0.5 };
  },

  /**
   * Extract title from various sources
   */
  extractTitle() {
    // Try Open Graph
    const ogTitle = document.querySelector('meta[property="og:title"]');
    if (ogTitle) return ogTitle.content;

    // Try headline element
    const h1 = document.querySelector('article h1, .article h1, h1.title, h1.headline, main h1');
    if (h1) return h1.textContent.trim();

    // Fallback to page title
    return document.title || 'Untitled';
  },

  /**
   * Extract metadata (author, date, etc.)
   */
  extractMetadata() {
    const metadata = {
      author: '',
      publishDate: '',
      modifiedDate: '',
      section: '',
      tags: []
    };

    // Author
    const authorMeta = document.querySelector('meta[name="author"], meta[property="article:author"]');
    const authorElement = document.querySelector('[rel="author"], .author, .byline, [itemprop="author"]');
    metadata.author = authorMeta?.content || authorElement?.textContent.trim() || '';

    // Publish date
    const dateMeta = document.querySelector('meta[property="article:published_time"], meta[name="publish-date"]');
    const dateElement = document.querySelector('time[datetime], .publish-date, [itemprop="datePublished"]');
    metadata.publishDate = dateMeta?.content || dateElement?.getAttribute('datetime') || dateElement?.textContent.trim() || '';

    // Section/category
    const sectionMeta = document.querySelector('meta[property="article:section"]');
    metadata.section = sectionMeta?.content || '';

    // Tags
    const tagMeta = document.querySelectorAll('meta[property="article:tag"]');
    metadata.tags = Array.from(tagMeta).map(tag => tag.content);

    return metadata;
  },

  /**
   * Extract images from article
   */
  extractImages() {
    const images = [];
    const articleElement = this.findArticleContainer();
    
    if (!articleElement) return images;

    const imgElements = articleElement.querySelectorAll('img');
    
    imgElements.forEach((img, index) => {
      if (img.width > 100 && img.height > 100) {  // Skip small images (icons, etc.)
        images.push({
          index: index,
          src: img.src,
          alt: img.alt || '',
          caption: img.parentElement.querySelector('figcaption')?.textContent.trim() || ''
        });
      }
    });

    return images;
  },

  /**
   * Clean text content
   */
  cleanText(text) {
    return text
      .replace(/\s+/g, ' ')
      .replace(/\n{3,}/g, '\n\n')
      .trim();
  },

  /**
   * Calculate extraction confidence score
   */
  calculateConfidence(content) {
    let score = 0;

    // Has title
    if (content.title && content.title.length > 5) score += 20;

    // Has paragraphs
    if (content.paragraphs.length > 0) score += 20;
    if (content.paragraphs.length > 3) score += 10;
    if (content.paragraphs.length > 10) score += 10;

    // Has metadata
    if (content.author) score += 10;
    if (content.publishDate) score += 10;

    // Has sufficient text
    if (content.fullText.length > 500) score += 10;
    if (content.fullText.length > 2000) score += 10;

    return Math.min(score, 100);
  }
};

// Make it globally available
if (typeof window !== 'undefined') {
  window.ContentExtractorV2 = ContentExtractorV2;
}