File size: 5,579 Bytes
097eaf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { load } from "cheerio";
import { logger } from "../../../lib/logger";
import { extractImages as _extractImages } from "@mendable/firecrawl-rs";

function resolveImageUrl(
  src: string,
  baseUrl: string,
  baseHref: string = "",
): string {
  let resolutionBase = baseUrl;

  if (baseHref) {
    try {
      new URL(baseHref);
      resolutionBase = baseHref;
    } catch {
      try {
        resolutionBase = new URL(baseHref, baseUrl).href;
      } catch {
        resolutionBase = baseUrl;
      }
    }
  }

  try {
    // Skip data URIs and blob URLs
    if (src.startsWith("data:") || src.startsWith("blob:")) {
      return src;
    }

    // Handle absolute URLs
    if (src.startsWith("http://") || src.startsWith("https://")) {
      return src;
    }

    // Handle protocol-relative URLs
    if (src.startsWith("//")) {
      const protocol = new URL(baseUrl).protocol;
      return protocol + src;
    }

    // Handle relative URLs
    return new URL(src, resolutionBase).href;
  } catch (error) {
    logger.debug("Failed to resolve image URL", {
      src,
      baseUrl,
      error,
      module: "scrapeURL",
      method: "extractImages",
    });
    return "";
  }
}

async function extractImagesCheerio(
  html: string,
  baseUrl: string,
): Promise<string[]> {
  const $ = load(html);
  const baseHref = $("base[href]").first().attr("href") || "";
  const images: Set<string> = new Set();

  // Extract from <img> tags
  $("img").each((_, element) => {
    const src = $(element).attr("src");
    if (src) {
      const resolvedUrl = resolveImageUrl(src.trim(), baseUrl, baseHref);
      if (resolvedUrl) {
        images.add(resolvedUrl);
      }
    }

    // Also check data-src for lazy-loaded images
    const dataSrc = $(element).attr("data-src");
    if (dataSrc) {
      const resolvedUrl = resolveImageUrl(dataSrc.trim(), baseUrl, baseHref);
      if (resolvedUrl) {
        images.add(resolvedUrl);
      }
    }

    // Check srcset for responsive images
    const srcset = $(element).attr("srcset");
    if (srcset) {
      // Parse srcset: "url1 1x, url2 2x, ..."
      const urls = srcset.split(",").map(s => s.trim().split(/\s+/)[0]);
      urls.forEach(url => {
        if (url) {
          const resolvedUrl = resolveImageUrl(url, baseUrl, baseHref);
          if (resolvedUrl) {
            images.add(resolvedUrl);
          }
        }
      });
    }
  });

  // Extract from <picture> elements
  $("picture source").each((_, element) => {
    const srcset = $(element).attr("srcset");
    if (srcset) {
      const urls = srcset.split(",").map(s => s.trim().split(/\s+/)[0]);
      urls.forEach(url => {
        if (url) {
          const resolvedUrl = resolveImageUrl(url, baseUrl, baseHref);
          if (resolvedUrl) {
            images.add(resolvedUrl);
          }
        }
      });
    }
  });

  // Extract from meta tags (Open Graph, Twitter Cards)
  const metaImages = [
    $('meta[property="og:image"]').attr("content"),
    $('meta[property="og:image:url"]').attr("content"),
    $('meta[property="og:image:secure_url"]').attr("content"),
    $('meta[name="twitter:image"]').attr("content"),
    $('meta[name="twitter:image:src"]').attr("content"),
    $('meta[itemprop="image"]').attr("content"),
  ];

  metaImages.forEach(src => {
    if (src) {
      const resolvedUrl = resolveImageUrl(src.trim(), baseUrl, baseHref);
      if (resolvedUrl) {
        images.add(resolvedUrl);
      }
    }
  });

  // Extract from link tags (apple-touch-icon, etc.)
  $(
    'link[rel*="icon"], link[rel*="apple-touch-icon"], link[rel*="image_src"]',
  ).each((_, element) => {
    const href = $(element).attr("href");
    if (href) {
      const resolvedUrl = resolveImageUrl(href.trim(), baseUrl, baseHref);
      if (resolvedUrl) {
        images.add(resolvedUrl);
      }
    }
  });

  // Extract background images from inline styles
  $("[style*='background-image']").each((_, element) => {
    const style = $(element).attr("style") || "";
    const matches = style.match(
      /background-image:\s*url\(['"]?([^'")]+)['"]?\)/gi,
    );
    if (matches) {
      matches.forEach(match => {
        const urlMatch = match.match(/url\(['"]?([^'")]+)['"]?\)/i);
        if (urlMatch && urlMatch[1]) {
          const resolvedUrl = resolveImageUrl(
            urlMatch[1].trim(),
            baseUrl,
            baseHref,
          );
          if (resolvedUrl) {
            images.add(resolvedUrl);
          }
        }
      });
    }
  });

  // Extract from video poster attributes
  $("video[poster]").each((_, element) => {
    const poster = $(element).attr("poster");
    if (poster) {
      const resolvedUrl = resolveImageUrl(poster.trim(), baseUrl, baseHref);
      if (resolvedUrl) {
        images.add(resolvedUrl);
      }
    }
  });

  // Filter out invalid URLs and convert Set to Array
  return Array.from(images).filter(url => {
    try {
      // Skip javascript: URLs for security
      if (url.toLowerCase().startsWith("javascript:")) {
        return false;
      }
      new URL(url);
      return true;
    } catch {
      return false;
    }
  });
}

export async function extractImages(
  html: string,
  baseUrl: string,
): Promise<string[]> {
  try {
    return await _extractImages(html, baseUrl);
  } catch (error) {
    logger.warn("Failed to call html-transformer! Falling back to cheerio...", {
      error,
      module: "scrapeURL",
      method: "extractImages",
    });

    // Fallback to Cheerio implementation
    return await extractImagesCheerio(html, baseUrl);
  }
}